задача уже решена, поэтому прикреплю сокращенно. может вопрос глупый, но почему в команде System.out.println(grandf); вызывается метод toString? разве не нужно казывать System.out.println(grandf.toString); или как-то так? почему это вообще работает? спасибо))
и еще.. не легче ли использовать arraylist в решении?
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String grandfN = reader.readLine();
Cat grandf = new Cat(grandfN);
System.out.println(grandf);
}
public static class Cat {
private String name;
private Cat mother, father;
Cat(String name) {
this.name = name;
}
@Override
public String toString() {
if (father == null && mother == null)
return "The cat's name is " + name + ", no mother, no father ";
else if (mother == null)
return "The cat's name is " + name + ", no mother, father is " + father.name;
else if (father == null)
return "The cat's name is " + name + ", mother is " + mother.name + ", no father ";
else
return "The cat's name is " + name + ", mother is " + mother.name + ", father is " + father.name;
}
}
}