In this project, we will write a program that simulates the rolling of dice, performs many rolls, and determines the frequency for each possible sum of the dice. We will create a kind of bar graph called a histogram and display it in two forms - one horizontal and one vertical. This project will require you to write several loops that process the integer variables in an array - as well as how to use a method (even if you do not understand it's inner workings).
You can find an initial version of the Histogram.java and HistogramCLI.java source files in project2.zip. As always, download and unzip this file on your removable media (or your own computer). You may find it helpful to create/save a Dr Java project file in the project2 directory.
How this program will work is as follows:
- Prompts user for how many dice to roll (1 to 3).
- Prompts user for how many sides each die should have (2 to 9).
- Asks user how many times to roll the dice (1000 to 2 billion).
- Rolls the dice that number of times.
- Creates a Histogram object and determine how tall each bar should be.
- Calls the Histogram object's methods for drawing the histogram, both horizontal and vertical. (Check out the sample outputbelow, to see how this might look.)
For this project, you will be working in both classes, writing the code that enables the program to work. The individual steps are in the comments in the files, but I'll also go over them briefly here:
- Declare your variables. (Details in code comments)
- Set up one do-while loop, that will encompass three others, along with the majority of the code in the main method. It will allow repeated execution of the rest of the program, until the user indicates to stop. That outermost do-while loop will encompass steps 3-10 below.
- Prompt the user for how many how many dice to roll (1 to 3), but only allow a number within the specified range.
- Prompt the user for how many sides each die should have (2 to 9), but only allow a number within the specified range.
- Prompt the user for how many times the dice should be rolled (1000 to 2 billion), but only allow a number within the specified range.
- Set your counting array to zero, using a loop.
- Calculate the minimum and maximum indices, and initialize the "counts" array. The minimum index is the smallest possible sum of the dice, while the maximum index is the largest possible sum.
- Start rolling the dice. After each roll, increment the element of the "counts" array at the index corresponding to the sum.
- Construct the Histogram object and call it's drawing methods. Study the Histogram class so that you are familiar with it's constructor and other methods, know how they work, and know what they return (or don't return).
- Ask if the user wants to do it again. Don't forget to close your outer loop and test the condition, specified in the code comments.In Histogram.java...
- We may have very large values in the values array supplied as a parameter to the constructor. Since we want to limit the size of the histogram bar graph to maxLength, we need to scale the data in the values array when we copy it into the instance copy of the data in the array. This consists of two loops. The first loop finds the largest value in the values array. (Declare, initialize, and use a variable named something like maxValue).
- The second loop multiplies each value in the values array by the max length we want for bars (maxLength) and divides by the largest value found (maxValue). Remember the limitations of multiplication and division for integer variables. You may need to cast the integer values to double for the calculation and cast the result back to integer to update the value in each counts array element with good "resolution". You can also get good resolution by performing integer multiplications and divisions in the correct order.
- In the drawHor method, your code must draw a horizontal bar graph of the data in the values array. (See sample output) This will require two nested loops. The outer loop will go through each element of the values array, and the inner loop will print an asterisk from 1 through the value in the outer loop element of the values array. You may also want to print the values array integer at the end of each bar of asterisks, even though this is not in the sample output.
- In the drawVer method, your code must draw a vertical bar graph of the data in the values array. (See sample output) This is a little trickier than the horizontal bar graph. You still need two nested loops. The outer loop will count down through all the values from maxLength to 1 to print each line. The inner loop will print a piece of each line for each value (index into the values array). If the count for the value is greater than or equal to the decreasing count of the outer loop, print an asterisk with two spaces after it. If the count for the value is not greater than or equal to the value of the outer loop count, print an equivalent number of spaces to maintain the column alignment but not show the "bar" in this column. For the last line of output, print the numbers from minIndex up to maxIndex; for each number, print that number followed by two spaces (if the number is less than 10) or one space (if the number is greater than or equal to 10).
To make sure your program is working correctly, try it out with different numbers of dice, different numbers of sides per die, and different numbers of rolls. Remember, more dice and more rolls will slow down the rolling portion of the program.