Create a Dice class that simulates rolling a die with a different number of side
ID: 653504 • Letter: C
Question
Create a Dice class that simulates rolling a die with a different number of sides. The default is a
standard die with six sides. You should have a default constructor that sets the number of sides
to 6. You should have another constructor that creates a new instance of Dice with the number
of sides in the argument. You should have one member function that rolls one die.
Write a main function that creates two Dice objects with a number of sides of your choosing.
Roll both dice and add the results. Roll the dice 10 times.
Now create a class, LoadedDice that is derived from Dice. Override the die roll function so that
it doubles the result of the die roll. You should still be able to create a LoadedDice object that
has the default 6 sides, or the number specified when you call the constructor.
Test your class by adding code to roll dice with LoadedDice objects.
Explanation / Answer
public class SnakeEyes {
public static void main (String[] args){
final int
ROLLS = 500;
int snakeEyes = 0, num1, num2;
Die die1 = new Die();
//creates a six-sided die
Die die2 = new Die(20);
// creates a twenty-sided die
for(int roll = 1; roll <= ROLLS; roll++){
num1 = die1.roll();
num2 = die2.roll();
if (num1 == 1 && num2 == 1)
// check for snake eyes
snakeEyes++;
}
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of snake eyes: " + snakeEyes);
System.out.println ("Ratio: " + (double)snakeEyes/ROLLS);
}
}
Die.java
import java.util.Random;
public class Die{
private final int MIN_FACES = 4;
private static Random generator =new Random();
private int numFaces; // number of sides on the die
private int faceValue; //current value showing on the die
public Die (){
numFaces = 6;
faceValue = 1;
}
//-----------------------------------------------------------------
// Explicitly sets the size of the die. Defaults to a size of
// six if
the parameter is invalid. Initial face value is 1.
//-----------------------------------------------------------------
public Die (int faces){
if (faces < MIN_FACES)
numFaces = 6;
else
numFaces = faces;
faceValue = 1;
}
//-----------------------------------------------------------------
// Rolls the die and returns the result.
//-----------------------------------------------------------------
public int roll ()
{
faceValue = generator.nextInt(numFaces) + 1;
return faceValue;
}
//-----------------------------------------------------------------
// Returns the current die value.
//-----------------------------------------------------------------
public int getFaceValue ()
{
return faceValue;
}
}
------------------------------------------------------------------------------------------
public class DiceSimulation {
private int trials = 0, // Formerly count
min = Integer.MAX_VALUE, // Formerly lowest
max = 0, // Formerly finalSum
sum = 0; // Formerly totalSum
private Die die1 = new Die(),
die2 = new Die();
/**
* One trial consists of tossing a pair of dice until a sum of 7 is obtained.
* The result of the trial is the sum of all tosses up to, but not including,
* the toss that resulted in 7.
*/
public int runTrial() {
int trialSum = 0, pairSum; // Formerly theSum and diceSum
while (7 != (pairSum = die1.toss() + die2.toss())) {
trialSum += pairSum;
}
if (trialSum > max) {
max = trialSum;
}
if (trialSum < min) {
min = trialSum;
}
sum += trialSum;
trials++;
return trialSum;
}
public void report() {
System.out.println("After " + trials + " simulations: ");
System.out.println("Biggest sum: " + max);
System.out.println("Smallest sum: " + min);
System.out.println("The average is: " + (double)sum / trials);
}
public static void main(String[] args) {
int trials = Integer.parseInt(args[0]);
DiceSimulation sim = new DiceSimulation();
for (int count = 0; count < trials; count++) {
sim.runTrial();
}
sim.report();
}
}