Make a family

  • 10
Let's try to create a program for a family census. To do this, we'll write a Human class with String name, boolean sex, int age, and ArrayList children fields. To practice, create objects and fill them with data so we end up with two grandparents, two grandmothers, a father, a mother, and three children. Display the objects on the screen.
You can't complete this task, because you're not signed in.
Comments (28)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jurij Thmsn Level 14, Flensburg, Germany
23 January, 10:26
enjoyed this exercise. really helped me to understand objects and constructors better.
Vitalina Tkachenko Level 17, Poland
17 October 2020, 20:41
I did also the children for grandfather and grandmother. So in result I had 4 ArrayLists: 1) children of grandfother1 and grandfather1 (it would be only father) 2) children of grandfother2 and grandfather2 (it would be only mother) 3) children of mother and father (3 kids) 4) noChilds (it would be empty because children don't have children, but we have to add it because of toString() method)
Juanf Level 25, Vilnius, Lithuania
3 August 2020, 10:00
Would be better to have an example of expected output, otherwise we're practising guessing abilities, in addition to java abilities.
Devonte A Level 17, Rubery, United Kingdom
8 June 2020, 23:15
1. Avoid null pointer exception - Initialize Arraylist e.g. ArrayList<Human> children = new ArrayList<>(); 2. Create three lists a. List with all children b. List with Father c. List with Mother 3. Constructor is not counted as a method. The rest should be fine :).
Lucas Hoage Level 14, Savannah, United States
4 June 2020, 05:42
Challenging. I understand the relationship between a constructor and passing arrays better though.
June Sung Park Level 41, Yangp'yong, Korea, Republic of
25 March 2020, 06:04
This family has paternal grandpa and grandma, maternal grandpa and grandma, father, mother and three children. I first created an array list of grandchildren who don't exist. Then I created three children. Each child has the array list of grandchildren as his/her children. I added those three children to an array list of children. Then I created father and mother. Each of them had the array list of children as his/her children. Now I added the father to an array list of dad, and added the mother to an array list of mom. Finally, I created paternal grandpa and grandma, each having the array list of dad as his/her children. Similarly, I created maternal grandpa and grandma, each having the array list of mom as his/her children. This led to a perfect verification!
Tony Roy Level 41, Victorville, United States
6 March 2020, 14:29
I was tripped up on this one because the conditions say "the Human class must have one method." But, as we have been told, a constructor is itself a method. Validation goes through with a constructor for the class.
Guadalupe Gagnon Level 37, Tampa, United States
6 March 2020, 14:57
Constructors are not methods. Constructors can't be called from an object reference while methods can be.
Balibee Level 13, Luzern, Switzerland
12 March 2020, 19:49
Hi, is toString considered a method? I was also confused by the 'must have one method' requirement so i didn't write a constructor.
Guadalupe Gagnon Level 37, Tampa, United States
12 March 2020, 19:54
toString() is a method. And constructors are not methods. Methods are declared by and must include a return value, name, and arguments (between no args and infinite). Constructors do not have a return value and are declared by using the class name with arguments.
class Solution{

   // this is a constructor.
   Solution(){
   }
   // this is a constructor that has one String argument
   Solution(String name){
   }

   // this is a method. It has a return value of void (meaning no return value)
   // a name doSomething, and no arguments
   void doSomething(){
   }

   // this is another method. It returns an int, has the name doSomething,
   //and includes 2 int arguments
   int doSomething(int a, int b){
   }
}
Attila Level 25
25 February 2020, 19:57
I think it is a great Task and practice, once you understand what to do. So if you struggle with understanding the Conditions / Requirements, like I did than: You need to create at least 3 ArrayLists. - One you fill up with the children, and this List will be passed to the parents. - One you fill up with one of the parents, and this List will be passed to the fist set of Grandparents. - One you fill up with the other parent, and this List will be passed to the second set of Grandparents. - In order to pass the verification, I had to create a 4th. Arraylist, which was empty, and I passed it to the Children. Otherwise I could not print them, (It gave a NullPointerException error without it) Maybe there is a better way to do this. I found this explanation in the Help section. I spent a long time trying to google what to do, but it got me nowhere.
Thomas Sixberry Level 16, Rochester Hills, United States
5 February 2020, 17:24
took awhile but we made it..
Kent Hervey Level 15, United States
17 January 2020, 01:00
I have been tracking the "Task has been completed by....# This task is the lowest number so far...I guess it is one of the hardest