Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.javarush.test.level07.lesson12.home06;
- /* Семья
- Создай класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human). Создай объекты и заполни их так, чтобы получилось:
- Два дедушки,
- две бабушки,
- отец,
- мать,
- трое детей. Вывести объекты на экран.
- Примечание:
- Если написать свой метод String toString() в классе Human, то именно он будет использоваться при выводе объекта на экран.
- Пример вывода:
- Имя: Аня, пол: женский, возраст: 21, отец: Павел, мать: Катя
- Имя: Катя, пол: женский, возраст: 55
- Имя: Игорь, пол: мужской, возраст: 2, отец: Михаил, мать: Аня
- …
- */
- public class Solution
- {
- public static void main(String[] args)
- {
- //Написать тут ваш код
- /*Создаем обьекты конструктора, c параметрами*/
- Human grandFather=new Human("Дід Віталій", true,100,null,null);
- Human grandFatherM=new Human("Дід Антон",true,100,null,null);
- Human grandMother=new Human("Баб Валя", false,80,null,null);
- Human grandMotherM=new Human("Баб Ліда",false,80,null,null);
- Human father=new Human("Батько Сашко", true,60,grandFather,grandMother);
- Human mother=new Human("Мати Валентина",false,50,grandFatherM,grandMotherM);
- Human kid1=new Human("Рома",true,30,father,mother);
- Human kid2=new Human("Саша",true,29,father,mother);
- Human kid3=new Human("Владик", true,28,father,mother);
- /*Через обьект класса Human добираемся до .toString()*/
- System.out.println(grandFather.toString());
- System.out.println(grandFatherM.toString());
- System.out.println(grandMother.toString());
- System.out.println(grandMotherM.toString());
- System.out.println(father.toString());
- System.out.println(mother.toString());
- System.out.println(kid1.toString());
- System.out.println(kid2.toString());
- System.out.println(kid3.toString());
- }
- public static class Human
- {
- //Написать тут ваш код
- /*класс Human с полями имя(String), пол(boolean),возраст(int), отец(Human), мать(Human).*/
- String name;
- boolean sex;
- int age;
- Human father;
- Human mother;
- /*Конструктор Human*/
- 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