package com.codegym.task.task08.task0824;
import java.util.*;
import java.util.ArrayList;
/*
Make a family
*/
public class Solution {
public static void main(String[] args) {
//write your code hereSystem.out.println(c1.toString());
ArrayList<Human> generation_1 = new ArrayList<Human>();
ArrayList<Human> generation_3 = new ArrayList<Human>();
ArrayList<Human> generation_2 = new ArrayList<Human>();
Human c1 = new Human("nvkvslk",23,true,generation_1);
Human c2 = new Human("nvkvslk",23,true,generation_1);
Human c3 = new Human("nvkvslk",23,true,generation_1);
Human f = new Human("fwik",23,true,generation_2);
Human m = new Human("fwik",23,false,generation_3);
generation_1.add(f);
generation_1.add(m);
Human gm = new Human("jksdfn",23,false,generation_3);
Human gm1 = new Human("jksdfn",23,false,generation_2);
Human gf = new Human("jksdfn",23,true,generation_2);
Human gf1 = new Human("jksdfn",23,true,generation_3);
System.out.println(c1.toString());
System.out.println(c2.toString());
System.out.println(c3.toString());
System.out.println(f.toString());
System.out.println(m.toString());
System.out.println(gm.toString());
System.out.println(gm1.toString());
System.out.println(gf1.toString());
System.out.println(gf.toString());
}
public static class Human {
//write your code here
String name;
boolean sex;
int age;
ArrayList<Human> children = new ArrayList<>();
Human(String name , int age , boolean sex , ArrayList <Human> children)
{
this.name=name;
this.age=age;
this.sex=sex;
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;
}
}
}
unable to pass the last condition
Under discussion
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
12 November 2019, 16:14
The problem in this code is that the children (the array inside person class) are not set properly on the Human objects created in main.
You will need to rethink the relationship between the 4 grandparents, 2 parents, and 3 children.
0