Results 1 to 3 of 3
Thread: Calling the Function again
- 12-19-2009, 08:17 AM #1
Member
- Join Date
- Dec 2009
- Posts
- 1
- Rep Power
- 0
Calling the Function again
I have problem understanding this code...
5) Implement a class "Person". A person has a name (a string), an
age (a double), as well as a father and a mother (persons as well).
All these fields should be private. Then write a "familySize" method
that counts how many members there are in a family. For example, with
the following Main class:
////////////////////////////////////////////////////////////
public class Main{
public Main(){
}
public static void main(String[] args){
Person paternalGrandfather = new Person("a", 59, null, null);
Person paternalGrandmother = new Person("b", 61, null, null);
Person maternalGrandfather = new Person("c", 62, null, null);
Person maternalGrandmother = new Person("d", 58, null, null);
Person myFather = new Person("e", 40, paternalGrandfather, paternalGrandmother);
Person myMother = new Person("f", 40, maternalGrandfather, maternalGrandmother);
Person me = new Person("g", 20, myFather, myMother);
Person neighbour = new Person("z", 99, null, null);
System.out.println("My family size: " + me.familySize());
}
}
////////////////////////////////////////////////////////////
the output should be:
------------------------------------------------------------
My family size: 7
------------------------------------------------------------
The Solution is given ..
public class Person{
private String name;
private double age;
private Person father, mother;
public Person(String name, double age, Person father, Person mother){
this.name = name;
this.age = age;
this.father = father;
this.mother = mother;
}
public int familySize(){
int paternalFamilySize, maternalFamilySize;
if(father == null){
paternalFamilySize = 0;
}else{
paternalFamilySize = father.familySize();
}
if(mother == null){
maternalFamilySize = 0;
}else{
maternalFamilySize = mother.familySize();
}
return 1 + paternalFamilySize + maternalFamilySize;
}
}
paternalFamilySize = father.familySize(); << This Calls the function family size again but how is counting the family size?? :confused:
- 12-19-2009, 11:02 AM #2
Member
- Join Date
- Dec 2009
- Posts
- 3
- Rep Power
- 0
Calling the Function again
Hi Swetz55,
I'll try to explain...Java Code:paternalFamilySize = father.familySize();
Function public int familySize() is an example of a recursive function. We know that each person has a father and a mother. What this function does is that it calls each person's father/mother until that person has no father/mother, then returns one and unwinds the stack.
This is what the call would look like for father's family size:
me.familySize -> myFather.familySize -> paternalGrandfather.familySize AND maternalGrandmother.familySize
The final two calls (paternalGrandfather and maternalGrandmother) would result in :
ANDJava Code:if(father == null)
Being true and hence respectively return 1 + 0 + 0 = 1. Moving back to myFather.familySize, the return would be 1 + 1 + 1 = 3. (The 2 ones being the number of father's mom and dad). Repeat for Person me's mom.Java Code:if(mother == null)
- 12-19-2009, 11:10 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Your family size is your father's family size plus your mother's family size plus one (that's you). Note that I define your family size in terms of family sizes of some other people. That's what recursion is all about:
FS(you) = FS(your father) + FS(your mother) + 1
If somebody doesn't have a mother or father anymore that family size will be counted as zero. That criterion stops the recursion and results in the elementary start value(s) from which the final family size is composed.
kind regards,
Jos
Similar Threads
-
Calling a generated GUI function
By dejos456 in forum New To JavaReplies: 5Last Post: 12-09-2009, 07:02 PM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
Weird problem upon calling same function twice
By alin_ms in forum New To JavaReplies: 2Last Post: 12-20-2008, 06:14 PM -
problem calling function from class to class
By alin_ms in forum New To JavaReplies: 3Last Post: 12-19-2008, 07:35 PM -
Calling Java Function through JSP
By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 06-06-2008, 06:28 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks