Created
July 19, 2016 21:25
-
-
Save i0nyx/f5385d05936f35006288d20ea027b537 to your computer and use it in GitHub Desktop.
Семья
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.javarush.test.level07.lesson12.home06; | |
/* Семья | |
Создай класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human). Создай объекты и заполни их так, чтобы получилось: | |
Два дедушки, две бабушки, отец, мать, трое детей. Вывести объекты на экран. | |
Примечание: | |
Если написать свой метод String toString() в классе Human, то именно он будет использоваться при выводе объекта на экран. | |
Пример вывода: | |
Имя: Аня, пол: женский, возраст: 21, отец: Павел, мать: Катя | |
Имя: Катя, пол: женский, возраст: 55 | |
Имя: Игорь, пол: мужской, возраст: 2, отец: Михаил, мать: Аня | |
… | |
*/ | |
public class Solution | |
{ | |
public static void main(String[] args) | |
{ | |
//напишите тут ваш код | |
Human gfather = new Human("gfather", true, 60, null, null); | |
Human gmother = new Human("gmother", false, 54, null, null); | |
Human gfather2 = new Human("gfather2", true, 62, null, null); | |
Human gmother2 = new Human("gmother2", false, 59, null, null); | |
Human father = new Human("father", true, 40, gfather, gmother); | |
Human mother = new Human("mother", false, 37, gfather2, gmother2); | |
Human children = new Human("children", true, 13, father, mother); | |
Human children2 = new Human("children2", false, 9, father, mother); | |
Human children3 = new Human("children3", false, 3, father, mother); | |
System.out.println(gfather); | |
System.out.println(gfather2); | |
System.out.println(gmother); | |
System.out.println(gmother2); | |
System.out.println(father); | |
System.out.println(mother); | |
System.out.println(children); | |
System.out.println(children2); | |
System.out.println(children3); | |
} | |
public static class Human | |
{ | |
//напишите тут ваш код | |
String name; | |
Boolean sex; | |
int age; | |
Human father; | |
Human mother; | |
public Human(String name, Boolean sex, int age, Human father, Human mother){ | |
this.name = name; | |
this.sex = sex ? true : false; //можно без "? true : false" | |
this.age = age; | |
this.father = father; | |
this.mother = mother; | |
} | |
public String toString() | |
{ | |
String text = ""; | |
text += "Имя: " + this.name; | |
text += ", пол: " + (this.sex ? "мужской" : "женский"); | |
text += ", возраст: " + this.age; | |
if (this.father != null) | |
text += ", отец: " + this.father.name; | |
if (this.mother != null) | |
text += ", мать: " + this.mother.name; | |
return text; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment