Assignment 10: 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 five meal plans, which we will call Plan1 through Plan 5, going down the table describing them. We want to model the card reader by having an object for each card. Copy from the class directory the following files, which model such a system.

> cp /homeemp/tvandrun/pub/241/MealPlan.java .
> cp /homeemp/tvandrun/pub/241/Plan1.java .
> cp /homeemp/tvandrun/pub/241/Plan2.java .
> cp /homeemp/tvandrun/pub/241/Plan3.java .
> cp /homeemp/tvandrun/pub/241/Plan4.java .
> cp /homeemp/tvandrun/pub/241/Plan5.java .
> cp /homeemp/tvandrun/pub/241/MealPlanDriver.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 classes (possibly abstract) 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 project 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

Create the script file as before (cat--only files you've written or changed, rm, compile, and run)

 > a2ps -P sp (the name of the script file)

(This will print "two up", meaning two pages shown next to each other on one pice of paper. If you use a2ps on a Java file, it will format it nicely like in the handouts I've given in class. The command lpr works similarly except it does no formatting and doesn't print two up by default.)

Then turn in the hard copy by 5:00, Wed, Apr 13.


Thomas VanDrunen
Last modified: Thu Apr 7 13:31:34 CDT 2005