Question: Part 1 : Create the PairOfDice Class The PairOfDice class will manage a pair of Die objects so we don t have to keep track

Part 1: Create the PairOfDice Class
The PairOfDice class will manage a pair of Die objects so we dont have to keep track of 2 dice in the main method of our DiceRoller class.
1.
Instance Data
a.
Your PairOfDice class will need properties declared to store two Die objects.
2.
Constructors
a.
Both dice in a PairOfDice will have the same maximum face value.
i.
Signature: public PairOfDice()
1.
Creates 2 Die objects with the default number of sides
ii.
Signature: public PairOfDice(int numSides)
1.
Accepts an integer parameter specifying the number of sides and creates two Die objects with the same number of sides.
3.
Getter Methods
a.
Include getter methods for your properties.
i.
Getter: public int getFaceValue1()
1.
Returns the current face value of the first Die only.
ii.
Getter: public int getFaceValue2()
1.
Returns the current face value of the second Die only.
iii.
Getter: public int getTotal()
1.
Returns the sum of the current face values of both dice.
iv.
No setter methods are needed.
4.
Public Methods
a.
Method declaration: public void roll()
i.
Rolls both Die objects.
1.
When rolling a PairOfDice, both dice will be rolled at the same time, though their individual values are independent.
5.
toString Method
a.
Write a public String toString() method that returns a representation of the PairOfDice. The string you return should look like: 7(1+6)
b.
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.
1.
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).
2.
Now, modify the main method of DiceRoller to contain a loop that
a.
Rolls the PairOfDice for the user and then rolls the same PairOfDice again for the computer.
b.
Reports the results of each roll (user and computer).
i.
The report for each roll will contain the total value of the PairOfDice followed by the face value of each Die (see the sample session below).
ii. Determines and announces the winner (or tie).
iii.
Asks the user if they would like to roll again.
c.
The loop will continue to repeat as long as the user continues entering y.
d.
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.
e.
Note: Your output should look like the following:

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!