Write a complete program that simulates the rolling of two dice. More specifical
ID: 3682261 • Letter: W
Question
Write a complete program that simulates the rolling of two dice. More specifically, it simulates a user-specified number of rolls and then prints a histogram of the number of rolls for each possible pair value. In other words, it prints a histogram for the number of times that two was thrown, the number of times that three was thrown, and so on, all the way up to the number of times that twelve was thrown. Use a frequency array to keep track of the number of times each pair value is thrown.
For example, the frequency[2] element holds the number of times two is thrown. The frequency[3] element holds the number of times three is thrown. As part of your program, write a DiceSimulation class that implements these methods:
newSimulation This method clears out the frequency array (assigns all elements to zero),
prompts the user for the number of rolls, and then simulates the rolls.
additionalRolls This method prompts the user for the number of rolls that should be added
to the current dice-rolling simulation. It then simulates the additional rolls.
printReport This method prints the dice-rolling simulation results.
As always, you should:
Limit your use of class variables and instance variables – only use them if appropriate.
Use appropriate modifiers for your methods. The modifiers we’ve discussed are private,
public, static, and final.
Use helper methods if appropriate.
Mimic the sample session precisely. In particular, note the window’s title, the prompt text, the result-
message text, and the button labels.
Provide a driver class that tests your DiceSimulation class. Your driver class should contain this
main method:
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String choice; // user's choice of action
boolean done = false; // user's quit flag
DiceSimulation diceSimulation = new DiceSimulation();
System.out.println(
"Welcome to the dice throwing simulator! ");
do
{
System.out.println(
"Options: (n)ew simulation, (a)dditional rolls," +
" (p)rint, (q)uit");
System.out.print("Enter n, a, p, or q ==> ");
choice = stdIn.nextLine();
switch (choice.charAt(0))
{
case 'n': case 'N':
diceSimulation.newSimulation();
break;
case 'a': case 'A':
diceSimulation.additionalRolls();
break;
case 'p': case 'P':
diceSimulation.printReport();
break;
case 'q': case 'Q':
done = true;
break;
default:
System.out.println("Invalid selection.");
} // end switch
} while (!done);
} // end main
Sample session:
Welcome to the dice throwing simulator!
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> x
Invalid selection.
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> N
How many dice rolls would you like to simulate? 200
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> a
How many additional rolls? 100
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS
Each "*" represents 1% of the total number of rolls.
Total number of rolls = 300.
2: ***
3: *******
4: ********
5: ***********
6: *************
7: *******************
8: ***************
9: ********
10: **********
11: *****
12: *
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> a
How many additional rolls? 10000
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS
Each "*" represents 1% of the total number of rolls.
Total number of rolls = 10300.
2: ***
3: *****
4: ********
5: ***********
6: **************
7: *****************
8: **************
9: ***********
10: *********
11: ******
8
© 2014 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
12: **
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> n
How many dice rolls would you like to simulate? 100
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS
Each "*" represents 1% of the total number of rolls.
Total number of rolls = 100.
2: ***
3: ***
4: ***********
5: ***********
6: ********
7: ******************
8: ****************
9: **********
10: *************
11: *****
12: **
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> q
Explanation / Answer
DiceSimulation.java
import java.util.Scanner;
public class DiceSimulation
{
//Variables
Scanner stdIn = new Scanner(System.in);
int numberOfRolls;
int moreRolls;
int roll;
//*******************************************************************************
public void newSimulation()
{
System.out.print("How many rolls of the dice" +
" would you like to perform? ==> ");
numberOfRolls = stdIn.nextInt();
}
//********************************************************************************
public void additionalRolls()
{
System.out.println("Rolls as of right now = " +
numberOfRolls);
System.out.print("How many additional rolls would" +
" you like to perform? ==> ");
moreRolls = stdIn.nextInt();
numberOfRolls = (moreRolls + numberOfRolls);// adds additional rolls to existing rolls
}// end method additionalRolls
//*******************************************************************************
public void printReport()
{
int[] rollCounter = new int[numberOfRolls + 1];// array to store rolls
double fractionOfReps;
int numOfAsterisks;
for (int rep=0; rep<numberOfRolls; rep++) // performs dice rolls
{
roll = ((int) (Math.random() * 6)+1) +
((int) (Math.random() * 6)+1);
rollCounter[roll]++; // adds roll to appropriate bin
}// end for
System.out.println();
System.out.println("DICE ROLLING SIMULATION RESULTS:");
System.out.println("Each * represents 1% of the total number of rolls.");
System.out.println("Total number of rolls = " + numberOfRolls);
System.out.println(" Roll Frequency Graph ");
for (roll=2; roll<=12; roll++)
{
// Prints the roll (2-12), the frequency it
// occurred, and a histogram of the percentage
// to the total number of rolls
System.out.printf(" %2d %2d ",roll,rollCounter[roll]);
fractionOfReps = (float) rollCounter[roll] / numberOfRolls;
numOfAsterisks = (int) Math.round(fractionOfReps * 100);
for (int i=0; i<numOfAsterisks; i++)
{
System.out.print("*");
}// end for
System.out.println();
}// end for
}// end method printReport
}// end class DiceSimulation
HelloWorld.java
import java.util.Scanner;
public class HelloWorld
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
String choice; // user's choice of action
boolean done = false; // user's quit flag
DiceSimulation diceSimulation = new DiceSimulation();
System.out.println(
"Welcome to the dice throwing simulator! ");
do
{
System.out.println(
"Options: (n)ew simulation, (a)dditional rolls," +
" (p)rint, (q)uit");
System.out.print("Enter n, a, p, or q ==> ");
choice = stdIn.nextLine();
switch (choice.charAt(0))
{
case 'n': case 'N':
diceSimulation.newSimulation();
break;
case 'a': case 'A':
diceSimulation.additionalRolls();
break;
case 'p': case 'P':
diceSimulation.printReport();
break;
case 'q': case 'Q':
done = true;
break;
default:
System.out.println("Invalid selection.");
} // end switch
} while (!done);
} // end main
}// end main class file
sample output
Welcome to the dice throwing simulator!
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> n
How many rolls of the dice would you like to perform? ==> 200
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> a
Rolls as of right now = 200
How many additional rolls would you like to perform? ==> 100
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==> p
DICE ROLLING SIMULATION RESULTS:
Each * represents 1% of the total number of rolls.
Total number of rolls = 300
Roll Frequency Graph
2 11 ****
3 13 ****
4 20 *******
5 31 **********
6 36 ************
7 53 ******************
8 35 ************
9 45 ***************
10 27 *********
11 21 *******
12 8 ***
Options: (n)ew simulation, (a)dditional rolls, (p)rint, (q)uit
Enter n, a, p, or q ==>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.