MC28 Lab 7: CompuDuds in Java (Spring 1997)

Due: May 21, 1997

In this lab, you'll make a few minor alterations to a Java version of the CompuDuds program from our textbook. The point is to become familiar with the mechanics of Java. The program is divided into five classes, each in its own file. The five files are To make a copy of all the files, the simplest approach would be to use the shell to copy the whole directory containing the files. If you in a shell window type
cp -r ~max/www-docs/courses/S97/MC28/labs/lab7/code .
(note that this command ends with a space and then a period), you will get a subdirectory called code containing all the files. Ask for help with this if you need it.

To compile and run the program, you'll need to work in a shell (terminal) window. First you'll need to change directory into the directory where you have the files, e.g.

cd code
Note that if you log out and log back in, you will need to again to the cd command to change to the correct directory.

The first thing to do is to test the program out without yet having made any changes. That way you have a baseline for comparison: if it isn't working, you aren't the one who broke it. To run the program, use the command

java -cs CompuDuds
The first time you do this, it will take quite a while for the program to start up, before it prints out its menu of choices. This is because the Java system is "compiling" your program, i.e., checking it over and translating it into an internal form, which is stores in files ending in .class. However, if you run the program again, it will be much quicker to start, because the compiling is already done. If you change one of the .java files and save it out, then when you next run the java command given above, it will recompile the file you changed, and so it will take longer, but not as long as the first time when it needed to compile all the files.

Now you can start making the necessary changes, and test and debug as you go along. You are to do the following:

  1. Modify the ItemList class (by editing the file and saving it out) so that it keeps a running total price as Items are added and deleted, and simply returns that in the totalPrice method, rather than adding up all the prices then. The program's outward behavior should remain unchanged.

  2. Add one or more new kinds of clothing, perhaps to suit your own tastes better. Each new class you add should get saved into its own file with a name ending in .java

  3. Add the ability to mark down the price of an Item by 10%, and use it to add the ability to mark down the price of all the Items in an ItemList by 10%. Finally, add a new option to the CompuDuds user interface that discounts everything that has been selected by 10%.

    Warning 1: Discounting each item by 10% and discounting the total price by 10% may wind up with a total price that doesn't match the sum of the individual prices, due to roundoff.

    Warning 2: Because thePrice is an int, you can't say

    thePrice = .9 * thePrice;
    (The problem is that multiplying by .9 results in something that isn't an int, and hence can't be stored into thePrice.) Instead you can say
    thePrice = thePrice * 9 / 10;
    This works because in Java (unlike Scheme), when the / has an int on both its left and righ, it means to take the integer quotient, and so you wind up with the quotient by 10 of the product of thePrice and 9.

Course web site:
Instructor: Max Hailperin <>
Other lab instructor: Karl Knight <>