Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1. package task08;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. /**
  6.  * Собираем семейство
  7.  1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
  8.  2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
  9.  3. Вывести все объекты Human на экран.
  10.  
  11.  
  12.  Требования:
  13.  1. Программа должна выводить текст на экран.
  14.  2. Класс Human должен содержать четыре поля.
  15.  3. Класс Human должен содержать один метод.
  16.  4. Класс Solution должен содержать один метод.
  17.  5. Программа должна создавать объекты и заполнять их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
  18.  */
  19. public class task0824 {
  20.     public static void main(String[] args) {
  21.         //напишите тут ваш код
  22.         Human grandFather1 = new Human("grandFather1", true, 80, new ArrayList<>());
  23.         Human grandFather2 = new Human("grandFather2", true, 82, new ArrayList<>());
  24.         Human grandMother1 = new Human("grandMother1", false, 70, new ArrayList<>());
  25.         Human grandMother2 = new Human("grandMother2", false, 74, new ArrayList<>());
  26.  
  27.         ArrayList<Human> fatherParants = new ArrayList<>();
  28.         fatherParants.add(grandFather1);
  29.         fatherParants.add(grandMother1);
  30.  
  31.         Human father = new Human("father", true, 36, fatherParants);
  32.  
  33.         ArrayList<Human> motherParants = new ArrayList<>();
  34.         motherParants.add(grandFather2);
  35.         motherParants.add(grandMother2);
  36.  
  37.         Human mother = new Human("mother", false, 29, motherParants);
  38.  
  39.         ArrayList<Human> parants = new ArrayList<>();
  40.         parants.add(father);
  41.         parants.add(mother);
  42.  
  43.         ArrayList<Human> children = new ArrayList<>();
  44.         Human child1 = new Human("child1", true, 10,children);
  45.         Human child2 = new Human("child2", false, 12,children);
  46.         Human child3 = new Human("child3", false, 23,children);
  47.  
  48.         System.out.println(grandFather1);
  49.         System.out.println(grandFather2);
  50.         System.out.println(grandMother1);
  51.         System.out.println(grandMother2);
  52.         System.out.println(father);
  53.         System.out.println(mother);
  54.         System.out.println(child1);
  55.         System.out.println(child2);
  56.         System.out.println(child3);
  57.     }
  58.  
  59.     public static class Human {
  60.         //напишите тут ваш код
  61.         String name;
  62.         boolean sex;
  63.         int age;
  64.         ArrayList<Human> children;
  65.  
  66.         public Human(String name, boolean sex, int age, ArrayList<Human> children) {
  67.             this.name = name;
  68.             this.sex = sex;
  69.             this.age = age;
  70.             this.children = children;
  71.         }
  72.         public String toString() {
  73.             String text = "";
  74.             text += "Имя: " + this.name;
  75.             text += ", пол: " + (this.sex ? "мужской" : "женский");
  76.             text += ", возраст: " + this.age;
  77.  
  78.             int childCount = this.children.size();
  79.             if (childCount > 0) {
  80.                 text += ", дети: " + this.children.get(0).name;
  81.  
  82.                 for (int i = 1; i < childCount; i++) {
  83.                     Human child = this.children.get(i);
  84.                     text += ", " + child.name;
  85.                 }
  86.             }
  87.             return text;
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement