Source of GradeBook.java


  1: // Fig. 5.9: GradeBook.java
  2: // GradeBook class uses switch statement to count A, B, C, D and F grades.
  3: import java.util.Scanner; // program uses class Scanner
  4: 
  5: public class GradeBook 
  6: {
  7:    private String courseName; // name of course this GradeBook represents
  8:    private int total; // sum of grades
  9:    private int gradeCounter; // number of grades entered
 10:    private int aCount; // count of A grades
 11:    private int bCount; // count of B grades
 12:    private int cCount; // count of C grades
 13:    private int dCount; // count of D grades
 14:    private int fCount; // count of F grades
 15:    
 16:    // constructor initializes courseName; 
 17:    // int instance variables are initialized to 0 by default
 18:    public GradeBook( String name )
 19:    {
 20:       courseName = name; // initializes courseName
 21:    } // end constructor
 22: 
 23:    // method to set the course name
 24:    public void setCourseName( String name )
 25:    {
 26:       courseName = name; // store the course name
 27:    } // end method setCourseName
 28: 
 29:    // method to retrieve the course name
 30:    public String getCourseName()
 31:    {
 32:       return courseName;
 33:    } // end method getCourseName
 34: 
 35:    // display a welcome message to the GradeBook user
 36:    public void displayMessage()
 37:    {
 38:       // getCourseName gets the name of the course
 39:       System.out.printf( "Welcome to the grade book for\n%s!\n\n", 
 40:          getCourseName() );
 41:    } // end method displayMessage
 42: 
 43:    // input arbitrary number of grades from user
 44:    public void inputGrades()
 45:    {
 46:       Scanner input = new Scanner( System.in );
 47: 
 48:       int grade; // grade entered by user
 49: 
 50:       System.out.printf( "%s\n%s\n   %s\n   %s\n", 
 51:          "Enter the integer grades in the range 0-100.", 
 52:          "Type the end-of-file indicator to terminate input:", 
 53:          "On UNIX/Linux/Mac OS X type <ctrl> d then press Enter",
 54:          "On Windows type <ctrl> z then press Enter" );
 55: 
 56:       // loop until user enters the end-of-file indicator
 57:       while ( input.hasNext() ) 
 58:       {
 59:          grade = input.nextInt(); // read grade
 60:          total += grade; // add grade to total
 61:          ++gradeCounter; // increment number of grades
 62:          
 63:          // call method to increment appropriate counter
 64:          incrementLetterGradeCounter( grade );
 65:       } // end while 
 66:    } // end method inputGrades
 67: 
 68:    // add 1 to appropriate counter for specified grade
 69:    public void incrementLetterGradeCounter( int grade )
 70:    {
 71:       // determine which grade was entered
 72:       switch ( grade / 10 )
 73:       {  
 74:          case 9:  // grade was between 90
 75:          case 10: // and 100 
 76:             ++aCount; // increment aCount
 77:             break; // necessary to exit switch
 78: 
 79:          case 8: // grade was between 80 and 89
 80:             ++bCount; // increment bCount    
 81:             break; // exit switch
 82: 
 83:          case 7: // grade was between 70 and 79
 84:             ++cCount; // increment cCount    
 85:             break; // exit switch
 86: 
 87:          case 6: // grade was between 60 and 69
 88:             ++dCount; // increment dCount    
 89:             break; // exit switch
 90: 
 91:          default: // grade was less than 60
 92:             ++fCount; // increment fCount    
 93:             break; // optional; will exit switch anyway
 94:       } // end switch
 95:    } // end method incrementLetterGradeCounter
 96: 
 97:    // display a report based on the grades entered by user 
 98:    public void displayGradeReport()
 99:    {
100:       System.out.println( "\nGrade Report:" );
101: 
102:       // if user entered at least one grade...
103:       if ( gradeCounter != 0 ) 
104:       {
105:          // calculate average of all grades entered
106:          double average = (double) total / gradeCounter;  
107: 
108:          // output summary of results
109:          System.out.printf( "Total of the %d grades entered is %d\n", 
110:             gradeCounter, total );
111:          System.out.printf( "Class average is %.2f\n", average );
112:          System.out.printf( "%s\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n", 
113:             "Number of students who received each grade:", 
114:             "A: ", aCount,   // display number of A grades
115:             "B: ", bCount,   // display number of B grades
116:             "C: ", cCount,   // display number of C grades 
117:             "D: ", dCount,   // display number of D grades
118:             "F: ", fCount ); // display number of F grades
119:       } // end if
120:       else // no grades were entered, so output appropriate message
121:          System.out.println( "No grades were entered" );
122:    } // end method displayGradeReport
123: } // end class GradeBook
124: 
125: 
126: /**************************************************************************
127:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
128:  * Pearson Education, Inc. All Rights Reserved.                           *
129:  *                                                                        *
130:  * DISCLAIMER: The authors and publisher of this book have used their     *
131:  * best efforts in preparing the book. These efforts include the          *
132:  * development, research, and testing of the theories and programs        *
133:  * to determine their effectiveness. The authors and publisher make       *
134:  * no warranty of any kind, expressed or implied, with regard to these    *
135:  * programs or to the documentation contained in these books. The authors *
136:  * and publisher shall not be liable in any event for incidental or       *
137:  * consequential damages in connection with, or arising out of, the       *
138:  * furnishing, performance, or use of these programs.                     *
139:  *************************************************************************/