ya_zay4eg

level08.lesson11.home06

Jan 8th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 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.         Human grandFather1 = new Human ("Mefodiy",true,99);
  17.         Human grandFather2 = new Human ("Kirilo",true,98);
  18.         Human grandMother1 = new Human ("Nastiya",false,89);
  19.         Human grandMother2 = new Human ("Ustiya",false,88);
  20.         Human Father = new Human ("Taras", true,50);
  21.         Human Mother = new Human ("Bulka",true,40);
  22.         Human kid1= new Human ("Ostap",true,20);
  23.         Human kid2= new Human ("Petro",true,25);
  24.         Human kid3= new Human ("MIron",true,29);
  25.         ArrayList<Human> childrens = new ArrayList<Human>();
  26.         childrens.add(kid1);
  27.         childrens.add(kid2);
  28.         childrens.add(kid3);
  29.         System.out.println(grandFather1.toString());
  30.         System.out.println(grandFather2.toString());
  31.         System.out.println(grandMother1.toString());
  32.         System.out.println(grandMother2.toString());
  33.         System.out.println(Father.toString());
  34.         System.out.println(Mother.toString());
  35.         System.out.println(childrens.toString());
  36.     }
  37.  
  38.     public static class Human
  39.     {
  40.         //Написать тут ваш код
  41.         String name;
  42.         boolean sex;
  43.         int age;
  44.         ArrayList<Human> children = new ArrayList<Human>();
  45.  
  46.         Human(String name,boolean sex,int age/*,ArrayList<Human>children*/){
  47.             this.name=name;
  48.             this.sex=sex;
  49.             this.age=age;
  50.         }
  51.  
  52.         public String toString()
  53.         {
  54.             String text = "";
  55.             text += "Имя: " + this.name;
  56.             text += ", пол: " + (this.sex ? "мужской" : "женский");
  57.             text += ", возраст: " + this.age;
  58.  
  59.             int childCount = this.children.size();
  60.             if (childCount > 0)
  61.             {
  62.                 text += ", дети: "+this.children.get(0).name;
  63.  
  64.                 for (int i = 1; i < childCount; i++)
  65.                 {
  66.                     Human child = this.children.get(i);
  67.                     text += ", "+child.name;
  68.                 }
  69.             }
  70.  
  71.             return text;
  72.         }
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment