Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Chang from arrays to variables / * Here's another Switch example, this one from the book, but it's also a good example of the random

Chang from arrays to variables/* Here's another Switch example, this one
from the book, but it's also a good example
of the random number generator.
Fig. 6.7: RollDie.java
Roll a six-sided die a bunch of times. */
import java.security.SecureRandom;
public class Part178Random
{
public static void main(String[] args)
{
// randomNumbers object will produce secure random numbers
SecureRandom randomNumbers = new SecureRandom();
int frequency1=0; // count of 1s rolled
int frequency2=0; // count of 2s rolled
int frequency3=0; // count of 3s rolled
int frequency4=0; // count of 4s rolled
int frequency5=0; // count of 5s rolled
int frequency6=0; // count of 6s rolled
// tally counts for 6,000,000 rolls of a die
for (int roll =1; roll <=6000000; roll++)
{
int face =1+ randomNumbers.nextInt(6); // number from 1 to 6
// use face value 1-6 to determine which counter to increment
switch (face)
{
case 1:
++frequency1; // increment the 1s counter
break;
case 2:
++frequency2; // increment the 2s counter
break;
case 3:
++frequency3; // increment the 3s counter
break;
case 4:
++frequency4; // increment the 4s counter
break;
case 5:
++frequency5; // increment the 5s counter
break;
case 6:
++frequency6; // increment the 6s counter
break;
}
}
System.out.println("Face\tFrequency"); // output headers
System.out.printf("1\t%d%n2\t%d%n3\t%d%n4\t%d%n5\t%d%n6\t%d%n",
frequency1, frequency2, frequency3, frequency4,
frequency5, frequency6);
}
}

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

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions

Question

5. Describe how contexts affect listening

Answered: 1 week ago