Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level06.lesson11.bonus02;
- 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 catgrandFather = new Cat(grandFatherName, null, null);
- String grandMotherName = reader.readLine();
- Cat catgrandMother = new Cat(grandMotherName, null, null);
- String fatherName = reader.readLine();
- Cat justFather = new Cat(fatherName, null, catgrandFather);
- String motherName = reader.readLine();
- Cat justMother = new Cat(motherName, catgrandMother, null);
- String sonName = reader.readLine();
- Cat son = new Cat(sonName, justMother, justFather);
- String doughterName = reader.readLine();
- Cat doughter = new Cat(doughterName, justMother, justFather);
- System.out.println(catgrandFather);
- System.out.println(catgrandMother);
- System.out.println(justFather);
- System.out.println(justMother);
- System.out.println(son);
- System.out.println(doughter);
- }
- public static class Cat
- {
- private String name;
- private Cat parentFather;
- private Cat parentMother;
- Cat(String name, Cat parentMother, Cat parentFather)
- {
- this.name = name;
- this.parentMother = parentMother;
- this.parentFather = parentFather;
- }
- @Override
- public String toString()
- {
- if (parentFather == null&&parentMother==null)
- return "Cat name is " + name + ", no mother, " + "no father";
- else if (parentMother==null&&parentFather==this.parentFather) {
- return "Cat name is " + name + ", no mother, " +"father is " + parentFather.name;
- }
- else if (parentMother==this.parentMother&&parentFather==null) {
- return "Cat name is " + name + ", mother is " + parentMother.name + ", no father";
- }
- else
- return "Cat name is " + name + ", mother is " + parentMother.name +", father is " + parentFather.name;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement