Advertisement
jaVer404

level08.lesson11.home06

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