Question: (Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the

(Dice Rolling) Write an application to simulate the rolling of two dice. The application should use an object of class Random once to roll the first 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 7.28 shows the 36 possible combinations of the two dice. Your application should roll the dice 36,000,000 times. Use a one-dimensional array to tally the number of times each possible sum appears. Display the results in tabular format.

Example output looks like this:

Sum Frequency Percentage 2 1020 2.83 3 1946 5.41 4 3006 8.35 5 4021 11.17 6 4940 13.72 7 6003 16.68 8 5011 13.92 9 4057 11.27 10 3025 8.40 11 1908 5.30 12 1063 2.95

Code looks like this:

import java.util.Random;

import java.util.Arrays;

public class DiceRolling {

public static void main(String args[]) {

Random dice = new Random();

int[] diceRolls = new int[36];

int roll1;

int roll2;

// Loops through total number of desired dice rolls

for(int count = 0; count < 36000000; count++) {

roll1 = (dice.nextInt(6) + 1);

roll2 = (dice.nextInt(6) + 1);

int row = 0;

int column = 0;

// Roll1 determines row

if(roll1 == 1) {

row = 30;

} else if(roll1 == 5){

row = 24;

} else if(roll1 == 4){

row = 18;

} else if(roll1 == 3){

row = 12;

} else if(roll1 == 2){

row = 6;

} else {

row = 0;

}

// Roll2 determines column

if(roll2 == 6) {

column = 5;

} else if(roll2 == 5) {

column = 4;

} else if(roll2 == 4) {

column = 3;

} else if(roll2 == 3) {

column = 2;

} else if(roll2 == 2) {

column = 1;

} else {

column = 0;

}

// Column + Row = value that corresponds to a coordinate

diceRolls[(row + column)] += 1;

}

// Print values

System.out.printf("\t%d\t%d\t%d\t%d\t%d\t%d ", 1, 2, 3, 4, 5, 6); // column label

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

for(int table = 0; table < diceRolls.length; table++) {

if( (table) % 6 == 0) {

System.out.printf(" %d|\t", (table / 6 + 1) ); // row label

}

System.out.printf("%d\t", diceRolls[table]);

}

// Formatting

System.out.println("");

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!