Write an application to simulate the rolling of two dice the application should
ID: 3828826 • Letter: W
Question
Write an application to simulate the rolling of two dice the application should use an object of class Random once to roll the Fust die and again to roll the second die, the sum of the two values should then be calculated Each die can show an integer value from 1 to 6, so the sum of the values will vary from 2 to 12, with 7 being the most frequent sum, and 2 and 12 the least frequent. Figure 6.36 m your textbook shows the 36 possible combinations of the two dice. Your application should roll the dice 36,000 times. Use a one-dimensional array to tally the number of times each possible sum appears Display the results in tabular format Determine whether the totals are reasonable. Your output from your program should resemble the following:Explanation / Answer
TwoDice36000.java
import java.text.DecimalFormat;
import java.util.Random;
public class TwoDice36000 {
public static void main(String[] args) {
final int ROLLS = 36000;
final int SIZE = 13;
//Declaring variables
int x; // first die
int y; // second die
double percentage;
//Creating an array
int freq[]=new int[SIZE];
//Creating an random class object
Random r = new Random();
//DecimalFormat class is used to format the output
DecimalFormat df=new DecimalFormat("##");
// roll dice 36,000 times
/* Write a for loop that iterates ROLLS times. Randomly
generate values for x (i.e., die1) and y (i,e, die2)
and increment the appropriate counter in array sum that
corresponds to the sum of x and y */
for(int i=0;i<ROLLS;i++)
{
x=r.nextInt(6) +1;
y=r.nextInt(6) +1;
freq[x+y]++;
}
//Displaying the table
System.out.println("Sum Frequency Percentage");
for(int i=2;i<13;i++)
{
percentage=((double)freq[i]/ROLLS)*100;
System.out.println(i+" "+freq[i]+" "+df.format(percentage));
}
}
}
__________________
Output:
Sum Frequency Percentage
2 1005 3
3 2002 6
4 2942 8
5 3967 11
6 5064 14
7 5907 16
8 4993 14
9 4102 11
10 2968 8
11 2000 6
12 1050 3
_________Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.