ΠΡΠ»ΠΈ ΡΠΎΠ·Π΄Π°ΠΌ ΠΊΠΎΠ½ΡΡΡΡΠΊΡΠΎΡ ΠΎΡΠ΄Π΅Π»ΡΠ½ΠΎ Ρ ΠΈΠΌΠ΅Π½Π΅ΠΌ ΠΈ ΠΏΠ°ΠΏΠΎΠΉ ΠΈΠ»ΠΈ Ρ ΠΈΠΌΠ΅Π½Π΅ΠΌ ΠΈ ΠΌΠ°ΠΌΠΎΠΉ, ΠΏΠΎΠ»ΡΡΠ°Π΅ΡΡΡ, ΡΡΠΎ ΡΡΠ½ ΠΈ Π΄ΠΎΡΡ Ρ ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΠΌΠΈ null. ΠΠ°ΠΊ ΡΡΠΎ ΠΈΡΠΏΡΠ°Π²ΠΈΡΡ?
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 grandpaName = reader.readLine();
Cat grandpa = new Cat(grandpaName);
String grandmaName = reader.readLine();
Cat grandma = new Cat(grandmaName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, grandpa);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandma);
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(grandpa);
System.out.println(grandma);
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 mother, Cat father) {
this.name = name;
this.father = father;
this.mother = mother;
}
@Override
public String toString() {
if (father == null && mother == null)
return "The cat's name is " + name + ", no mother, no father ";
else if (father == null)
return "The cat's name is " + name + ","+mother.name + ", no father";
else if (mother == null)
return "The cat's name is " + name + ", no mother " + father.name;
else
return "The cat's name is " + name + ", mother is " + mother.name + ", father is " + father.name;
}
}
}