Эх, не могу понять. Да уж, а на этой задаче много народу застряло. Хорошая задача, нраица.
Даже жаль что пришлось создать тред, но самостоятельно разобраться не могу.
Ну, про 5 и 6 строку видно ниже, не принимает. Но при запуске получается так:
Cat name is 1, no mother , no father
Cat name is 2, no mother , no father
Cat name is 3, no mother, father is 1
Cat name is 4, mother is 2, no father
Cat name is 5, mother is 3, father is 4
Cat name is 6, mother is 3, father is 4
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));
String grandfatherName = reader.readLine();
Cat catGrandFather = new Cat(grandfatherName);
String grandmotherName = reader.readLine();
Cat catGrandMother = new Cat (grandmotherName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, catGrandFather, null);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, null, catGrandMother);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, catMother, catFather);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, catFather);
System.out.println(catGrandFather);
System.out.println(catGrandMother);
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) {
this.name = name;
this.father = father;
}
Cat(Cat mother, String name) {
this.name = name;
this.mother = mother;
}*/
Cat(String name, Cat father, Cat mother) {
this.name = name;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
if (father == null & mother == null)
return "Cat name is " + name + ", no mother " + ", no father";
if (father == null)
return "Cat name is " + name + ", mother is " + mother.name + ", no father";
if (mother == null)
return "Cat name is " + name + ", no mother" + ", father is " + father.name;
else return "Cat name is " + name + ", mother is " + mother.name + ", father is " + father.name;
}
}
}