Полагаю, что дело в конструкторе. Но не понимаю где и почему.
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human brother=new Human("Sergey", true, 26);
Human sister=new Human("Masha", false, 14);
Human iAm=new Human("Kirill", true, 24);
ArrayList<Human> children=new ArrayList<>();
children.add(brother);
children.add(sister);
children.add(iAm);
Human father=new Human("Vladimir", true, 47, children);
ArrayList<Human> fatherArray=new ArrayList<>();
fatherArray.add(father);
Human mother=new Human("Kira", false, 45, children);
ArrayList<Human> motherArray=new ArrayList<>();
fatherArray.add(mother);
Human grandFather1=new Human("Vladimir", true, 70, fatherArray);
Human grandFather2=new Human("VladimirSadko", true, 73, motherArray);
Human grandMother1=new Human("Vala", false, 68, fatherArray);
Human grandMother2=new Human("Natasha", false, 75, motherArray);
ArrayList<Human> allFamily=new ArrayList<>();
allFamily.add(brother);
allFamily.add(sister);
allFamily.add(iAm);
allFamily.add(father);
allFamily.add(mother);
allFamily.add(grandFather1);
allFamily.add(grandFather2);
allFamily.add(grandMother1);
allFamily.add(grandMother2);
for(int i=0; i<allFamily.size(); i++) {
System.out.println(allFamily.get(i).toString());
}
//напишите тут ваш код
}
public static class Human {
String name;
Boolean sex;
int age;
ArrayList<Human> children;
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, ArrayList<Human> children) {
this.name=name;
this.sex=sex;
this.age=age;
this.children=children;
}
//напишите тут ваш код
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", дети: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}