Не могу понять в чем проблема. Порядок ввода-вывода вроде ок.
package com.javarush.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Родственные связи кошек
*/
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
Cat grandfather = new Cat(reader.readLine());
Cat grandmother = new Cat(reader.readLine());
Cat father = new Cat (reader.readLine(), null, grandfather);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandmother, null);
Cat son = new Cat(reader.readLine(), catMother, father);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, father);
System.out.println(grandfather);
System.out.println(grandmother);
System.out.println(father);
System.out.println(catMother);
System.out.println(son);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private Cat mother;
private Cat father;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat mother, Cat father) {
this.name = name;
this.mother = mother;
this.father = father;
}
@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 is " + name + ", no mother, " + "father is дедушка " + father.name;
else if (mother != null && father != null)
return "The cat's name is " + name + ", mother is мама " + mother.name + ", father is папа " + father.name;
else /* if (mother != null && father == null) */
return "The cat's name is " + name + ", mother is бабушка " + mother.name + ", no father";
}
}
}