Комрады, товарищи! Трижды пересчитывал объекты! И с матерью и без матери все ровно! Даже на выводе. Перед каждым new, новее не придумаешь. Что не так?
package com.javarush.task.task07.task0724;
/*
Семейная перепись
*/
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
ArrayList<Human> list = new ArrayList<>();//напишите тут ваш код
list.add(new Human("Vasja", true,24 ));
list.add(new Human("Sveta", false,21 ));
list.add(new Human("Kolja", true,22 ));
list.add(new Human("Ljuba", false,27 ));
list.add(new Human("Kayja", false,27, new Human("Fedja", true,60),new Human("Olja", false, 44) ));
list.add(new Human("Nastja", false,29, new Human("Viktor", true,62),new Human("Ksjusha", false, 44) ));
list.add(new Human("Oleg", true,15, new Human("Andrej", true,54),new Human("Marina", false, 44) ));
list.add(new Human("Pavel", true,14, new Human("Ivan", true,57),new Human("Julja", false, 44) ));
list.add(new Human("Sanjz", true,12, new Human("Slava", true,49),new Human("Ira", false, 44) ));
for (Human human: list
) {
System.out.println(human);
}
}
public static class Human {
String name;
boolean sex;
int age;//напишите тут ваш код
Human father, mother;
public Human(String name, boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
public 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;
}
}
}