public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("введите имя бабушки");
String grandmaName = reader.readLine();
Cat grandma = new Cat(grandmaName, null, null);
System.out.println("введите имя дедушки");
String grandpaName = reader.readLine();
Cat grandpa = new Cat(grandpaName, null, null);
System.out.println("введите имя отца");
String fatherName = reader.readLine();
Cat father = new Cat(fatherName, null, grandpa);
System.out.println("введите имя мамы");
String motherName = reader.readLine();
Cat catMother = new Cat(motherName, grandma, null);
System.out.println("введите имя дочери");
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catMother, father);
System.out.println("введите имя сына");
String sonName = reader.readLine();
Cat son = new Cat(sonName, catMother, father);
System.out.println(grandma);
System.out.println(grandpa);
System.out.println(father);
System.out.println(catMother);
System.out.println(catDaughter);
System.out.println(son);
}
public static class Cat {
private String name;
private Cat firstParent;
private Cat secondParent;
Cat(String name) {
this.name = name;
}
Cat(String name, Cat firstParent, Cat secondParent) {
this.name = name;
this.firstParent = firstParent;
this.secondParent = secondParent;
}
@Override
public String toString() {
if (firstParent == null & secondParent == null & name!=null)
return "The cat's name is " + name + ", no mother, no father";
else if (firstParent == null & name!=null & secondParent!=null)
return "The cat's name is " + name + ", no mother, father is " + secondParent;
else if (secondParent == null & firstParent!=null & name!= null)
return "The cat's name is " + name + ", mother is " + firstParent + ", no father";
else if (secondParent!= null & firstParent!= null & name!= null)
return "The cat's name is " + name + ", mother is " + firstParent + ", father is " + secondParent;
else return toString();
}
}
}
Сергей
7 уровень
вывод неверный, хотя условия вроде правильно расставил
Решен
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Сергей
22 марта 2021, 07:29
The cat's name is sfgsg, no mother, no father
The cat's name is fgdsfg, no mother, no father
The cat's name is dfgd, no mother, father is The cat's name is fgdsfg, no mother, no father
The cat's name is gdfgdg, mother is The cat's name is sfgsg, no mother, no father, no father
The cat's name is dfgfd, mother is The cat's name is gdfgdg, mother is The cat's name is sfgsg, no mother, no father, no father, father is The cat's name is dfgd, no mother, father is The cat's name is fgdsfg, no mother, no father
The cat's name is gdfgd, mother is The cat's name is gdfgdg, mother is The cat's name is sfgsg, no mother, no father, no father, father is The cat's name is dfgd, no mother, father is The cat's name is fgdsfg, no mother, no father
вот такой вывод
0
Алексей Тонких Backend Developer в Сбербанк
22 марта 2021, 07:35полезный
У вас должны выводиться имена кошек. Когда вы указываете переменную кошки в строке, то для неё вызывается переопределенный toString() - и для каждой кошки выполняется ваш переопределенный метод - поэтому вывод и некорректен.
+1
Сергей
22 марта 2021, 07:47
благодарю
0