Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Modify the program below to print a histogram in which the total number of times the dice rolls equals each possible value is displayed by

Modify the program below to print 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. Two dices will be used in each roll.

I have been trying for hours but I could not figure out the answer to this question

Thanks.

Example:

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

Dice roll statistics (result varies): 2s: ###### 3s: #### 4s: ### 5s: ######## 6s: ################### 7s: ############# 8s: ############# 9s: ############## 10s: ########### 11s: ##### 12s: ####

Note:

Create a variable name seedVal and assign the value 11.

Call the Math.setSeed( ) method to set the seed for the Random number generator object randGen.

Declare the variables to track the number found for each dice roll.

For each dice roll, add up the occurrences for each number found. Use either if case or switch case (recommended) method.

Print the statistics similar to the example shown above. Use if else method.

Original

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(); // FIXME 1 and 2: Set the seed to the Random number generator int i = 0; // Loop counter iterates numRolls times int numRolls = 0; // User defined number of rolls // FIXME 3: Declare and initiate cariables needed int numSixes = 0; // Tracks number of 6s found int numSevens = 0; // Tracks number of 7s found int die1 = 0; // Dice 1 values int die2 = 0; // Dice 2 values int rollTotal = 0; // Sum of dice values

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;

// FIXME 4: Count number of sixs and sevens; complete the same for all other possible values if (rollTotal == 6) { numSixes = numSixes + 1; } if (rollTotal == 7) { numSevens = numSevens + 1; } System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + "+" + die2 + ")"); }

// Print statistics on dice rolls System.out.println(" Dice roll statistics:"); // FIXME 5: Complete printing the histogram System.out.println("6s: " + numSixes); System.out.println("7s: " + numSevens); } else { System.out.println("Invalid rolls. Try again."); }

return; } }

My Version

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 seedVal = 11;

randGen.setSeed(seedVal);

// FIXME 1 and 2: Set the seed to the Random number generator

int i = 0; // Loop counter iterates numRolls times int numRolls = 0; // User defined number of rolls

// FIXME 3: Declare and initiate cariables needed

int[] numValues=new int[12]; int die1 = 0; // Dice 1 values int die2 = 0; // Dice 2 values int rollTotal = 0; // Sum of dice values

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;

// FIXME 4: Count number of sixs and sevens; complete the same for all other possible values numValues[rollTotal]++; System.out.println("Debugging: Roll " + (i+1) + " is " + rollTotal + " (" + die1 + "+" + die2 + ")"); }

// Print statistics on dice rolls System.out.println(" Dice roll statistics:");

// FIXME 5: Complete printing the histogram for(int i=2;i<=12;i++) { System.out.print(i+"s: "); for(int j=0;j { System.out.print("#"); } System.out.println(); } else { System.out.println("Invalid rolls. Try again."); }

return; } }

Error My Version is Getting

DiceStats.java:60: error: 'else' without 'if'

else {

^

DiceStats.java:66: error: reached end of file while parsing

}

^

2 errors

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

Step: 3

blur-text-image

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

Database 101

Authors: Guy Kawasaki

1st Edition

0938151525, 978-0938151524

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago