не проходит условие на создание 9 объектов 4 без родителей 5 с ними
программа работает корректно, компилирует и выдает:
Имя: Гриша, пол: мужской, возраст: 85
Имя: Люба, пол: женский, возраст: 85
Имя: Вася, пол: мужской, возраст: 85
Имя: Нина, пол: женский, возраст: 85
Имя: Владимир, пол: мужской, возраст: 60, отец: Вася, мать: Нина
Имя: Людмила, пол: женский, возраст: 60, отец: Гриша, мать: Люба
Имя: Юра, пол: мужской, возраст: 40, отец: Владимир, мать: Людмила
Имя: Марина, пол: женский, возраст: 35, отец: Владимир, мать: Людмила
Имя: Денис, пол: мужской, возраст: 30, отец: Владимир, мать: Людмила
почему не прохожу по условию?
package com.javarush.task.task07.task0724;
/*
Семейная перепись
*/
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Solution {
public static void main(String[] args) {
// напишите тут ваш код
Human grandfather1 = new Human("Гриша", true, 85, null, null);
Human grandfather2 = new Human("Вася", true, 85, null, null);
Human grandmother1 = new Human("Люба", true, 85, null, null);
Human grandmother2 = new Human("Нина", true, 85, null, null);
Human father = new Human("Владимир", true, 60, grandfather2, grandmother2);
Human mother = new Human("Людмила", true, 60, grandfather1, grandmother1);
Human brother = new Human("Юра", true, 40, father, mother);
Human sister = new Human("Марина", true, 35, father, mother);
Human me = new Human("Денис", true, 30, father, mother);
Human [] family = {grandfather1, grandmother1, grandfather2, grandmother2, father, mother, brother, sister, me};
for (Human list: family) {
System.out.println(list.toString());
}
}
public static class Human {
// напишите тут ваш код
String name;
boolean sex;
int age;
Human father, mother;
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 Human(String name, boolean sex, int age) {
this(name, sex, age, null, null);
}
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;
}
}
}