Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

5. Give examples of variations in contextual rules.

Answered: 1 week ago