Вот код, решил задачу, вроде работает правильно, но не хочет пропускать последний метод, почему можете разобраться?
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
/*
Собираем семейство
*/
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("Baby1", true, 5);
Human child2 = new Human("Baby2", true, 4);
Human child3 = new Human("Baby3", false, 3);
Human father = new Human("Dad", true, 26, child1, child2, child3);
Human mother = new Human("Mom", false, 25, child1, child2, child3);
Human grandfather1 = new Human("Grandfather1", true, 51, father, mother);
Human grandfather2 = new Human("Grandfather2", true, 52, father, mother);
Human grandmother1 = new Human("Granny1", false, 54, father, mother);
Human grandmother2 = new Human("Granny2", false, 55, father, mother);
System.out.println(grandfather1.toString());
System.out.println(grandfather2.toString());
System.out.println(grandmother1.toString());
System.out.println(grandmother2.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 {
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<Human>();
public Human(String name, boolean sex, int age) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public Human(String name, boolean sex, int age, Human children1, Human children2) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.children.add(children1);
this.children.add(children2);
}
public Human(String name, boolean sex, int age, Human children1, Human children2, Human children3) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.children.add(children1);
this.children.add(children2);
this.children.add(children3);
}
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;
}
}
}