Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 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 grandPaName = reader.readLine();
  37. Cat catGrandPa = new Cat(grandPaName);
  38.  
  39. String grandMaName = reader.readLine();
  40. Cat catGrandMa = new Cat(grandMaName);
  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.  
  62. public static class Cat
  63. {
  64. private String name;
  65. private Cat parentFather;
  66. private Cat parentMother;
  67.  
  68. Cat(String name)
  69. {
  70. this.name = name;
  71. }
  72.  
  73. Cat(String name, Cat parentMother, Cat parentFather)
  74. {
  75. this.name = name;
  76. this.parentMother = parentMother;
  77. this.parentFather = parentFather;
  78. }
  79.  
  80. @Override
  81. public String toString()
  82. {
  83. if (parentMother == null && parentFather == null)
  84. return "Cat name is " + name + ", no mother, no father";
  85. else if (parentFather == null)
  86. return "Cat name is " + name + ", mother is " + parentMother.name + ", no father";
  87. else if (parentMother == null)
  88. return "Cat name is " + name + ", no mother" + ", " + "father is " + parentFather.name;
  89. else return "Cat name is " + name + ", mother is " + parentMother.name + ", father is " + parentFather.name;
  90. }
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement