// Supplementary to "Software Design ...", John A Robinson, Newnes, 2004, import java.io.*; // Import the functions to do console i/o public class diceroll { // Everything is in a class definition in java // The class name is the same as the filename public static void main(String args[]) throws java.io.IOException // main works analogously to main in C++, but // has to be declared as shown within the class. // Because we're using standard input in a // console application, we might get IOExceptions. // Have to explicitly say we aren't going to // process them but throw them upwards { final int max_dice = 12; int dice[] = new int[max_dice]; // Arrays set up by new in Java. // Never have to delete because // Java does garbage collection. final int possible_outcomes = max_dice*6; int freq_counters[] = new int[possible_outcomes+1]; // Extra 1 is because outcomes will go from 1 to possible_outcomes, // not from 0 to possible_outcomes-1 int num_dice; int num_rolls; int roll_cnt; int i; // The following line illustrates output to the console: System.out.println("Enter number of dice (1 - " + max_dice + "): "); // To read input in a structured way, we're going to use a // BufferedReader object. To read raw input bytes, we'd use // System.in.read(). BufferedReader cin = new BufferedReader( new InputStreamReader(System.in)); // This lets us read in a line of text into a Java String: String temp = cin.readLine(); // Which we can then convert to an int num_dice = Integer.parseInt(temp); if ((num_dice < 1)||(num_dice > max_dice)) { System.out.println ("Sorry. Can only handle between 1 and " + max_dice + " dice"); return; } System.out.println("Enter number of rolls: "); temp = cin.readLine(); num_rolls = Integer.parseInt(temp); if (num_rolls < 1) { System.out.println("Cannot do less than 1 roll!"); return; } for (i = 0; i < possible_outcomes+1; i++) // Initialize range of cnts freq_counters[i] = 0; for (roll_cnt = 0; roll_cnt < num_rolls; roll_cnt++) { roll(dice, num_dice); int sum = 0; for (i = 0; i < num_dice; i++) sum += dice[i]; freq_counters[sum]++; } print_histogram(num_dice*6,freq_counters); } static int roll(int dice[], int num_dice) { for (int i = 0; i < num_dice; i++) { dice[i] = (int) (Math.random() * 6) + 1; // Math.random() generates a number in [0,1) } return 0; } static int print_histogram(int num_bins, int bin_array[]) { int max_height = 25; // Desired height of histogram int line_length = 79; // Length of a text line char outline[]; outline = new char[line_length+1]; int i; // First, clear outline for (i = 0; i < line_length; i++) outline[i] = ' '; outline[line_length] = 0; // Null termination // Find highest peak in histogram int height = -1; for (i = 0; i < num_bins; i++) { if (bin_array[i] > height) height = bin_array[i]; } // Now make the histogram fit in max_height: // On each iteration decrement the height threshold by // height_dec - set so histogram will occupy max_height lines int height_dec = height/max_height; if (height_dec < 1) height_dec = 1; while(height > 0) { for (i = 0; i <= num_bins; i++) { if (bin_array[i] >= height) outline[i] = '*'; } System.out.println(outline); height -= height_dec; } return 0; } } // Example run of the program: // (Distribution may appear skewed when printed with variable-width font) // // Enter number of dice (1 - 12): 8 // Enter number of rolls: 9999 // * // *** // ***** // ***** // ******* // ******* // ******* // ********* // ********* // ********* // *********** // *********** // *********** // ************* // ************* // ************* // *************** // *************** // *************** // ***************** // ***************** // ******************* // ********************* // ********************* // ************************* // ************************************ //