ya_zay4eg

level07.lesson12.home06

Dec 15th, 2014
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. package com.javarush.test.level07.lesson12.home06;
  2.  
  3. /* Семья
  4. Создай класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human). Создай объекты и заполни их так, чтобы получилось:
  5. Два дедушки,
  6. две бабушки,
  7. отец,
  8. мать,
  9.  
  10. трое детей. Вывести объекты на экран.
  11. Примечание:
  12. Если написать свой метод String toString() в классе Human, то именно он будет использоваться при выводе объекта на экран.
  13. Пример вывода:
  14. Имя: Аня, пол: женский, возраст: 21, отец: Павел, мать: Катя
  15. Имя: Катя, пол: женский, возраст: 55
  16. Имя: Игорь, пол: мужской, возраст: 2, отец: Михаил, мать: Аня
  17. …
  18. */
  19.  
  20. public class Solution
  21. {
  22.     public static void main(String[] args)
  23.     {
  24.         //Написать тут ваш код
  25.         /*Создаем обьекты конструктора, c параметрами*/
  26.         Human grandFather=new Human("Дід Віталій", true,100,null,null);
  27.         Human grandFatherM=new Human("Дід Антон",true,100,null,null);
  28.         Human grandMother=new Human("Баб Валя", false,80,null,null);
  29.         Human grandMotherM=new Human("Баб Ліда",false,80,null,null);
  30.         Human father=new Human("Батько Сашко", true,60,grandFather,grandMother);
  31.         Human mother=new Human("Мати Валентина",false,50,grandFatherM,grandMotherM);
  32.         Human kid1=new Human("Рома",true,30,father,mother);
  33.         Human kid2=new Human("Саша",true,29,father,mother);
  34.         Human kid3=new Human("Владик", true,28,father,mother);
  35.         /*Через обьект класса Human добираемся до .toString()*/
  36.         System.out.println(grandFather.toString());
  37.         System.out.println(grandFatherM.toString());
  38.         System.out.println(grandMother.toString());
  39.         System.out.println(grandMotherM.toString());
  40.         System.out.println(father.toString());
  41.         System.out.println(mother.toString());
  42.         System.out.println(kid1.toString());
  43.         System.out.println(kid2.toString());
  44.         System.out.println(kid3.toString());
  45.     }
  46.  
  47.     public static class Human
  48.     {
  49.         //Написать тут ваш код
  50.         /*класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human).*/
  51.         String name;
  52.         boolean sex;
  53.         int age;
  54.         Human father;
  55.         Human mother;
  56.                             /*Конструктор Human*/
  57.         Human (String name, boolean sex, int age, Human father, Human mother) {
  58.             this.name=name;
  59.             this.sex=sex;
  60.             this.age=age;
  61.             this.father=father;
  62.             this.mother=mother;
  63.         }
  64.  
  65.         public String toString()
  66.         {
  67.             String text = "";
  68.             text += "Имя: " + this.name;
  69.             text += ", пол: " + (this.sex ? "мужской" : "женский");
  70.             text += ", возраст: " + this.age;
  71.  
  72.             if (this.father != null)
  73.                 text += ", отец: " + this.father.name;
  74.  
  75.             if (this.mother != null)
  76.                 text += ", мать: " + this.mother.name;
  77.  
  78.             return text;
  79.         }
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment