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

In java please, while following instructions carefully! If you could also leave

ID: 3709291 • Letter: I

Question

In java please, while following instructions carefully! If you could also leave comments, that would be greatly appreciated! Thank you!!

Write a program called DiceRolls that simulates multiple rolls of two 6-sided dice (use two random numbers for this) and computes the percentage of the total number for each possible outcome. The program should ask the user how many times to roll the dice and use a loop to simulate that many rolls. The program must use an int array to keep track of how many times each possible roll (i.e., 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12) occurs. You may also find it convenient to use double arrays to handle the observed and expected percentages. After all the "rolls" have been performed, determine what the actual observed percentage of the total number of rolls was for each number. For example, if the dice were rolled 500 times and the value 8 was observed 62 times, then the observed percentage would be 12.40%. However, the expected percentage for 8 is 13.89% (5 out of 36 rolls)-you can find the percentages for other rolls onlin at several websites, e.g., (http://boardgames.about.com/od/dicegames/a/probabilities.htm). For each possible roll, display the roll value, the observed percentage, and the expected percentage. Display the results to the console as a table, aligning the results in nice even columns using the printf method. For example: Enter the number of rolls to be simulated: 1300 The percentages observed for 1300 rolls: roll observed expected 2.83% 6.00% 8. 12% 2. 78% 5. 56% 8.33% 4 12 2. 51% 2.78%

Explanation / Answer

Dear Student,

package cheggy;

import java.util.Scanner;

public class Dice {

private static Scanner sc;

public static int getRandomInt(int min, int max) {

min = (int)Math.ceil(min);

max =(int) Math.floor(max);

return (int)Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive

}

public static void main(String args[]) {

int dice1;

int dice2;

int sum;

double iteration;

double obs;

double expec;

double[] expected= {2.778,5.556,8.333,11.111,13.889,16.667,13.889,11.111,8.333,5.556,2.778};

double[] total = new double[11]; //all values of SUM possible ie. 2 to 12 (index 0-10 respectively) initialized to 0;

System.out.println("Enter the number of rolls to be simulated:");

sc = new Scanner(System.in);

iteration=sc.nextDouble();

double temp=iteration;

while(temp>0) {

dice1=getRandomInt(1,7);

dice2=getRandomInt(1,7);

sum=dice1+dice2;

System.out.println(sum);

total[sum-2]++;//the sum that appears is incremented in respective array index

temp--;

}

System.out.println("roll observed expected");

System.out.println("---- --------- --------");

for(int i=2;i<=12;i++) {

obs=(double)(total[i-2]/iteration)*100;

expec=expected[i-2];

System.out.print(i+" ");

System.out.print(obs);

System.out.print(" "+expec);

System.out.println();

}

}

}

Feel Free to ask any doubt...

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