CMPT 126 - Harbour Centre - Fall 2006
Home  
Assigned Readings  
Examples  
Labs  
Assignments  
References  
Gradebook  
 
 

For this lab, you will create a simple class hierarchy using inheritance.

  1. Create a Java class Furniture. Each piece of furniture has a cost (an integer number of cents, so a cost of 999 represents $9.99), and a description (a String).

    Create a toString method that outputs information about a piece of furniture in the form:

    Coffee Table ($67.99)

    Hint: return String.format("%s ($%.2f)", ....);

  2. Create a class Chair that is a subclass of Furniture that adds a boolean to keep track of whether or not the chair is padded. Add an appropriate constructor that sets the three member variables.

    Do not create a toString method: use the one inherited from Furniture.

  3. Create a class Table that is a subclass of Furniture. Each table will have a width and length (in meters), in addition to the properties of all furniture.

    Create a constructor that sets the member variables of a Table. Override the toString method so an instance is printed in this form:

    Coffee Table [2.3 x 1.5 m] ($67.99)
  4. Create a class FurnTest that contains a main function. In the main function, create an ArrayList of Furniture objects. Add to it some objects of each type (Chair, Table).

    Then, iterate through the list and output each one (with System.out.println), creating output like this:

    Kitchen Chair ($59.99)
    Kitchen Chair ($59.99)
    Kitchen Chair ($59.99)
    Kitchen Chair ($59.99)
    Kitchen Table [2.3 x 1.5 m] ($129.50)
  5. Create a text file named time.txt. In it, indicate how long it took you to complete this lab exercise.

When you're done, create a ZIP file containing all of the files you created for this lab and submit it with the submission server.