Не вижу ошибки, глаза замылились
package com.javarush.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
У каждой кошки есть имя, кот-папа и кошка-мама.
Изменить класс Cat так, чтобы он мог описать данную ситуацию.
Создать 6 объектов: дедушку (папин папа), бабушку (мамина мама), папу, маму, сына, дочь.
Вывести их всех на экран в порядке: дедушка, бабушка, папа, мама, сын, дочь.
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandPaName = reader.readLine();
Cat catGrandPa = new Cat(grandPaName);
String grandMaName = reader.readLine();
Cat catGrandMa = new Cat(grandMaName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, catGrandPa, null);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, null, catGrandMa);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catFather,catMother);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catFather, catMother);
System.out.println(catGrandPa); //выводит поочередно семейство кошек
System.out.println(catGrandMa);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat father;
private Cat mother;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat father, Cat mother) {
this.name = name;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
if (mother == null && father == null)
{
return "The cat's name is " + name + ", no mother, no father";
}
else if (mother == null && father != null)
{
return "The cat's name " + name + ", no mother, father is " + father.name;
}
else if (mother != null && father == null)
{
return "The cat's name " + name + ", mother is " + mother.name + ", no father";
}
else
{
return "The cat's name " + name + ", mother is " + mother.name + ", father is " + father.name;
}
}
}
}