Что-то совсем запутался.
package com.javarush.task.task08.task0824;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
/* 1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
3. Вывести все объекты Human на экран.
Требования:
1. Программа должна выводить текст на экран.
2. Класс Human должен содержать четыре поля.
3. Класс Human должен содержать один метод.
4. Класс Solution должен содержать один метод.
5. Программа должна создавать объекты и заполнять их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей и выводить все объекты Human на экран.*/
public class Solution {
public static void main(String[] args) {
Human son1 = new Human("Олег", true, 21);
Human son2 = new Human("Антон", true, 19);
Human doch = new Human("Лиза", false, 17);
ArrayList<Human> child1 = new ArrayList<Human>();
child1.add(son1);
child1.add(son2);
child1.add(doch);
Human papa = new Human("Алексей", true, 50,child1);
Human mama = new Human("Антонина", false, 45,child1);
ArrayList<Human> child2 = new ArrayList<Human>();
child2.add(papa);
ArrayList<Human> child3 = new ArrayList<Human>();
child3.add(mama);
Human opa1 = new Human("Евлампий", true, 76, child2);
Human oma1 = new Human("Агафья", false, 74,child2);
Human opa2 = new Human("Евстафий", true, 73,child3);
Human oma2 = new Human("Мойра", false, 70,child3);
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;
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;
}
}
}