Source of BarChart.java


  1: // Fig. 7.6: BarChart.java
  2: // Bar chart printing program.
  3: 
  4: public class BarChart 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       int array[] = { 0, 0, 0, 0, 0, 0, 1, 2, 4, 2, 1 };
  9: 
 10:       System.out.println( "Grade distribution:" ); 
 11: 
 12:       // for each array element, output a bar of the chart
 13:       for ( int counter = 0; counter < array.length; counter++ ) 
 14:       {
 15:          // output bar label ( "00-09: ", ..., "90-99: ", "100: " )
 16:          if ( counter == 10 )
 17:             System.out.printf( "%5d: ", 100 ); 
 18:          else
 19:             System.out.printf( "%02d-%02d: ", 
 20:                counter * 10, counter * 10 + 9  ); 
 21:  
 22:          // print bar of asterisks
 23:          for ( int stars = 0; stars < array[ counter ]; stars++ )
 24:             System.out.print( "*" );
 25: 
 26:          System.out.println(); // start a new line of output
 27:       } // end outer for
 28:    } // end main
 29: } // end class BarChart
 30: 
 31: 
 32: 
 33: /**************************************************************************
 34:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 35:  * Pearson Education, Inc. All Rights Reserved.                           *
 36:  *                                                                        *
 37:  * DISCLAIMER: The authors and publisher of this book have used their     *
 38:  * best efforts in preparing the book. These efforts include the          *
 39:  * development, research, and testing of the theories and programs        *
 40:  * to determine their effectiveness. The authors and publisher make       *
 41:  * no warranty of any kind, expressed or implied, with regard to these    *
 42:  * programs or to the documentation contained in these books. The authors *
 43:  * and publisher shall not be liable in any event for incidental or       *
 44:  * consequential damages in connection with, or arising out of, the       *
 45:  * furnishing, performance, or use of these programs.                     *
 46:  *************************************************************************/