package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("child1", true, 19);
Human child2 = new Human("child2", false, 15);
Human child3 = new Human("child3", false, 8);
ArrayList<Human> childrenJr = new ArrayList<>();
childrenJr.add(child1);
childrenJr.add(child2);
childrenJr.add(child3);
Human father = new Human("father", true, 45, childrenJr);
Human mather = new Human("mather", false,42, childrenJr);
ArrayList<Human> childrenOlder = new ArrayList<>();
childrenOlder.add(father);
childrenOlder.add(mather);
Human grabdFather1 = new Human("grabdFather1", true, 92, childrenOlder);
Human grandMather1 = new Human("grandMather1", false, 89, childrenOlder);
Human grabdFather2 = new Human("grabdFather2", true, 87, childrenOlder);
Human grandMather2 = new Human("grandMather2", false, 85, childrenOlder);
System.out.println(grabdFather1.toString());
System.out.println(grabdFather2.toString());
System.out.println(grandMather1.toString());
System.out.println(grandMather2.toString());
System.out.println(father.toString());
System.out.println(mather.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;
}
Human(String name, boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
this.children = new ArrayList<>();
}
//напишите тут ваш код
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;
}
}
}
/*package com.javarush.task.task08.task0824;
/*
Собираем семейство
public class Solution {
public static void main(String[] args) {
//напишите тут ваш код
}
public static class Human {
//напишите тут ваш код
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;
}
}
}
*/
package com.javarush.task.task08.task0824;
/*
Собираем семейство
*/
import java.util.ArrayList;
import java.util.Collections;
public class Solution {
public static void main(String[] args) {
Human child1 = new Human("child1", true, 19);
Human child2 = new Human("child2", false, 15);
Human child3 = new Human("child3", false, 8);
ArrayList<Human> childrenJr = new ArrayList<>();
childrenJr.add(child1);
childrenJr.add(child2);
childrenJr.add(child3);
Human father = new Human("father", true, 45, childrenJr);
Human mather = new Human("mather", false,42, childrenJr);
ArrayList<Human> childrenOlder = new ArrayList<>();
childrenOlder.add(father);
childrenOlder.add(mather);
Human grabdFather1 = new Human("grabdFather1", true, 92, childrenOlder);
Human grandMather1 = new Human("grandMather1", false, 89, childrenOlder);
Human grabdFather2 = new Human("grabdFather2", true, 87, childrenOlder);
Human grandMather2 = new Human("grandMather2", false, 85, childrenOlder);
System.out.println(grabdFather1.toString());
System.out.println(grabdFather2.toString());
System.out.println(grandMather1.toString());
System.out.println(grandMather2.toString());
System.out.println(father.toString());
System.out.println(mather.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;
}
Human(String name, boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
this.children = new ArrayList<>();
}
//напишите тут ваш код
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;
}
}
}
/* Собираем семейство
1. Создай класс Human с полями имя (String), пол (boolean), возраст (int), дети (ArrayList<Human>).
2. Создай объекты и заполни их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей.
3. Выведи все объекты Human на экран (Подсказка: используй метод toString() класса Human).
Требования:
1. Программа должна выводить текст на экран.
2. Класс Human должен содержать четыре поля.
3. Класс Human должен содержать один метод.
4. Класс Solution должен содержать один метод.
5. Программа должна создавать объекты и заполнять их так, чтобы получилось: два дедушки, две бабушки, отец, мать, трое детей и выводить все объекты Human на экран.*/