0
public class Main {

  public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

    String oldCatFather = reader.readLine();
    Cat catOldFather = new Cat(oldCatFather);

    String oldCatMather = reader.readLine();
    Cat catOldMather = new Cat(oldCatMather);

    String father = reader.readLine();
    Cat catFather = new Cat(father,catOldFather,null);

    String mather = reader.readLine();
    Cat catMather = new Cat(mather,null,catOldMather);

    String son = reader.readLine();
    Cat catSon = new Cat(son,catMather,catFather);

    String daughter = reader.readLine();
    Cat catDaughter = new Cat(daughter,catMather,catFather);

    System.out.println(catOldFather);
    System.out.println(catOldMather);
    System.out.println(catFather);
    System.out.println(catMather);
    System.out.println(catSon);
    System.out.println(catDaughter);
  }

  public static class Cat {
    private String name;
    private Cat father ;
    private Cat mather;

    Cat(String name) {
        this.name = name;
    }

    Cat(String name, Cat father ,Cat mather) {
        this.name = name;
        this.father = father;
        this.mather = mather;
    }

    @Override
    public String toString() {
        if (name ==  null && name == null){
            return "The cat's name is " + name + ", no mother, no father";

        }
        else if (father == null){
            return  "The cat's name is " + name + ", no mother,  father is" + father.name + "";

        }
        else if (mather == null){
            return  "The cat's name is " + name + ",  mother is "+ mather.name + ", no father";

        }
        else  {
            return  "The cat's name is " + name + ",  mother is "+ mather.name + ",  father is" + father.name + "";
        }
    }
  }
}

НЕ ПОНИМАЮ КАК ИСПРАВИТЬ СПАСИБО ЗА ПОНИМАНИЕ

1 ответ 1

1
@Override
public String toString() {
  return "The cat's name is " + name + ", " + 
    (mather == null? "no mother" : ("mother is " + mather.name)) + ", " +
    (father == null? "no father" : ("father is " + father.name));
}

Неправильный порядок параметров:

Cat catSon = new Cat(son,catMather,catFather);
...
Cat catDaughter = new Cat(daughter,catMather,catFather);

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.