Bill Teter
Office:          149 Redcay
Telephone:    2782
Office Hours:  Tuesday, Thursday 9:00-12:00

email:  william.teter@plattsburgh.edu

       

 

Lab 4        Genealogy and related questions

Discussion:
    Genealogy concerns the family relationships of people over time.  A person's date of birth is an important part of a genealogy.  Further a person has ancestors and may have descendants.  A culture creates names for various relations:  cousin, aunt, uncle, siblings.

Specification:
    A person has a name, date of birth, mother, father, and a list of children.  A person may be either male or female.  A genealogy has a map between names and persons.  The Genealogy class will provide the following methods:

void addPerson(Person p)
        // adds p to the geneolgy by adding the pair (p.name, p) to the genealogies map.

Person getPerson(String n)
        // returns the person who has name n.  Null is returned if no person has n for a name.

Set<String> getNames()
        // returns the set of all names of people in the genealogy.

Set<Person>  getGrandParents(Person p)
        // returns the set of grandparents of Person p.  The set will contain only the known grandparents of p so it could be empty or have size less than 4.

Set<Person> getGrandParents(String n)
        // pre:  n is the name of some person in genealogy.
        // returns the set of grandparents of person with name n.

Set<Person> getSiblings(Person p)
        // returns  set of siblings of Person p.  Two people are siblings if they have the same mother or the same father.  If p has no parents return an empty set.

Set<Person> getSiblings(String n)
        // pre: n is the name of some person p in genealogy.
        // returns getSiblings(p).

Testing:
    A data file called gen.data is in the public directory.   Create a Test class that uses this text file to build a genealogy object.  Then test each of the methods in Genealogy class in the following order:

-- list all names, one per line

 

 

-- list the grandparents of Sam, one per line.  (Do this twice. Once for the String parameter, once for the Peron parameter.

-- list the grandparents of John, one per line.  Note, John doesn't have any.  (Do this twice. Once for the String parameter, once for the Peron parameter.

-- list the siblings of Max one per line.  (Do this twice. Once for the String parameter, once for the Peron parameter.

-- list the siblings of Frank one per line.  (Do this twice. Once for the String parameter, once for the Peron parameter.

Implementation advice:

    See lab 1 to review how to read from a file.

    Your test program will need to split each line of the data file into an array of string tokens.  Examine the data file so you see how its contents are formatted.  I suggest you use the date data to create a new date object. Then, check to see if the mother field is "'null" or not.  If not "null" you may assume the named person has already appeared in the data file and can be retrieved from the genealogy with the getPerson() method.  After the mother Person object has been retrieved, do the same for father.  You are now ready to create the new Person and add that person to the genealogy.


    Set is a generic interface.  To create a set you will use the HashSet implementation.  For example:

        Set<Person> siblings = new HashSet<Person>();

will create a set called siblings.  The set interface includes the add() method.  So you can add a Person p to the set by

        siblings.add(p).

For the various accessor methods your code should begin by creating a set with the appropriate generic, when you have something to add to the set, add it.  When you are done the method will return the the set.

    In the Test program you may use the following code to print everything in a set.  It is called an enhanced for loop.  The inside of the loop can of course be any statements.  Assume that setX is a set of Person objects:

        for (Person p : setX)
            System.out.println(p);

This code will print one person per line.