Everything prints correctly with and without the for loop.What is the problem ? It fails to last requirement.
package com.codegym.task.task08.task0824;
import java.util.ArrayList;
/*
Make a family
*/
public class Solution {
public static void main(String[] args) {
//write your code here
ArrayList<Human> children = new ArrayList<Human>();
children.add(new Human("child1", true, 3));
children.add(new Human("child2", true, 1));
children.add(new Human("child3", false, 5));
Human gf1 = new Human("grandfather1", true, 70);
Human gf2 = new Human("grandfather2", true, 65);
Human gm1 = new Human("grandmother1", false, 70);
Human gm2 = new Human("grandmother2", false, 65);
Human father = new Human("father", true, 30);
Human mother = new Human("mother", false, 32, children);
System.out.println(gf1.toString());
System.out.println(gf2.toString());
System.out.println(gm1.toString());
System.out.println(gm2.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
for(int i = 0; i < mother.children.size(); i++)
{
System.out.println(mother.children.get(i));
}
}
public static class Human {
//write your code here
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<Human>();
public Human(String name, boolean sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
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 += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + 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.codegym.task.task08.task0824;
import java.util.ArrayList;
/*
Make a family
*/
public class Solution {
public static void main(String[] args) {
//write your code here
ArrayList<Human> children = new ArrayList<Human>();
children.add(new Human("child1", true, 3));
children.add(new Human("child2", true, 1));
children.add(new Human("child3", false, 5));
Human gf1 = new Human("grandfather1", true, 70);
Human gf2 = new Human("grandfather2", true, 65);
Human gm1 = new Human("grandmother1", false, 70);
Human gm2 = new Human("grandmother2", false, 65);
Human father = new Human("father", true, 30);
Human mother = new Human("mother", false, 32, children);
System.out.println(gf1.toString());
System.out.println(gf2.toString());
System.out.println(gm1.toString());
System.out.println(gm2.toString());
System.out.println(father.toString());
System.out.println(mother.toString());
for(int i = 0; i < mother.children.size(); i++)
{
System.out.println(mother.children.get(i));
}
}
public static class Human {
//write your code here
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<Human>();
public Human(String name, boolean sex, int age)
{
this.name = name;
this.sex = sex;
this.age = age;
}
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 += "Name: " + this.name;
text += ", sex: " + (this.sex ? "male" : "female");
text += ", age: " + this.age;
int childCount = this.children.size();
if (childCount > 0) {
text += ", children: " + this.children.get(0).name;
for (int i = 1; i < childCount; i++) {
Human child = this.children.get(i);
text += ", " + child.name;
}
}
return text;
}
}
}