Task: Write a program called DiceStats which computes and displays statistics on
ID: 3725286 • Letter: T
Question
Task: Write a program called DiceStats which computes and displays statistics on the frequency of sums obtained when rolling a pair of dice, as described below.
Steps: Your program must contain the following elements:
A subroutine called printHashes which takes an integer as a parameter and prints that many # symbols on a line, ending with a new line.
A subroutine called printHistogram which takes an array of integers and two additional integers (called low and high) as parameters, and, for each slot between low (inclusive) and high (exclusive), prints out the number of the slot followed by a number of hashes determined by the value stored in the array at that slot. For example, if the array contains the seven values 1 8 0 3 2 10 7, low is 1, and high is 5, then printHistogram should produce:
A function called generateStats which takes the number of sides on a die and the number of times to roll as parameters, rolls a pair of dice with the specified number of sides the specified number of times, and returns an array where slot i of the array contains the number of times the sum i was rolled.
A main program which rolls a pair of 6-sided dice 400 times and prints out a histogram showing how often each sum was rolled.
Make use of the various subroutines and functions where you can - printHistogram should use printHashes to print the hashes for each slot of the array and the main program should use all three subroutines/functions.
Explanation / Answer
package January;
import java.util.Random;
// Class DiceStats definition
public class DiceStats
{
// Method to print '#'symbol
void printHashes(int no)
{
// Loops no times and displays '#' symbol
for(int x = 0; x < no; x++)
System.out.print("#");
System.out.println();
}// End of method
// Method to print histogram based on the value of the array index position from low (inclusive)to high (exclusive)
void printHistogram(int arr[], int low, int high)
{
// Loops from low to high
for(int x = low; x < high; x++)
{
// Display the number
System.out.print(x + ": ");
// Calls the method to print '#' symbol number of times based on arr[x] value
printHashes(arr[x]);
}// End of for loop
}// End of method
// Method to generate random number
void generateStats(int arr[], int sides, int numberOfTimes)
{
// Random number object created
Random rnd = new Random();
// Stores dice low value
int low = 1;
// Stores dice high value based on the given parameter as side
int high = sides;
// Loops till the value of numberOfTimes
for(int x = 0; x < numberOfTimes; x++)
{
// Generates two random numbers between low and high
int roll1 = rnd.nextInt(high - low) + low;
int roll2 = rnd.nextInt(high - low) + low;
// Calculates sum and stores it in array x index position
arr[x] = roll1 + roll2;
}// End of for loop
}// End of method
// main method definition
public static void main(String[] args)
{
// Creates an array of size 400
int arr[] = new int[400];
// Sets the low and high value
int low = 1, high = 8;
// Class DiceStats object declared
DiceStats ds = new DiceStats();
// Calls the method to generate random number with 6 side dice 400 times
ds.generateStats(arr, 6, 400);
// Calls the method to print histogram between low and high
ds.printHistogram(arr, low, high);
}// End of method
}// End of method
Sample Output:
1: ##########
2: ########
3: ##
4: ########
5: ######
6: ######
7: ###
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.