Вывод:
Имя: name0, пол: мужской, возраст: 10
Имя: name1, пол: женский, возраст: 11
Имя: name2, пол: мужской, возраст: 12
Имя: name3, пол: женский, возраст: 13
Имя: name4, пол: женский, возраст: 14, отец: a4, мать: M4
Имя: name5, пол: мужской, возраст: 15, отец: a5, мать: M5
Имя: name6, пол: мужской, возраст: 16, отец: a6, мать: M6
Имя: name7, пол: мужской, возраст: 17, отец: a7, мать: M7
Имя: name8, пол: женский, возраст: 18, отец: a8, мать: M8
package com.javarush.task.task07.task0724;
/*
Семейная перепись
*/
import java.util.Arrays;
public class Solution {
public static void main(String[] args) {
Human a[] = new Human[9];
for (int i = 0; i < 4; i++) {
a[i] = new Human("name"+i, (Math.random() < 0.5), 10+i);
}
for (int i = 4; i < a.length; i++) {
a[i] = new Human("name"+i, (Math.random() < 0.5), 10+i, new Human("a"+i, false, 30+i), new Human("M"+i, true, 35+i));
}
Arrays.stream(a).flatMap(x->Arrays.stream(new Human[]{x})).forEach(System.out::println);
//Arrays.stream(a).forEach(System.out::print);
// System.out.println(Arrays.toString(a));//напишите тут ваш код
}
public static class Human {
String name;
boolean sex;
int age;
Human father;
Human mother;
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 father, Human mother) {
this.name = name;
this.sex = sex;
this.age = age;
this.father = father;
this.mother = mother;
}
//напишите тут ваш код
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
if (this.father != null)
text += ", отец: " + this.father.name;
if (this.mother != null)
text += ", мать: " + this.mother.name;
return text;
}
}
}