Question
Using and Writing methods in Java with dice roll example You are to write a complete Java program named Lab04.java. Your program will simulate rolling
Using and Writing methods in Java with dice roll example
You are to write a complete Java program named Lab04.java. Your program will simulate rolling 2 six-sided dice, and keep track of how many times each possible roll (2, 3, . . . , 12) occurs.
First roll the dice 100 times and calculate the fraction of each of the value (2, 3, . . . , 12). In other words, calculate the number of occurrences of each value divided by 100. Compare these fractions with the probabilistic values for each number;
# fraction prob value
2 1/36 0.0278
3 2/36 0.0556
4 3/36 0.0833
5 4/36 0.1111
6 5/36 0.1389
7 6/36 0.1667
8 5/36 0.1389
9 4/36 0.1111
10 3/36 0.0833
11 2/36 0.0556
12 1/36 0.0278
Write a static void method called rollDice that has two parameters, and int and a Random. The int parameter determines how many times to roll the dice and the Random is used to generate the actual values. Note that we could make the Random variable local to the method, but that would create a new object with each call, which is not necessary. Think carefully about which method in Random to call and how to appropriately generate the actual roll values. For example, consider why simply generating a single random number between 2 and 12 would NOT be correct. In the method, do the rolls and count how many times each number comes up. Then print out the number of times each number comes up and its fraction out of all the rolls. In the main program, create the Random object, then enter a conditional loop. At each iteration of the loop, ask the user to enter the number of rolls desired and call the rollDice() method with the appropriate parameters. Then as the user if he/she wants to continue. If so, repeat the process; if not terminate the program. The idea is that each iteration of the main program loop calls the rollDice() method and performs one experiment, showing the empirical probabilities of the rolls which you can compare to the theoretical probabilities
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started