ITEC 120 LAB 8 Back to Lab index page

Download these two files: Die.java and RollingDice.java.

Die.java defines a class -- it contains data used to describe the state of a die object and methods which define services the class provides. What are the data and what are the methods?

RollingDice.java uses the class to define die objects, and calls methods from the Die class. We sometimes refer to the this kind of file as a driver. The driver contains the main method.

Compile and run the driver. (When you compile the driver, which creates Die objects, Die.java will get compiled too, as long as Die.java is in the same directory as the driver.)

Look at the code in both files. What methods in Die.java are being called by the driver? Look at the output. What do the methods do?

Go into the driver and remove .toString() from the first println statement. Recompile and run again. What effect did that have?

Now, add a method to Die.java. Write a method called getHiddenValue which returns the value which will be on the bottom of the die. On six sided die, opposite sides always add up to 7, so 1 is opposite of 6, 3 is opposite of 4, and 2 is on the bottom when 5 is showing. Look at the other methods in the file to help you figure out the syntax. Compile the file to see if you got the syntax correct. Notice that you cannot run this file by itself. There is no main method!

Next, modify the driver to call the new method you just wrote. After the last roll of the two dice, report what is on the bottom of the two dice.

Show your code and your working program to your instructor or peer instructor to be checked off. Be prepared to answer the questions in bold!