Вроде все работает у меня)
package com.javarush.task.jdk13.task08.task0824;
import java.util.ArrayList;
/*
Вся семья в сборе
*/
public class Solution {
public static void main(String[] args) {
ArrayList<Human> children = new ArrayList<Human>();
Human child1 = new Human("Vasya", true, 12);
Human child2 = new Human("Lelik", true, 12);
Human child3 = new Human("Senya", true, 12);
children.add(child1);
children.add(child2);
children.add(child3);
Human parent1 = new Human("Batya", true, 35, children);
Human parent2 = new Human("Mats", false, 38, children);
Human grandparent1 = new Human("Deda1", true, 88);
Human grandparent2 = new Human("Deda2", true, 86);
Human grandparent3 = new Human("Babulya1", false, 88);
Human grandparent4 = new Human("Babulya2", false, 88);
System.out.println(grandparent1);
System.out.println(grandparent2);
System.out.println(grandparent3);
System.out.println(grandparent4);
System.out.println(parent1);
System.out.println(parent2);
System.out.println(child1);
System.out.println(child2);
System.out.println(child3);
}
public static class Human {
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
Human (String name, boolean sex, int age, ArrayList<Human> children){
this.name = name;
this.sex = sex;
this.age = age;
this.children = children;
}
Human (String name, boolean sex, int age){
this.name = name;
this.sex = sex;
this.age = age;
}
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;
}
}
}