Source of Analysis.java


  1: // Fig. 4.12: Analysis.java
  2: // Analysis of examination results.
  3: import java.util.Scanner; // class uses class Scanner
  4: 
  5: public class Analysis 
  6: {
  7:    public void processExamResults() 
  8:    {
  9:       // create Scanner to obtain input from command window
 10:       Scanner input = new Scanner( System.in );
 11: 
 12:       // initializing variables in declarations
 13:       int passes = 0; // number of passes
 14:       int failures = 0; // number of failures
 15:       int studentCounter = 1; // student counter
 16:       int result; // one exam result (obtains value from user)
 17: 
 18:       // process 10 students using counter-controlled loop
 19:       while ( studentCounter <= 10 ) 
 20:       {
 21:          // prompt user for input and obtain value from user
 22:          System.out.print( "Enter result (1 = pass, 2 = fail): " );
 23:          result = input.nextInt();
 24: 
 25:          // if...else nested in while 
 26:          if ( result == 1 )          // if result 1,
 27:             passes = passes + 1;     // increment passes;            
 28:          else                        // else result is not 1, so
 29:             failures = failures + 1; // increment failures
 30: 
 31:          // increment studentCounter so loop eventually terminates
 32:          studentCounter = studentCounter + 1;  
 33:       } // end while
 34: 
 35:       // termination phase; prepare and display results
 36:       System.out.printf( "Passed: %d\nFailed: %d\n", passes, failures );
 37: 
 38:       // determine whether more than 8 students passed
 39:       if ( passes > 8 )
 40:          System.out.println( "Raise Tuition" );
 41:    } // end method processExamResults
 42: 
 43: } // end class Analysis
 44: 
 45: /**************************************************************************
 46:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 47:  * Pearson Education, Inc. All Rights Reserved.                           *
 48:  *                                                                        *
 49:  * DISCLAIMER: The authors and publisher of this book have used their     *
 50:  * best efforts in preparing the book. These efforts include the          *
 51:  * development, research, and testing of the theories and programs        *
 52:  * to determine their effectiveness. The authors and publisher make       *
 53:  * no warranty of any kind, expressed or implied, with regard to these    *
 54:  * programs or to the documentation contained in these books. The authors *
 55:  * and publisher shall not be liable in any event for incidental or       *
 56:  * consequential damages in connection with, or arising out of, the       *
 57:  * furnishing, performance, or use of these programs.                     *
 58:  *************************************************************************/