Source of RollDie.java


  1: // Fig. 6.8: RollDie.java
  2: // Roll a six-sided die 6000 times.
  3: import java.util.Random;
  4: 
  5: public class RollDie 
  6: {
  7:    public static void main( String args[] )
  8:    {
  9:       Random randomNumbers = new Random(); // random number generator
 10: 
 11:       int frequency1 = 0; // count of 1s rolled
 12:       int frequency2 = 0; // count of 2s rolled
 13:       int frequency3 = 0; // count of 3s rolled
 14:       int frequency4 = 0; // count of 4s rolled
 15:       int frequency5 = 0; // count of 5s rolled
 16:       int frequency6 = 0; // count of 6s rolled
 17: 
 18:       int face; // stores most recently rolled value
 19:    
 20:       // summarize results of 6000 rolls of a die
 21:       for ( int roll = 1; roll <= 6000; roll++ ) 
 22:       {
 23:          face = 1 + randomNumbers.nextInt( 6 ); // number from 1 to 6
 24:    
 25:          // determine roll value 1-6 and increment appropriate counter
 26:          switch ( face ) 
 27:          {   
 28:             case 1:
 29:                ++frequency1; // increment the 1s counter
 30:                break; 
 31:             case 2:
 32:                ++frequency2; // increment the 2s counter
 33:                break;
 34:             case 3:
 35:                ++frequency3; // increment the 3s counter
 36:                break;
 37:             case 4:
 38:                ++frequency4; // increment the 4s counter
 39:                break;
 40:             case 5:
 41:                ++frequency5; // increment the 5s counter
 42:                break;
 43:             case 6:
 44:                ++frequency6; // increment the 6s counter
 45:                break; // optional at end of switch
 46:          } // end switch
 47:       } // end for
 48: 
 49:       System.out.println( "Face\tFrequency" ); // output headers
 50:       System.out.printf( "1\t%d\n2\t%d\n3\t%d\n4\t%d\n5\t%d\n6\t%d\n",
 51:          frequency1, frequency2, frequency3, frequency4,
 52:          frequency5, frequency6 );
 53:    } // end main
 54: } // end class RollDie
 55: 
 56: /**************************************************************************
 57:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 58:  * Pearson Education, Inc. All Rights Reserved.                           *
 59:  *                                                                        *
 60:  * DISCLAIMER: The authors and publisher of this book have used their     *
 61:  * best efforts in preparing the book. These efforts include the          *
 62:  * development, research, and testing of the theories and programs        *
 63:  * to determine their effectiveness. The authors and publisher make       *
 64:  * no warranty of any kind, expressed or implied, with regard to these    *
 65:  * programs or to the documentation contained in these books. The authors *
 66:  * and publisher shall not be liable in any event for incidental or       *
 67:  * consequential damages in connection with, or arising out of, the       *
 68:  * furnishing, performance, or use of these programs.                     *
 69:  *************************************************************************/