Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates the rolling of two dice. Use an Array or ArrayLis

ID: 3624482 • Letter: W

Question

Write a program that simulates the rolling of two dice. Use an Array or ArrayList to keep
track of the number of times that each total number is thrown. In other words, keep track of
how many times the combination of the two simulated dice is 2, how many times the
combination is 3, and so on, all the way up through 12.

Allow the user to choose how many times the “dice” will be thrown. Then, once the dice
have been thrown the specified number of times, print a histogram (using the * character)
that shows the total number of times each number was rolled.


Sample session:

Welcome to the dice throwing simulator!

How many dice rolls would you like to simulate? 100

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: **

Thank you for using the dice throwing simulator. Goodbye!

Explanation / Answer

import java.util.*;
class Throws
{
public static void main(String[] a)
{
Scanner in = new Scanner(System.in);
System.out.println("Welcome to the dice throwing simulator!");
System.out.println("How many dice rolls would you like to simulate? ");
int k = in.nextInt();
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 = " + k);
displayResults(simulateThrows(k));
System.out.println("Thank you for using the dice throwing simulator. Goodbye! ");
}

private static int[] simulateThrows(int k) {
Random r = new Random();
int[] counts=new int[11];
for(int n=0; n<k; n++)
++counts[r.nextInt(6)+r.nextInt(6)];
return counts;
}

private static void displayResults(int[] counts)
{
for(int i=0; i<counts.length; ++i)
{
System.out.print((i+2) + ":");
for(int n=0; n<counts[i]; ++n)
{
System.out.print('*');
}
System.out.println();
}
System.out.println();
}
}

SAMPLE OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote