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(25)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Devonte ALevel 14 , Rubery
8 June, 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 HoageLevel 14 , Savannah
4 June, 05:42
Challenging. I understand the relationship between a constructor and passing arrays better though.
June Sung ParkLevel 27 , Yangp'yong
25 March, 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 RoyLevel 41 , Victorville
6 March, 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 GagnonLevel 37 , Tampa
6 March, 14:57
Constructors are not methods. Constructors can't be called from an object reference while methods can be.
BalibeeLevel 13 , Luzern
12 March, 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 GagnonLevel 37 , Tampa
12 March, 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){
   }
}
AttilaLevel 20
25 February, 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 SixberryLevel 16 , Rochester Hills
5 February, 17:24
took awhile but we made it..
Kent HerveyLevel 13
17 January, 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
zyLevel 36 , Jakarta
23 October 2019, 18:55
if last req not validating and u have null pointer exception because when u set null to the arraylist and then "toString" method trying to get list size() where u set null so threr is exception becouse null doesnt has any method lol try to add just new ArrayList<HUman>() i mean empty list
Jonathan FellerLevel 17 , Munich
4 September 2019, 12:24
pretty silly requirements again, why should class variables not be private?
TurnerLevel 11 , Pittsburgh
3 September 2019, 17:11
Sadly, my use of lambda here "fails" requirement #3. Apparently using the Consumer construct (look it up, if interested) trips the verification as adding another method to Main(). I am quickly choosing to have higher attempt counts if it allows me to practice advance / best practice concepts and constructs of the Java language. My hope is eventually I can practice ANY Java AND meet the puzzle requirements as well.