Валидатор не пропустил, ругался на то что поля в классе Human помечены модификатором доступа private, исправил на public и решение принято. Но! Растолкуйте чем чревато оставить модификатор private?
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("Аркадий", false, 15);
Human child2 = new Human("Анжела", true, 16);
Human child3 = new Human("Валера", false, 17);
Human father = new Human("Олег", false, 37, child1, child2, child3);
Human mother = new Human("Наталья", true, 35, child1, child2, child3);
Human grandfather1 = new Human("Леонид", false, 70, father);
Human grandfather2 = new Human("Ефим", false, 71, mother);
Human grandmother1 = new Human("Надежда", true, 67, father);
Human grandmother2 = new Human("Любовь", true, 68, mother);
System.out.println(grandfather1.toString());
System.out.println(grandfather2.toString());
System.out.println(grandmother1.toString());
System.out.println(grandfather2.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
System.out.println(child1.toString());
System.out.println(child2.toString());
System.out.println(child3.toString());
//напишите тут ваш код
}
public static class Human {
public String name;
public boolean sex;
public int age;
public ArrayList<Human> children = new ArrayList<>();
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... children){
this.name = name;
this.sex = sex;
this.age = age;
Collections.addAll(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;
}
}
}