Post Lab 10: Rolling a Pair of Dice
Due Friday, November 13, 2009
In this assignment you will write a PairOfDice class (in file
PairOfDice.java) and then use
it in a program (PlayGame.java) with a loop.
The PairOfDice class must meet the following specifications:
Instance data: Two Die objects (die1 and die2).
You need to save the class Die.java from your
textbook (it is on pages 176-177 - an example program that uses
Die objects is on page 175). Do not change Die.java - just use
it. For example, you will
declare the two instance variables die1 and die2
to be of type Die.
- a constructor with no parameters - the constructor should instantiate
each Die object.
- a method roll that rolls each die (use the method in
the Die class - rolling a pair of dice involves rolling each
single die!) and returns the sum of
the face values. The method should have
- a method isDoubles that returns true if the face values on
the two dice are the same ("doubles") and false otherwise. Note that
you do not need to use an if - you can do this with a single
return statement. (Again,
you need to use methods in the Die class here and in the following methods.)
- a toString method that returns the PairOfDice object as a string in
the following format (assuming the first die is a 2 and the second a 5):
[ 2 : 5 ]
- a method getTotalRoll that returns the total of the
face values on the two dice (this method DOES NOT roll the dice
again - it just returns the total of the current face values).
Write a program PlayGame that does the following:
- Instantiates a PairOfDice object.
- Rolls the pair of dice and
saves the value of the roll (call this point).
- Prints out the first roll, appropriately labeled. (Print
the "roll" - that is, print the object which shows
the values on each die - not just the total.)
- Next keep rolling the dice until you roll point again (the variable
point does not change - the goal is to roll the same total
as your first roll). Count
the number of times you roll and the number of times that you get doubles.
Print out each roll.
Note that this needs a loop. The general structure will be as follows:
// Roll for point (as described above)
// Loop set up - initialize the counting variables (one for number
// of rolls and one for doubles) and roll the dice to get a new roll
while the new roll is not equal to point
// print the current dice, appropriately labeled
// increment the count for the number of rolls
// if the roll is doubles increment the doubles counter
// roll again
- After the loop print out the number of rolls it took to match
point (appropriately labeled) and the number of doubles that were rolled
while you were rolling the dice.
Submission: cp a tgz file (with your
name as the file name) that contains the java files for your program to the