/* * @topic T00845 Using Comparable interface to sort objects * @brief Main driver */ package hw7demo; import java.util.Arrays; import java.util.Scanner; public class MainApp { public static void main(String[] args) { int sideCount = 6; // To provide enough space for the range // [2, 32] // we need to add 1 to the number of array elements: // 0, 1, 2, 3, 4, ..., 32 (array size is 33) // 0 and 1 are waisted elements of the array DiceRoller roller = new DiceRoller(sideCount); int[] combinations = new int[1 + sideCount * 2]; for (;;) { Arrays.fill(combinations, 0); // ask the user for the number of rolls: int numberOfRolls = 100; while (numberOfRolls-- != 0) { int combination = roller.roll(); // register the roll in the stats: ++combinations[ combination]; } printCombinations( combinations ); // for debugging Stats[] stats = new Stats[ combinations.length - 2 ]; populateStats( stats, combinations ); Arrays.sort( stats ); printStats( stats ); System.out.println(); if ( askToContinue() == false ) { break; } }//forever }//main private static void printStats( Stats[] stats ) { System.out.println(); for ( Stats st : stats ) { System.out.print(st + " "); } } private static void populateStats( Stats[] stats, int[] combinations ) { for (int idx = 2; idx < combinations.length; ++idx ) { stats[ idx - 2 ] = new Stats( idx, combinations[idx] ); } } private static void printCombinations( int[] combinations ) { System.out.print("The stats are: "); for (int idx = 2; idx < combinations.length; ++idx ) { System.out.print(idx + ":" + combinations[idx] + " "); } } private static boolean askToContinue() { Scanner sc = new Scanner( System.in ); System.out.print( "Type x to exit: "); String str = sc.next(); if ( str.charAt(0) == 'x') { return false; } return true; } }//class MainApp