Question: import java.util.Random; /** * Die.java * * Represents one die (singular of dice) with faces showing values * between 1 and 6. * * @author
| * Represents one die (singular of dice) with faces showing values |
| * @author Java Foundations |
| * @author CS121 Instructors (modified a few things from book) |
| private final int MAX = 6; // maximum face value |
| private int faceValue; // current value showing on the die |
| * Constructor: Sets the initial face value of this die. |
| * Computes a new face value for this die and returns the result. |
| * @return The new face value. |
| //faceValue = (int)(Math.random() * MAX) + 1; |
| faceValue = rand.nextInt(MAX) + 1; |
| * Face value mutator. The face value is not modified if the |
| * specified value is not valid. |
| * @param value The new face value. Must be between 1 and max face |
| public void setFaceValue (int value) |
| if (value > 0 && value <= MAX) { |
| * @return The current face value. |
| public int getFaceValue() |
| * Returns a string representation of this die. |
| String result = "Die [faceValue = " + faceValue + "]"; |
}
--------------------------------------------------------------------
You will use your Die class from the pre-lab to create a PairOfDice class. Then you will create a DiceRoller program to play a dice game against the computer using your PairOfDice.
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.
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.
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
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.
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)
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.