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.
Make a family
You must be signed in to leave a comment
4 December 2021, 09:48
If I created 4 granparents 1 father 1 mother and 3 children inside the ArrayList<Human> How many objects a ihave created? what's wrong with my code?
7 September 2021, 18:12
Well, I think a concise exercise diferent to those would be more helper for all of us that we have trying to understand the concepts. I feel that I'm trying to do things that I don't know yet and nobody taught me. Human... children ???
16 July 2021, 01:19
I wish we were allowed to edit the constructor parameters. Would have been so much less tedious.
11 March 2021, 10:58
This piece of code Human... children I'd never guess. From my understanding it enables creating objects with various number of children. I researched online and found that this is called "the three dots (...) are used in a function’s declaration as a parameter. These dots allow zero to multiple arguments to be passed when the function is called. The three dots are also known as var args."
9 July 2021, 15:13
Yes, but you can solve this task without varargs. If you also pass the constructor an ArrayList containing the children of the given Human object.
23 January 2021, 10:26
enjoyed this exercise. really helped me to understand objects and constructors better.
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 Software Developer at EPAM
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.
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 :).
19 September 2021, 21:37
4 June 2020, 05:42
Challenging. I understand the relationship between a constructor and passing arrays better though.
June Sung Park
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!