Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. package com.javarush.test.level06.lesson11.bonus02;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6.  
  7. /* Нужно добавить в программу новую функциональность
  8. Задача: У каждой кошки есть имя и кошка-мама. Создать класс, который бы описывал данную ситуацию. Создать два объекта: кошку-дочь и кошку-маму. Вывести их на экран.
  9. Новая задача: У каждой кошки есть имя, кошка-папа и кошка-мама. Изменить класс Cat так, чтобы он мог описать данную ситуацию.
  10. Создать 6 объектов: дедушку(папин папа), бабушку(мамина мама), папу, маму, сына, дочь.
  11. Вывести их всех на экран в порядке: дедушка, бабушка, папа, мама, сын, дочь.
  12.  
  13. Пример ввода:
  14. дедушка Вася
  15. бабушка Мурка
  16. папа Котофей
  17. мама Василиса
  18. сын Мурчик
  19. дочь Пушинка
  20.  
  21. Пример вывода:
  22. Cat name is дедушка Вася, no mother, no father
  23. Cat name is бабушка Мурка, no mother, no father
  24. Cat name is папа Котофей, no mother, father is дедушка Вася
  25. Cat name is мама Василиса, mother is бабушка Мурка, no father
  26. Cat name is сын Мурчик, mother is мама Василиса, father is папа Котофей
  27. Cat name is дочь Пушинка, mother is мама Василиса, father is папа Котофей
  28. */
  29.  
  30. public class Solution
  31. {
  32. public static void main(String[] args) throws IOException
  33. {
  34. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  35.  
  36. String grandfatherName = reader.readLine();
  37. Cat catGrandpa = new Cat(grandfatherName, null, null);
  38.  
  39. String grandmotherName = reader.readLine();
  40. Cat catGrandma = new Cat(grandmotherName, null, null);
  41.  
  42. String fatherName = reader.readLine();
  43. Cat catFather = new Cat(fatherName,null,catGrandpa);
  44.  
  45. String motherName = reader.readLine();
  46. Cat catMother = new Cat(motherName, catGrandma,null);
  47.  
  48. String sonName = reader.readLine();
  49. Cat catSon = new Cat(sonName, catMother,catFather);
  50.  
  51. String daughterName = reader.readLine();
  52. Cat catDaughter = new Cat(daughterName,catMother,catFather);
  53.  
  54. System.out.println(catGrandpa);
  55. System.out.println(catGrandma);
  56. System.out.println(catFather);
  57. System.out.println(catMother);
  58. System.out.println(catSon);
  59. System.out.println(catDaughter);
  60. }
  61. public static class Cat
  62. {
  63. private String name;
  64. private Cat mother;
  65. private Cat father;
  66.  
  67. Cat(String name, Cat mother, Cat father) {
  68. this.name = name;
  69. this.mother = mother;
  70. this.father = father;
  71.  
  72. }
  73. @Override
  74. public String toString()
  75. {
  76. if (father == null && mother == null) {
  77. return "Cat name is " + name + ", no mother, no father"; }
  78. else if (mother == null && father != null) {
  79. return "Cat name is " + name + ", no mother, father is " + father.name; }
  80. else if (mother != null && father == null) {
  81. return "Cat name is " + name + ", mother is " + mother.name + ", no father"; }
  82. else {
  83. return "Cat name is " + name + ", mother is " + mother.name + ", father is " + father.name; }
  84. }
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement