When tossing one die, results of 1 through 6 are possible results. So, when toss
ID: 3767509 • Letter: W
Question
When tossing one die, results of 1 through 6 are possible results. So, when tossing a pair of dice, sums of 2 through 12 are possible results. If the dice are fair, it can be shown that the probability of tossing each of these sums is:
2 or 12: 1/36; 3 or 11: 1/18; 4 or 10: 1/12;
5 or 9: 1/9; 6 or 8:5/36; 7: 1/6
Write a Java program that will repeatedly simulate tossing a fair pair of dice a number of times specified by the user (any number between 1 and 100,000), and calculate and display the total number of each sum produced along with its percentage of the total tosses (the relative frequency approximation of probability).
Your program must contain these methods:
rollDie – this method will receive an array and the number of tosses and will simulate tossing one die the specified number of times, storing the toss results in the array.
findSum – this method will receive the number of tosses, two arrays holding the toss results for two dice, and a third array, and will calculate the toss sums, storing these in the third array.
tossCount – this method will receive the number of tosses, an array of toss sums, and an array of counters for the possible sums, and will examine the toss sums array and count the toss sum totals.
display – this method will receive the number of tosses and an array of counters for the possible sums, and display the total number of tosses, and the total number of each of the possible sums, along with the probability of each possible sum.
Your method main should repeatedly obtain a number of tosses from the user and then call the other methods to handle the simulations, until the user elects to exit the program.
Notes and suggestions:
In rollDie, use the Math.random() method to simulate each die toss. Remember Math.random() return a random number greater than or equal to 0.0 and less than 1.0. You can use (int) (Math.random() * 6 + 1) to get an integer from 1 to 6.
Methods tossCount and display both receive “an array of counters for the possible sums”. As noted above, the toss sums will be values 2 through 12. So, use an integer array of size 13, with all elements initialized to 0, for the toss sums counter array. Then, to count a sum of value k (k between 2 and 12), increment element k of the toss sums counter array.
In display, the probability of a given sum is the percentage of tosses that produced that sum.
In display, use the tab escape sequence to produce evenly spaced columns of numbers.
Explanation / Answer
import java.util.*;
class simulateDie {
public static void main(String args[] ) throws Exception {
int n;
Scanner in = new Scanner(System.in);
String s;
do
{
System.out.println("enter the number of time you want to roll the die");
n=in.nextInt();
int[] a1 = new int[n];
int[] a2 = new int[n];
int[] asum = new int[n];
int[] tcnt = new int[13];
rollDie(a1,n);
rollDie(a2,n);
findSum(n,a1,a2,asum);
tossCount(n,asum,tcnt);
display(n,tcnt);
System.out.println("enter q to quit");
s=in.next();
}while(s!="q");
}
public static void rollDie(int[] a, int n)
{
int i;
for(i=0;i<n;i++)
{
a[i]=(int)(Math.random()*6+1);
}
}
public static void findSum(int n, int[] a, int[] b, int[] c)
{
for(int i=0;i<n;i++)
{
c[i]=a[i]+b[i];
}
}
public static void tossCount(int n, int[] asum, int[] cnt)
{
for(int i=0;i<n;i++)
{
cnt[asum[i]]++;
}
}
public static void display(int n, int[] cnt)
{
System.out.println("number of tosses = "+n);
double[] pr=new double[13];
for(int i=2;i<8;i++)
pr[i]=pr[14-i]= (i-1)/36.0;
System.out.println(" sum count probability");
for(int i=2;i<13;i++)
{
System.out.println(i+" "+cnt[i]+" "+pr[i]);
}
}
}
output:
q
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.