Inheritance (wagon, saferwagon)
CS 330 - Fall 2004 - Ms. Katz

Due: Friday, November 12th at 10pm
There are two directories to turn in for this assignment. See note below.

Goals
 
- to better understand how inheritance works
- to build a few small Java classes

Overview

In this assignment, you will build three small Java classes. These are extremely straightforward with no input from the user which conveniently avoids the input issues in Java. The C++ assignments on inheritance from earlier terms were of similar complexity.

You'll build a class that represents a wagon (imagine a farmer's wagon that would be pulled to market). The original Wagon class does absolutely no bounds checking. Then you will extend the Wagon class to do bounds checking so that the wagon doesn't overflow or try to empty more than it has. That will be the SaferWagon.

I'm giving you one driver class, PileUp, and an outline of the Wagon class.

There are two directories to turn in for this assignment. The first will contain your Wagon class and PileUp (main) class. The second will contain your Wagon class (which will have changed from the first one), SaferWagon class, and TakeCare (main) class. It is okay for the second submission to have the PileUp file in it as well. Or you may want to create another directory when you complete the first part.

Getting Started

You will find the files supporting this lab in /home/grader/katz330/labs/wagon. There is a driver class (PileUp.java) along with a skeletal Wagon.java class that documents the member functions you should build.

To start, make a directory called wagon and copy the Wagon class and PileUp driver into that directory.

Compiling and Running Java Programs

Run your Java source code (whatever.java) through javac, the Java compiler, which will create files with the same name as your original source code but ending in class rather than java. Use the java program to run the main class (give the root of the file name without .java or .class). Be careful with the file endings. For example:
   javac Wagon.java PileUp.java
   java PileUp

You can build documentation from the .java files with the javadoc program if you have the special comments. You don't need to create the documentation for this assignment, but this is useful to know:
   javadoc Wagon.java PileUp.java

What You Need to Build

Fill in the member functions in Wagon. Each member function has a comment describing what it should do. The code you add will look pretty much like C++. Do not have any checks in this code. None. Return true for load and unload.

Compile and run it. Be sure you use javac to compile and java to run. The main function is in PileUp. What output should you expect? The code has some hints, but work through the examples.

Build your own tests. Include them in a separate function after mine in PileUp. Provide answers as in my tests. Have at least four output lines reporting on the wagon's state.

Submit this directory as wagon.

Plan to extend Wagon with SaferWagon. The safer wagon will ensure that the contents stays in bounds. That is, the SaferWagon will not allow the user to load above the capacity or remove beyond the existing contents. If a load or unload operation cannot be completed as requested, it is not done at all and returns false as its return value. Your comments should reflect this behavior.

For safety, create a new directory and copy your submitted Wagon class to this new directory. Create a new SaferWagon.java file. You can copy and paste from the Wagon class to get comments and function signatures when you need them.

Now think about this subclass. Will it have new instance variables? No, all of its information will be stored in Wagon's instance variables. But the instance variables in Wagon are private. What does that mean you can and cannot do to them? They should remain private, and SaferWagon should not have instance variables of its own.

Start building SaferWagon. SaferWagon extends Wagon. Note that in the class definition (see NamedTimer). You'll need to build the constructor, but you can't change the instance variables. As the first (and only) statement in SaferWagon's constructor, call Wagon's constructor as super( ) with appropriate parameters.

You'll also build a driver class called TakeCare that will look a lot like PileUp but will use SaferWagon. At this point, write a simple version of TakeCare based on PileUp but using SaferWagon. Note that after you've built the constructor, you can compile the three classes (Wagon, SaferWagon, and TakeCare) and run TakeCare. Any functions you haven't built for SaferWagon will be performed by the functions in Wagon.

Write the SaferWagon load function. Don't modify Wagon's load function to do this. Check for overloading here in SaferWagon. Do not change the contents if the amount to add doesn't fit. You shouldn't print an error message here. Just don't do the action and return false. You know enough about how to do this. Compile and check that. You'll want to be sure you have tests in TakeCare that will uncover bugs in your load function. Test the boundaries.

Write the SaferWagon unload function. Think about why this isn't as easy as the load. What function did you use in the check in load? What similar function does not exist for unload? Add any additional functions you need to Wagon, but do not change the instance variables from private and do not change Wagon's unload function. Comment any functions appropriately. Think simple.

Now write SaferWagon's unload so that it doesn't unload cargo it doesn't have.

Check your work. Clean up the comments. The output in TakeCare should help me see that you have the answers you expect. You should call the new function you wrote for Wagon at least once from TakeCare. Add tests to TakeCare that will uncover bugs. Submit as saferwagon when you're done.

That wasn't so bad, was it?

If you really think you need a Java book, consider Cay Horstmann's Computing Concepts with Java Essentials: www.horstmann.com/ccj.html or Big Java: www.horstmann.com/bigjava.html. You might also try Sun's Java Tutorial: java.sun.com/docs/books/tutorial/. Maybe you can play with that over winter break. However, it is my intention that between what we've done in lecture and the timer example, you have enough information to complete this assignment without additional references.