Advertisement
Alex_Zuev

Untitled

Apr 17th, 2015
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.10 KB | None | 0 0
  1. Enter your code herepackage 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.         System.out.println("Введите имя котЭ бабка");
  37.         String grannyName = "granny " + reader.readLine();
  38.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  39.         String grannySex = reader.readLine();
  40.         Cat catGranny = new Cat(grannyName, grannySex);
  41.  
  42.         System.out.println("Введите имя котЭ дедка");
  43.         String grandpaName = "grandpa " + reader.readLine();
  44.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  45.         String grandpaSex = reader.readLine();
  46.         Cat catGrandpa = new Cat(grandpaName, grandpaSex);
  47.  
  48.         System.out.println("Введите имя котЭ мамка");
  49.         String moterName = "momy " + reader.readLine();
  50.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  51.         String motherSex = reader.readLine();
  52.         Cat catMother = new Cat(moterName, catGranny, motherSex);
  53.  
  54.         System.out.println("Введите имя котЭ папка");
  55.         String fatherName = "dady " + reader.readLine();
  56.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  57.         String fatherSex = reader.readLine();
  58.         Cat catFather = new Cat(fatherName, catGrandpa, fatherSex);
  59.  
  60.  
  61.         System.out.println("Введите имя котЭ доця");
  62.         String daughterName = "daughter " + reader.readLine();
  63.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  64.         String daughterSex = reader.readLine();
  65.         Cat catDaughter = new Cat(daughterName, catMother, catFather, daughterSex);
  66.  
  67.         System.out.println("Введите имя котЭ сына");
  68.         String sonName = "son " + reader.readLine();
  69.         System.out.println("Введите пол котЭ если женский введи \"f\" если мужской введи \"m\"");
  70.         String sonSex = reader.readLine();
  71.         Cat catSon = new Cat(sonName, catMother, catFather, sonSex);
  72.  
  73.         System.out.println(catGrandpa);
  74.         System.out.println(catGranny);
  75.         System.out.println(catFather);
  76.         System.out.println(catMother);
  77.         System.out.println(catSon);
  78.         System.out.println(catDaughter);
  79.        
  80.  
  81.     }
  82.  
  83.     public static class Cat
  84.     {
  85.         private String name;
  86.         private Cat parentf;
  87.         private Cat parentm;
  88.         private boolean sex;
  89.  
  90.  
  91.         Cat(String name, String sex)
  92.         {
  93.  
  94.             this.name = name;
  95.             this.sex = CheckSex(sex);
  96.         }
  97.  
  98.         Cat(String name, Cat parent, String sex)
  99.         {
  100.             this.name = name;
  101.  
  102.             this.sex = CheckSex(sex);
  103.             if (parent.sex )
  104.                 this.parentf = parent;
  105.             else
  106.                 this.parentm = parent;
  107.         }
  108.  
  109.         Cat(String name, Cat parent1, Cat parent2, String sex)
  110.         {
  111.             this.name = name;
  112.  
  113.             this.sex = CheckSex(sex);
  114.             if (parent1.sex )
  115.             {
  116.  
  117.                 this.parentm = parent2;
  118.                 this.parentf = parent1;
  119.             } else
  120.             {
  121.                 this.parentm = parent1;
  122.                 this.parentf = parent2;
  123.             }
  124.  
  125.  
  126.         }
  127.  
  128.         public boolean CheckSex(String sex)
  129.         {
  130.             return  (sex.equals("f") )
  131.                 ;
  132.  
  133.         }
  134.  
  135.         @Override
  136.         public String toString()
  137.         {
  138.             if (parentf == null && parentm == null)
  139.                 return "Cat name is " + name + ", no mother " + " , no father ";
  140.             else if (parentf != null && parentm == null)
  141.                 return "Cat name is " + name + ", mother is " + parentf.name + " , no father ";
  142.             else if (parentm != null && parentf == null)
  143.                 return "Cat name is " + name + ", no mother  " + ",  father is " + parentm.name;
  144.             else
  145.                 return "Cat name is " + name + ", mother is " + parentf.name + ",  father is " + parentm.name;
  146.  
  147.         }
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement