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!!
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...
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.