Results 1 to 3 of 3
  1. #1
    Swetz55 is offline Member
    Join Date
    Dec 2009
    Posts
    1
    Rep Power
    0

    Question 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:

  2. #2
    asina12 is offline Member
    Join Date
    Dec 2009
    Posts
    3
    Rep Power
    0

    Default Calling the Function again

    Hi Swetz55,

    Java Code:
    paternalFamilySize = father.familySize();
    I'll try to explain...

    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 :

    Java Code:
    if(father == null)
    AND

    Java Code:
    if(mother == 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.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,565
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  1. Calling a generated GUI function
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-09-2009, 07:02 PM
  2. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  3. Weird problem upon calling same function twice
    By alin_ms in forum New To Java
    Replies: 2
    Last Post: 12-20-2008, 06:14 PM
  4. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  5. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 06:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •