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)
- {
- //Написать тут ваш код
- Human grandFather1 = new Human ("Mefodiy",true,99);
- Human grandFather2 = new Human ("Kirilo",true,98);
- Human grandMother1 = new Human ("Nastiya",false,89);
- Human grandMother2 = new Human ("Ustiya",false,88);
- Human Father = new Human ("Taras", true,50);
- Human Mother = new Human ("Bulka",true,40);
- Human kid1= new Human ("Ostap",true,20);
- Human kid2= new Human ("Petro",true,25);
- Human kid3= new Human ("MIron",true,29);
- ArrayList<Human> childrens = new ArrayList<Human>();
- childrens.add(kid1);
- childrens.add(kid2);
- childrens.add(kid3);
- 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(childrens.toString());
- }
- public static class Human
- {
- //Написать тут ваш код
- String name;
- boolean sex;
- int age;
- ArrayList<Human> children = new ArrayList<Human>();
- Human(String name,boolean sex,int age/*,ArrayList<Human>children*/){
- this.name=name;
- this.sex=sex;
- this.age=age;
- }
- 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