Post Lab 8: Rolling a Pair of Dice
Due Monday, November 7, 2005
In this assignment you will write a PairOfDice class and then use
it in a program with a loop.
The PairOfDice class must meet the following specifications:
Instance data: Two Die objects (die1 and die2).
Use the class Die.java from your
textbook.
Methods:
- a constructor with no parameters - the constructor will instantiate
each Die object.
- a method roll that rolls each die and returns the sum of
the face values. The method should have
no parameter. (NOTE: You need to use methods in the Die class here.)
- 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.
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" - the values on each die - not just the total.)
- Next keep rolling the dice until you roll point again. 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.
Hand In:
- Hard copy of PairOfDice.java and PlayGame.java.
- Tar you post lab directory that contains all of your files and email
it to your instructor with the subject cs120 post8.