помогите дедушку с бабушкой различить. Я так понимаю что дело не в конструкторе. Желательно объяснить словами. Благодарю Вас! Напишите, что лишнее, что можно оптимизировать.Полям класса Кат присвоил тип Стринг.
Строчку где дедушка должен быть дедушкой программа как будто не видит.
Подскажите, а как можно сделать уточнение в сравнении перегруженного метода toString, и казать типа, что если еще поле name объекта catFather называется "Котофей", тогда и запустить эту строчку которую не видит программа: {
return "The cat's name is " + name + ", no mother" + ", father is " + catFather;}
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 catGrandPa = new Cat(grandPaName);
String grandMaName = reader.readLine();
Cat catGrandMa = new Cat(grandMaName);
String fatherName = reader.readLine();
Cat catFather = new Cat(fatherName, grandPaName);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandMaName);
String sonName = reader.readLine();
Cat catSon = new Cat(sonName, motherName, fatherName);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, motherName, fatherName);
System.out.println(catGrandPa);
System.out.println(catGrandMa);
System.out.println(catFather);
System.out.println(catMother);
System.out.println(catSon);
System.out.println(catDaughter);
}
public static class Cat {
private String name;
private String catMother;
private String catFather;
Cat() {
}
Cat(String name) {
this.name = name;
}
Cat(String name, String catMother) {
this.name = name;
this.catMother = catMother;
}
Cat(String name, String catMother, String catFather) {
this.name = name;
this.catMother = catMother;
this.catFather = catFather;
}
@Override
public String toString() {
if (catMother == null & catFather == null){
return "The cat's name is " + this.name + ", no mother" + ", no father ";}
if (catFather == null & catMother != null) {
return "The cat's name is " + name + ", mother is " + catMother + ", no father ";}
if (catMother == null & catFather != null ){
return "The cat's name is " + name + ", no mother" + ", father is " + catFather;}
else
{return "The cat's name is " + name + ", mother is " + catMother + ", father is " + catFather;}
}
}
}