Advertisement
jaVer404

level07.lesson12.home06

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