Question: Create a lab09 project in Eclipse (or use the one you created for the pre-lab). Make sure your Die and SnakeEyes classes are in your
Create a lab09 project in Eclipse (or use the one you created for the pre-lab).
Make sure your Die and SnakeEyes classes are in your project directory.
Create a new class called PairOfDice.
Create a new class called DiceRoller and add a main method.
By the end of this lab, you will have three classes that build on one another.
Die (from the pre-lab): Represents a single die with a current face value between 1 and the maximum value specified in its constructor.
DiceRoller: The main driver class that creates dice, rolls them, and prints their face values.
PairOfDice: Manages a pair of Die objects so we don't have to keep track of 2 dice in our DiceRoller class.
Part 1: Create the PairOfDice class
The PairOfDice class will manage a pair of Die objects so we don't have to keep track of 2 dice in the main method of our DiceRoller.
A pair of dice.
Instance Data
Your PairOfDice class will need variables to store two Die objects.
Constructors
Both dice in a PairOfDice will have the same maximum face value.
Pro tip: When you think you are done writing your constructors, go to the main method in your DiceRoller driver class and test them out before moving on. You should be able to create a new PairOfDice instance using either constructor.
Declaration: PairOfDice()
Creates 2 Die objects with the default number of sides.
Declaration: PairOfDice(int numSides)
Accepts an integer parameter specifying the number of sides and creates 2 Die objects with the same number of sides.
Getter Methods
Include getter methods for your instance data.
Pro Tip: Test your methods in DiceRoller as you write them.
Getter: public int getFaceValue1(): Returns the current face value of the first Die only.
Getter: public int getFaceValue2(): Returns the current face value of the second Die only.
Getter: public int getTotal(): Returns the sum of the current face values.
Setter: (we don't need any setters)
Public methods
Method declaration: public int roll(): Rolls both Die objects and returns the sum of their face values.
When rolling a PairOfDice, both dice will be rolled at the same time, though their individual values are independent.
toString Method
Write a public String toString() method that returns a representation of the PairOfDice. The string you return should look something like:
7 (1 + 6)
In the above example, 7 is the sum of the face values of the dice, 1 is the face value of the first die, and 6 is the face value of the second die.
Part 2: Use your PairOfDice to play a game
Implement DiceRoller to use a PairOfDice object to play a game where the user and the computer each get to roll pairs of dice and the one with the higher total wins.
Instead of two individual Die objects, use one PairOfDice object. When you create your PairOfDice, pass in a reasonable maximum face value (e.g. 6, 4, 12).
Now, modify the main method of DiceRoller to contain a loop that
Rolls the PairOfDice for the user and then rolls the same PairOfDice again for the computer.
Reports the results of each roll (user and computer).
The report for each roll will contain the total value of the PairOfDice followed by the face value of each Die (see sample session in section below).
Determines and announces the winner (or a tie).
Asks the user if they would like to roll again.
The loop will continue to repeat as long as the user continues entering 'y'.
As long as the play continues, DiceRoller should keep track of the number of wins for each player and report the total score after each round.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
