Lab 4: Inheritance

The goal of this assignment is to practice designing a class hierarchy and using inheritance.

As usual, make a new directory for this project and cd into it.

1. Introduction

Bon Appetit has four meal plans. The first three (going down the list), we'll call plans 1-3. For the purposes of this lab, we will split the fourth plan in the list into two, Plan 4 and Plan 5, the difference being that for one of these plans, the card can be used at any meal, but for the other it can be used only for breakfast and lunch (this is the way it used to be at the dining hall; it makes the problem a little more interesting). We want to model the card reader by having an object for each card.

After making a new directory for the lab, copy from the class directory the following files, which model such a system.

cp /cslab/class/cs245/lab4/*.java .

Look at the files. Notice that all "Plan" classes implement a common interface, defined by MealPlan; on each plan, you can charge breakfasts, lunches, dinners, or Stupe purchases, and you can also reset the week and print the card's current status. Read through the driver, too, and figure out what it does (basically, it tries out each plan).

2. Designing a class hierarchy

The problem is, the type hierarchy is very flat (all classes merely implement the interface), and this has resulted in a lot of redundancy. Your task for this assignment is, by way of writing new abstract classes and using subclassing and inheritance, to get rid of as much redundancy as possible.

You are operating on one rule:

Don't change the driver MealPlanDriver.java, and don't change the other classes in any way that will break the driver (ie, there must be non-abstract classes called Plan1 etc, there must be a type MealPlan...)

3. Hint

Notice that this lab already works without you doing anything. Make small changes, one at a time, and make sure it still works after each change.

3. Turn in

To show that your project works, you should print out a record of your using the program. First off, use the command script

script

This creates a file called typescript. Everything that happens in the terminal will be recorded in that file, until you type exit. Now, display all your files (so they'll be recorded in typescript) using the cat command, which prints a file to the terminal.

cat *.java

Now, compile your program and run it five times, chosing a different meal plan each time, to demonstrate that it works.

Finally, exit the script

exit

and print it out.

a2ps typescript

Thomas VanDrunen
Last modified: Thu Sep 20 08:27:51 CDT 2007