public class GradeBook
1: // Fig. 4.6: GradeBook.java
2: // GradeBook class that solves class-average problem using
3: // counter-controlled repetition.
4: import java.util.Scanner; // program uses class Scanner
5:
6: public class GradeBook
7: {
8: private String courseName; // name of course this GradeBook represents
9:
10: // constructor initializes courseName
11: public GradeBook( String name )
12: {
13: courseName = name; // initializes courseName
14: } // end constructor
15:
16: // method to set the course name
17: public void setCourseName( String name )
18: {
19: courseName = name; // store the course name
20: } // end method setCourseName
21:
22: // method to retrieve the course name
23: public String getCourseName()
24: {
25: return courseName;
26: } // end method getCourseName
27:
28: // display a welcome message to the GradeBook user
29: public void displayMessage()
30: {
31: // getCourseName gets the name of the course
32: System.out.printf( "Welcome to the grade book for\n%s!\n\n",
33: getCourseName() );
34: } // end method displayMessage
35:
36: // determine class average based on 10 grades entered by user
37: public void determineClassAverage()
38: {
39: // create Scanner to obtain input from command window
40: Scanner input = new Scanner( System.in );
41:
42: int total; // sum of grades entered by user
43: int gradeCounter; // number of the grade to be entered next
44: int grade; // grade value entered by user
45: int average; // average of grades
46:
47: // initialization phase
48: total = 0; // initialize total
49: gradeCounter = 1; // initialize loop counter
50:
51: // processing phase
52: while ( gradeCounter <= 10 ) // loop 10 times
53: {
54: System.out.print( "Enter grade: " ); // prompt
55: grade = input.nextInt(); // read grade from user
56: total = total + grade; // add grade to total
57: gradeCounter = gradeCounter + 1; // increment counter by 1
58: } // end while
59:
60: // termination phase
61: average = total / 10; // integer division yields integer result
62:
63: // display total and average of grades
64: System.out.printf( "\nTotal of all 10 grades is %d\n", total );
65: System.out.printf( "Class average is %d\n", average );
66: } // end method determineClassAverage
67:
68: } // end class GradeBook
69:
70: /**************************************************************************
71: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
72: * Pearson Education, Inc. All Rights Reserved. *
73: * *
74: * DISCLAIMER: The authors and publisher of this book have used their *
75: * best efforts in preparing the book. These efforts include the *
76: * development, research, and testing of the theories and programs *
77: * to determine their effectiveness. The authors and publisher make *
78: * no warranty of any kind, expressed or implied, with regard to these *
79: * programs or to the documentation contained in these books. The authors *
80: * and publisher shall not be liable in any event for incidental or *
81: * consequential damages in connection with, or arising out of, the *
82: * furnishing, performance, or use of these programs. *
83: *************************************************************************/