Question

In: Computer Science

Analyzing dice rolls is a common example in understanding probability and statistics. The following calculates the...

Analyzing dice rolls is a common example in understanding probability and statistics. The following calculates the number of times the sum of two dice (randomly rolled) equals six or seven.

import java.util.Scanner;
import java.util.Random;

public class DiceStats {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();
int i; // Loop counter iterates numRolls times
int numRolls; // User defined number of rolls
int numSixes; // Tracks number of 6s found
int numSevens; // Tracks number of 7s found
int die1; // Dice values
int die2; // Dice values
int rollTotal; // Sum of dice values

numSixes = 0;
numSevens = 0;

System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();

if (numRolls >= 1) {
// Roll dice numRoll times
for (i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;

// Count number of sixs and sevens
if (rollTotal == 6) {
numSixes = numSixes + 1;
}
else if (rollTotal == 7) {
numSevens = numSevens + 1;
}
System.out.println("Roll " + (i+1) + " is " +
rollTotal + " (" + die1 +
"+" + die2 + ")");
}

// Print statistics on dice rolls
System.out.println("\nDice roll statistics:");
System.out.println("6s: " + numSixes);
System.out.println("7s: " + numSevens);
}
else {
System.out.println("Invalid rolls. Try again.");
}

return;
}
}

Create different versions of the program that:

  1. Calculates the number of times the sum of the randomly rolled dice equals each possible value from 2 to 12.
  2. Repeatedly asks the user for the number of times to roll the dice, quitting only when the user-entered number is less than 1. Hint: Use a while loop that will execute as long as numRolls is greater than 1. Be sure to initialize numRolls correctly.
  3. Prints a histogram in which the total number of times the dice rolls equals each possible value is displayed by printing a character like * that number of times, as shown below.

Figure 9.17.1: Histogram showing total number of dice rolls for each possible value.

Dice roll histogram:

2:  ******
3:  ****
4:  ***
5:  ********
6:  *******************
7:  *************
8:  *************
9:  **************
10: ***********
11: *****
12: ****

Solutions

Expert Solution

import java.util.Scanner;
import java.util.Random;

public class Main {
public static void main(String[] args) {
  
Scanner scnr = new Scanner(System.in);
Random randGen = new Random();

int numRolls; // User defined number of rolls
int numTwo = 0;
int numThree = 0;
int numFour = 0;
int numFive = 0;
int numSix = 0;
int numSeven = 0;
int numEight = 0;
int numNine = 0;
int numTen = 0;
int numEleven = 0;
int numTwelve = 0;
int rollTotal; // Sum of dice values
int die1;
int die2;

do
{
System.out.println("Enter number of rolls: ");
numRolls = scnr.nextInt();

if (numRolls >= 1) {
// Roll dice numRoll times
for (int i = 0; i < numRolls; ++i) {
die1 = randGen.nextInt(6) + 1;
die2 = randGen.nextInt(6) + 1;
rollTotal = die1 + die2;
  
//Switch case to increase specific variable as per rollTotal value
switch(rollTotal)
{
case 2:
numTwo = numTwo + 1;
break;
  
case 3:
numThree = numThree + 1;
break;
  
case 4:
numFour = numFour + 1;
break;
  
case 5:
numFive = numFive + 1;
break;
  
case 6:
numSix = numSix + 1;
break;
  
case 7:
numSeven = numSeven + 1;
break;
  
case 8:
numEight = numEight + 1;
break;
  
case 9:
numNine = numNine + 1;
break;
  
case 10:
numTen = numTen + 1;
break;
  
case 11:
numEleven = numEleven + 1;
break;
  
case 12:
numTwelve = numTwelve+ 1;
break;
  
}
}
}
else{
System.out.println("Invalid rolls. Try again.");
}
}while(numRolls>1);

// Print statistics on dice rolls

System.out.println();
System.out.println();

for(int i=2;i<=12;i++)
{
System.out.print(i+"s: ");
switch(i)
{
case 2:
while(numTwo>0)
{
System.out.print("*");
numTwo--;
}
System.out.println();
break;
  
case 3:
while(numThree>0)
{
System.out.print("*");
numThree--;
}
System.out.println();
break;
  
case 4:
while(numFour>0)
{
System.out.print("*");
numFour--;
}
System.out.println();
break;
  
case 5:
while(numFive>0)
{
System.out.print("*");
numFive--;
}
System.out.println();
break;
  
case 6:
while(numSix>0)
{
System.out.print("*");
numSix--;
}
System.out.println();
break;
  
case 7:
while(numSeven>0)
{
System.out.print("*");
numSeven--;
}
System.out.println();
break;
  
case 8:
while(numEight>0)
{
System.out.print("*");
numEight--;
}
System.out.println();
break;
  
case 9:
while(numNine>0)
{
System.out.print("*");
numNine--;
}
System.out.println();
break;
  
case 10:
while(numTen>0)
{
System.out.print("*");
numTen--;
}
System.out.println();
break;
  
case 11:
while(numEleven>0)
{
System.out.print("*");
numEleven--;
}
System.out.println();
break;
  
case 12:
while(numTwelve>0)
{
System.out.print("*");
numTwelve--;
}
System.out.println();
break;
}
}


return;
}
}

Output:


Related Solutions

1. Amy rolls 15 8-sided dice. What is the probability that at least 2 of the...
1. Amy rolls 15 8-sided dice. What is the probability that at least 2 of the rolls are 5s? Answer: 0.5759 2. Amy shoots 10 arrows at a target. Each arrow hits the target (independently) with probability 0.3. What is the probability that at least 3 of the arrows hit the target? Answer: 0.6172 3. Amy shoots 64000 arrows at a target. Each arrow hits the target (independently) with probability 0.3. What is the probability that more than 2 of...
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls...
Consider rolling two 6-sided dice. What is the probability that at least two of the rolls have a sum that exceeds 6? at least 7 of the rolls have a sum that is even? exactly three rolls have a sum that equals 5?
Rolling a dice 15 times, find the probability of 6 consecutive rolls have 6 distinct numbers....
Rolling a dice 15 times, find the probability of 6 consecutive rolls have 6 distinct numbers. (i.e. 423123456..., 45612335626..., etc.)
Find/discover an example of statistics & probability in the news to discuss the following statement that...
Find/discover an example of statistics & probability in the news to discuss the following statement that represents one of the objectives of statistics analysis: "Statistics and Probability help us make decisions based on data analysis." Briefly discuss how the news item or article meets this objective. Cite your references. Also, keep in mind and discuss how the impact of your study on your patients or staff might differ if you found it in a journal.
Probability is the link between Descriptive Statistics and Inferential Statistics. So get out your dice, coins,...
Probability is the link between Descriptive Statistics and Inferential Statistics. So get out your dice, coins, and cards. To get started, what is the probability of getting either a king or a red card if I pick a card at random from a standard deck of cards? Also, let's say that a pro basketball player is a career 80% free throw shooter. Suppose the player attempts 4 free throws in a game. What is the probability that the player will...
What is the Common Rule in probability and statistics and why does it exist in the...
What is the Common Rule in probability and statistics and why does it exist in the United States? just need second thought will give great comments and feedback (:
Q\ Suppose we have dice; the dice have six outcomes find the probability of the following:...
Q\ Suppose we have dice; the dice have six outcomes find the probability of the following: - a) What’s the probability of rolling a 4? b) What’s the probability of rolling an odd number? c) What’s the probability of rolling less than 6? d) What’s the probability of rolling a 1 and 3? e) What’s the probability of rolling a 0? f) The probability of event a or c? g) What’s the probability of rolling not event b?
Two dice are rolled. Determine the probability of the following. Rolling an even number or a...
Two dice are rolled. Determine the probability of the following. Rolling an even number or a number greater than 10
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability...
Rolling a pair of 12 sided dice and summing the numbers find the following: The probability distribution of the sums. Sums= P(Sums)
The normal distribution or bell-shaped curve from statistics provides an example of a continuous probability distribution...
The normal distribution or bell-shaped curve from statistics provides an example of a continuous probability distribution curve. While calculating the probability of the occurrence of an event, we find that the area under the curve between the desired range of profitability values is 0.438. What does this mean? A. The probability of the occurrence of the event is 56.2 percent. B. The probability of the occurrence of the event is 0.438 percent. C. The probability of the occurrence of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT