Ввод:
дідо
баба
тато
мама
син
дочка
вивод:
Cat name is дід, no mother, no father
Cat name is баба, no mother, no father
Cat name is тато, no mother, father is дід
Cat name is мама, mother is баба, no father
Cat name is син, mother is мама, father is тато
Cat name is дочка, mother is мама, father is тато
Process finished with exit code 0
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/* Нужно добавить в программу новую функциональность
Задача: У каждой кошки есть имя и кошка-мама. Создать класс, который бы описывал данную ситуацию. Создать два объекта: кошку-дочь и кошку-маму. Вывести их на экран.
Новая задача: У каждой кошки есть имя, кошка-папа и кошка-мама. Изменить класс Cat так, чтобы он мог описать данную ситуацию.
Создать 6 объектов: дедушку(папин папа), бабушку(мамина мама), папу, маму, сына, дочь.
Вывести их всех на экран в порядке: дедушка, бабушка, папа, мама, сын, дочь.
Пример ввода:
дедушка Вася
бабушка Мурка
папа Котофей
мама Василиса
сын Мурчик
дочь Пушинка
Пример вывода:
Cat name is дедушка Вася, no mother, no father
Cat name is бабушка Мурка, no mother, no father
Cat name is папа Котофей, no mother, father is дедушка Вася
Cat name is мама Василиса, mother is бабушка Мурка, no father
Cat name is сын Мурчик, mother is мама Василиса, father is папа Котофей
Cat name is дочь Пушинка, mother is мама Василиса, father is папа Котофей
*/
public class Solution
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String GrandFatherName = reader.readLine();
Cat GrandcatFather = new Cat(GrandFatherName,null,null);
String GrandmotherName = reader.readLine();
Cat GrandCatMother = new Cat(GrandmotherName,null,null);
String FatherName = reader.readLine();
Cat catFather = new Cat(FatherName,GrandcatFather,null);
String motherName = reader.readLine();
Cat catMother = new Cat(motherName,null,GrandCatMother);
String SonName = reader.readLine();
Cat catSon = new Cat(SonName, catFather,catMother);
String daughterName = reader.readLine();
Cat catDaughter = new Cat(daughterName, catFather,catMother);
System.out.println(GrandcatFather);
System.out.println(GrandCatMother);
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,Cat father,Cat mother)
{
this.mother=mother;
this.father=father;
this.name = name;
}
@Override
public String toString()
{
String name;
if (mother == null && father==null) {
return "Cat name is " + this.name + ", no mother," + " no father";
}
if(mother ==null&&father!=null) {
return "Cat name is " + this.name + ", no mother," + " father is " + father.name;
}
if(father==null&&mother!=null) {
return "Cat name is " + this.name + ", mother is " + mother.name + ", no father";
}
return "Cat name is " + this.name + ", mother is " + mother.name + ", father is "+father.name;
}
}
}
ПЕРЕЙДИТЕ В ПОЛНУЮ ВЕРСИЮ
GrandFatherName
GrandFather
GrandmotherName
Grandmother
FatherName
SonName
и да, если метода toString() правили — возвращайте все как было, его вообще трогать не надо было.
и зачем было вводить в toString строку String name;