CLASS ASSIGNMENT 1

 

 

 

RollForSeven.java

crapstat2.java

Download these programs. The first program rolls dice until the roll comes up seven and reports the number of rolls. The second simulates 100000 games of craps and reports the number of wins. Each of these programs uses the class PairOfDice to represent a pair of dice. However, this class does not exist yet. If you try to compile the programs, you will get an error message telling you that class PairOfDice cannot be found.

Your assignment is to write a PairOfDice class that will work with the RollForSeven and crapstat2 programs. You should have private instance variables to represent the numbers showing on the two dice. These numbers change when the dice are rolled. You will need a method called roll() that rolls the dice and returns the total. You will also need methods called getDie1() and getDie2() that return the numbers showing on each of the dice. When an object is created, you should make sure that the instance variables are in a legal state. If you don't assign values to them, they will have the value zero, which is not possible with real dice. (You can either initialize the instance to some legal values like 3 and 4, or roll the dice when an object is first constructed.) These initial values are not used in either of the sample program, but when you are simulating real objects, it's a good idea to simulate them as closely as possible.

Your class should be written in a file named PairOfDice.java. After you have written and compiled it, you should be able to compile and run RollForSeven.java and crapstat2.java.