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

email:  william.teter@plattsburgh.edu

       

 

Lab 1

Objectives:
    Download programs that complement the text
    Intro to emacs
    Write a java application that uses two classe

1.  Create a directory for this course, csc223:
        mkdir csc223

2a.  Go into this directory  (ie. make it your pwd--present working directory):
        cd csc223
You will keep all your lab work for this course in this directory.

2b. Create a subdirectory of csc223 called weiss  and make it your present working directory:
        mkdir weiss
        cd weiss

3.  Download code.tar from Mark Weiss' text site at
http://www.cs.fiu.edu/~weiss/dsj3/code/code.html
and place it in the csc223/weiss  subdirectory.  Untar the file with the linux command
    tar -xvf code.tar
This will create lots of .java files and build a new subdirectory called weiss that contains even more sample code used in the text. 

Steps for lab project 1

4.  Go back to directory csc223 and make a new subdirectory inside csc223 called lab1.

5.  Make lab1 your present working directory.

Use emacs to create two classes -- Person.java and House.java according to the following specifications:

Person class

Person will have the following fields:
    -  A String field called name
    -  An int field called age
    -  A field of type House called home
    - A field of type int called gender.  Create public static final int constants called MALE and FEMALE with values of 0 and 1.
    - A field of type Person called mother
    - A field of type Person called father

Person class will have the following constructor and methods:
    - A constructor that specifies (in this order):
        name, age, gender, mother, father
    - A method with signature public String toString() that returns the state of a Person
    - A accessor method with signature public House getHome() that returns the value of home
    - A mutator method with signature public void setHome(House h) which sets the value of home to h

House class

House will have the following fields:
    - A String field called address
    - A Person field called owner

House will have the following methods:
    - A constructor that specifies:
        address
    - A method with signature public String toString() that returns the state of a House
    - a accessor method with signature public Person getOwner() that returns the value of owner
    - a mutator method with signature public void setOwner(Person p) which set the owner to p.

Write a test program that tests the constructors and all the methods in both classes.  Put the test program in its own class called Test.java.  Make up data about a family or two and the houses they live in.   It is impossible for all persons to have a mother and father without some incestuous relationships.  So use null as the value for the mother and father fields when constructing persons when necessary.

You may discover a problem as you print data about a person.  The printing may go on forever.  This is because House and Person reference each other.  That is, a person may live in a house that he owns.  To break the infinite recursion you might want to just print the name of the owner of the house rather than all the data about the owner.  Likewise, in the Person class you may wish to only print the mother and father's names, not the entire state of the mother and father. To accomplish this you will need an accessor method for the name field in the Person class.  Remember also that a method may not be invoked on a null object.