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 grandpa1 = new Human("Nikolay", true, 72, null, null);
Human grandpa2 = new Human("Alexey,", true, 67, null, null);
Human grandma1 = new Human("Olga", false, 71, null, null);
Human grandma2 = new Human("Irina", false, 70, null, null);
Human father = new Human("Pavel", true, 47, grandpa1, grandma1);
Human mother = new Human("Ekaterina", false, 46, grandpa2, grandma2);
Human child1 = new Human("Ivan", true, 20, father, mother);
Human child2 = new Human("Maria", false, 23, father, mother);
Human child3 = new Human("Stanislav", true, 18, father, mother);
System.out.println(grandpa1);
System.out.println(grandpa2);
System.out.println(grandma1);
System.out.println(grandma2);
System.out.println(father);
System.out.println(mother);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
}
public static class Human
{
//напишите тут ваш код
public String name;
public boolean sex;
public int age;
public Human father;
public Human mother;
Human(String name, boolean sex, int age, Human father, Human mother)
{
this.name = name;
this.sex = sex;
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;
}
}
}
Leave A Comment