Question: In this lab you will write three classes: Die, PairOfDice, and Player. The Die class mimics the rolling of a die. Specifically, this class will
In this lab you will write three classes: Die, PairOfDice, and Player.
The Die class mimics the rolling of a die. Specifically, this class will implement the following methods:
A default constructor initializing the number of sides of a die to 6.
An overloaded constructor that takes an integer number of sides (assume greater than 1).
roll which generates and returns a random number between 1 and the number of sides (inclusive).
An accessor method to read the value of the face on the die.
A toString method returning the string representation of the face value.
The maximum number of sides should be stored as a private constant in the Die class. Also use the Random class for the random number generator.
The PairOfDice class mimics the rolling of two dice. Specifically, this class will implement the following methods:
A default constructor that creates and initializes the number of sides of each die to 6.
An overloaded constructor that creates and takes two integer number of sides, one for each die.
roll rolls each die and returns the sum.
An accessor method to read the sum of the dice.
The Player class implements the main method which creates the dice pair and rolls them several times reporting the results.
I need a variable for die1, and variable for die2, then add those up to get the sum of a roll. However I am not supposed to have a third variable that is the sum.
An example ouput should look like this (numbers will be random):
Die 1: 2
Die 2: 4
Roll1: 6
Die 1: 6
Die 2: 3
Roll2: 9
What I have so far:
import java.util.Random; public class Die { private int NumberOfSides, SideValue; private Random random; public Die() { this.NumberOfSides = 6; random = new Random(); } public Die(int NumberOfSides) { this.NumberOfSides = NumberOfSides; random = new Random(); } public int roll() { SideValue = random.nextInt(NumberOfSides) + 1; return SideValue; } public int getFaceValue() { return SideValue; } public String toString() { return String.valueOf(SideValue); } }
..............................................................
public class PairOfDice { private Die die1; private Die die2; public PairOfDice() { die1 = new Die(); die2 = new Die(); } public PairOfDice(int NumberOfsides1, int NumberOfSides2) { die1 = new Die(NumberOfsides1); die2 = new Die(NumberOfSides2); } public int roll() { return die1.roll() + die2.roll(); } public int getSum() { return roll(); } }
...............................................................
public class Player { public static void main(String[] args) { PairOfDice pairOfDice = new PairOfDice(); for (int i = 1; i <= 5; i++) { System.out.println("Roll " + i + " Roll1: " +die1.roll()); System.out.println("Roll " + i + " Roll2: " +die2.roll()); System.out.println("Roll " + i + " Sum: " + (roll())); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
