1. What's @Override ?
2. What's my code wrong 😞?
package com.codegym.task.task06.task0621;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Cat relations
*/
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,null ,null);
String grandmotherName = reader.readLine();
Cat catGrandMother = new Cat(grandmotherName,null ,null);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName,null ,grandfatherName);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandmotherName,null);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName,fatherName,motherCat);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName,fatherName,motherName);
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, 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 (father == null)
return "The cat's name is " + name + ", no father ";
else if (mother == null)
return "The cat's name is " + name + ", no mother ";
else
return "The cat's name is " + name + ", " + father.name + " is the father" + ", & " + mother.name + " is the mother" ;
}
}
}