Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level07.lesson12.home06;
- /* Семья
- 1. Создай класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human).
- 2. Создай объекты и заполни их так, чтобы получилось:
- Два дедушки, две бабушки, отец, мать, трое детей.
- Вывести объекты на экран.
- Примечание:
- Если написать свой метод String toString() в классе Human,
- то именно он будет использоваться при выводе объекта на экран.
- Пример вывода:
- Имя: Аня, пол: женский, возраст: 21, отец: Павел, мать: Катя
- Имя: Катя, пол: женский, возраст: 55
- Имя: Игорь, пол: мужской, возраст: 2, отец: Михаил, мать: Аня
- …
- */
- public class Solution
- {
- public static void main(String[] args)
- {
- //Написать тут ваш код
- Human grandFather1 = new Human("Дед Борис", true, 66, null, null);
- Human grandFather2 = new Human("Дід Петро", true, 68, null, null);
- Human grandMother1 = new Human("баба Мотрона", false, 150, null, null);
- Human grandMother2 = new Human("баба Ельза", false, 166, null, null);
- Human father1 = new Human("Bobby", true, 30, grandFather1, grandMother1);
- Human mother1 = new Human("Kylie", false, 35, grandFather2, grandMother2);
- Human kid1 = new Human("kid1", true, 3, father1, mother1);
- Human kid2 = new Human("kid2", false, 5, father1, mother1);
- Human kid3 = new Human("kid3", true, 4, father1, mother1);
- System.out.println(grandFather1.toString());
- System.out.println(grandFather2.toString());
- System.out.println(grandMother1.toString());
- System.out.println(grandMother2.toString());
- System.out.println(father1.toString());
- System.out.println(mother1.toString());
- System.out.println(kid1.toString());
- System.out.println(kid2.toString());
- System.out.println(kid3.toString());
- }
- public static class Human
- {
- //Написать тут ваш код
- String name;
- boolean sex;
- int age;
- Human father;
- Human mother;
- Human (String name,boolean sex, int age, Human father, Human mother)
- {
- this.name = name;
- this.sex = sex;
- this.age = age;
- this.father = father;
- this.mother = mother;
- }
- public String toString()
- {
- String text = "";
- text += "Имя: " + this.name;
- text += ", пол: " + (this.sex ? "мужской" : "женский");
- text += ", возраст: " + this.age;
- if (this.father != null)
- text += ", отец: " + this.father.name;
- if (this.mother != null)
- text += ", мать: " + this.mother.name;
- return text;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement