Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package task08;
- import java.util.ArrayList;
- /**
- * Собираем семейство
- 1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
- 2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
- 3. Вывести все объекты Human на экран.
- Требования:
- 1. Программа должна выводить текст на экран.
- 2. Класс Human должен содержать четыре поля.
- 3. Класс Human должен содержать один метод.
- 4. Класс Solution должен содержать один метод.
- 5. Программа должна создавать объекты и заполнять их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
- */
- public class task0824 {
- public static void main(String[] args) {
- //напишите тут ваш код
- Human grandFather1 = new Human("grandFather1", true, 80, new ArrayList<>());
- Human grandFather2 = new Human("grandFather2", true, 82, new ArrayList<>());
- Human grandMother1 = new Human("grandMother1", false, 70, new ArrayList<>());
- Human grandMother2 = new Human("grandMother2", false, 74, new ArrayList<>());
- ArrayList<Human> fatherParants = new ArrayList<>();
- fatherParants.add(grandFather1);
- fatherParants.add(grandMother1);
- Human father = new Human("father", true, 36, fatherParants);
- ArrayList<Human> motherParants = new ArrayList<>();
- motherParants.add(grandFather2);
- motherParants.add(grandMother2);
- Human mother = new Human("mother", false, 29, motherParants);
- ArrayList<Human> parants = new ArrayList<>();
- parants.add(father);
- parants.add(mother);
- ArrayList<Human> children = new ArrayList<>();
- Human child1 = new Human("child1", true, 10,children);
- Human child2 = new Human("child2", false, 12,children);
- Human child3 = new Human("child3", false, 23,children);
- System.out.println(grandFather1);
- System.out.println(grandFather2);
- System.out.println(grandMother1);
- System.out.println(grandMother2);
- System.out.println(father);
- System.out.println(mother);
- System.out.println(child1);
- System.out.println(child2);
- System.out.println(child3);
- }
- public static class Human {
- //напишите тут ваш код
- String name;
- boolean sex;
- int age;
- ArrayList<Human> children;
- public 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