CPSC 124, Spring 2006
Lab 10: Writing a Class

With the test on Friday and with the second project and final project out for you to think about, this will be a particularly short lab. The goal of the lab is simply to write a certain complete class and to test it with a short main program. It is important for you to understand how to write a complete class, since you are responsible for that for the test on Friday. The class will be a variation on one of the examples that we have already looked at in detail: the PairOfDice class. You will write a class that can represent a pair of dice with any number of sides.

You will probably finish this lab before the end of the period. In that case, you can work on your Mosaic Draw project.

I will be grading Lab 9 some time this week. If you do any work on the second programming project before the end of the week, you should do it in a copy of your Eclipse project for Lab 9. When I grade your lab, I would like to be able to test the program just as you turned it in, before you start adding to it for the second programming project. To make a copy of a project in Eclipse, simply right-click on it, select "Copy", right-click again, and select "Paste".

To begin, you can create a new Eclipse project. Then create a new class named PairOfDice. An object of type PairOfDice represents a pair of dice with some given number of sides. Both dice in the pair have the same number of sides, which is specified when the pair is created. After the pair of dice object has been created, the number of sides cannot be changed. The class should contain at least:

The member variables of the class should be private. You should add comments to the class. Test your class with a main program that creates two pairs of 10-sided dice and rolls the two pairs over and over until they come up with the same total value. Print out the number of times that the dice were rolled. The class that contains the main program does not need to be commented.

Turn in printouts of both the class containing the main program and the PairOfDice class next Tuesday.


David J. Eck, March 2006