Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level08.lesson11.home06;
- /* Вся семья в сборе
- 1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
- 2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
- 3. Вывести все объекты Human на экран.
- */
- import java.util.ArrayList;
- public class Solution
- {
- public static void main(String[] args)
- {
- //Написать тут ваш код
- ArrayList<Human> children = new ArrayList<Human>();
- ArrayList<Human> children5 = new ArrayList<Human>();
- Human child1 = new Human("Roma", true, 30, children5);
- Human child2 = new Human("Vita", false, 29, children5);
- Human child3 = new Human("Sasha", true, 30, children5);
- children.add(child1);
- children.add(child2);
- children.add(child3);
- Human mother = new Human("Val'a", false, 40, children);
- Human father = new Human("Sasha", true, 55, children);
- ArrayList<Human> children2 = new ArrayList<Human>();
- children2.add(father);
- ArrayList<Human> children3 = new ArrayList<Human>();
- children3.add(mother);
- ArrayList<Human> children4 = null;
- Human grandFather1 = new Human("Anton", true, 66, children2);
- Human grandFather2 = new Human("Vitaliy", true, 76, children3);
- Human grandMother1 = new Human("Lida", false, 70, children2);
- Human grandMother2 = new Human("Val'a", false, 75, children3);
- ArrayList<Human> children1 = new ArrayList<Human>();
- children1.add(child1);
- children1.add(child2);
- children1.add(child3);
- System.out.println(grandFather1.toString());
- System.out.println(grandFather2.toString());
- System.out.println(grandMother1.toString());
- System.out.println(grandMother2.toString());
- System.out.println(father.toString());
- System.out.println(mother.toString());
- System.out.println(child1.toString());
- System.out.println(child2.toString());
- System.out.println(child3.toString());
- }
- public static class Human
- {
- //Написать тут ваш код
- String name;
- boolean sex;
- int age;
- ArrayList<Human> children;
- Human (String name, boolean sex, int age, ArrayList<Human> children) {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.children = children;
- }
- public String toString()
- {
- String text = "";
- text += "Имя: " + this.name;
- text += ", пол: " + (this.sex ? "мужской" : "женский");
- text += ", возраст: " + this.age;
- int childCount = this.children.size();
- if (childCount > 0)
- {
- text += ", дети: "+this.children.get(0).name;
- for (int i = 1; i < childCount; i++)
- {
- Human child = this.children.get(i);
- text += ", "+child.name;
- }
- }
- return text;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement