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

email:  william.teter@plattsburgh.edu

       

 

Lab 2 -- Person class.

Objectives:  create class hierarchies that model a geneology.

1.  Create a directory called lab2 in your repository work space.  In this workspace touch the following files.
        touch Test.java
        touch Person.java
        touch Male.java
        touch Female.java
        touch Date.java

These are the only files that you will submit to the repository.  Go to the parent directory (cd ..) and add these files to  the the repository directory with
    svn add lab2.
When you are ready to commit the files to the repository you may (at any time) from the parent directory enter the command
    svn commit lab2.

2.  Write the code for the Date class.  There will be 3 instance variables, month, day and year of type int.  The constructor will specify the values of these instance variables with three parameters.  Override toString() so that it returns a string for a date object using the normal format like "2/8/2008".  Write code in the Test program to make sure the Date class works correctly.

3.  Write the code for the Person class.  It should be abstract.  The class should specify two instance variables:  name of type String and dateOfBirth of type Date.  The constructor should have appropriate parameters for the name and dateOfBirth fields.  Override the toString() method so that the person's name and dateOfBirth are returned as a String.

4.  Extend Person with the two classes Male and Female.  The constrctors for these classes should have name and dateOfBirth parameters and invoke the constructor from the Person class using super().  Override toString() so that the string returned begins with "male" or "female" and is proceeded by name and date of birth.  (This too can be done by using super.  super.toString() will invoke the toString method from the parent class.

5.  Write code in the Test program that creates some persons and uses System.out.println() to make sure that they have appropriate values in their instance variables.

6.  Add mother and father instance variables to the Person class.  They of course should be of type Female and Male respectively.  (Note that these instance variables are references to Person objects not just their String names.)  Add appropriate parameters to the constructors in Person, Male, and Female.  When a Person is created the mother and/or father may be unknown.  In these cases a null argument will be used.  Leave the toString() methods as they are but add accessor methods:
        getMother()
        getFather()
that return the correct Person object.  Modify your Test program and check that getMother() and getFather() work.

7.  Add a new instance variable to Person called children that is of type List<Person>.  Create the list in the constructor and use an ArrayList<Person> implementation.  Add a mutator method to Person called addChild(Person child) which adds child to the children list of this person.

8.  Add a accessor method to the Person class that returns the list of children:
        public List<Person> getChildren()

9.  Now add to the constructor in Person code that adds this person to the children list of the mother and father so long as they are known (not null).

10.  Add a class variable to Person that gives the population, the total number of persons created to date. Write an accessor method for the variable called getPop().  Add code to the constructor in Person that increments population as each person is created.

11.  In your Test program create 4 persons using the following data.  bob was born 3/4/1980 and mary was born 2/19/82.  They just had twins today (2/9/2008) named june and sam.  For each of these 4 persons p print:
        System.out.println(p + " children:  "+ p.getChildren());
You should also print the population.