public class GradeBook
1: // Fig. 7.14: GradeBook.java
2: // Grade book using an array to store test grades.
3:
4: public class GradeBook
5: {
6: private String courseName; // name of course this GradeBook represents
7: private int grades[]; // array of student grades
8:
9: // two-argument constructor initializes courseName and grades array
10: public GradeBook( String name, int gradesArray[] )
11: {
12: courseName = name; // initialize courseName
13: grades = gradesArray; // store grades
14: } // end two-argument GradeBook 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: // perform various operations on the data
37: public void processGrades()
38: {
39: // output grades array
40: outputGrades();
41:
42: // call method getAverage to calculate the average grade
43: System.out.printf( "\nClass average is %.2f\n", getAverage() );
44:
45: // call methods getMinimum and getMaximum
46: System.out.printf( "Lowest grade is %d\nHighest grade is %d\n\n",
47: getMinimum(), getMaximum() );
48:
49: // call outputBarChart to print grade distribution chart
50: outputBarChart();
51: } // end method processGrades
52:
53: // find minimum grade
54: public int getMinimum()
55: {
56: int lowGrade = grades[ 0 ]; // assume grades[ 0 ] is smallest
57:
58: // loop through grades array
59: for ( int grade : grades )
60: {
61: // if grade lower than lowGrade, assign it to lowGrade
62: if ( grade < lowGrade )
63: lowGrade = grade; // new lowest grade
64: } // end for
65:
66: return lowGrade; // return lowest grade
67: } // end method getMinimum
68:
69: // find maximum grade
70: public int getMaximum()
71: {
72: int highGrade = grades[ 0 ]; // assume grades[ 0 ] is largest
73:
74: // loop through grades array
75: for ( int grade : grades )
76: {
77: // if grade greater than highGrade, assign it to highGrade
78: if ( grade > highGrade )
79: highGrade = grade; // new highest grade
80: } // end for
81:
82: return highGrade; // return highest grade
83: } // end method getMaximum
84:
85: // determine average grade for test
86: public double getAverage()
87: {
88: int total = 0; // initialize total
89:
90: // sum grades for one student
91: for ( int grade : grades )
92: total += grade;
93:
94: // return average of grades
95: return (double) total / grades.length;
96: } // end method getAverage
97:
98: // output bar chart displaying grade distribution
99: public void outputBarChart()
100: {
101: System.out.println( "Grade distribution:" );
102:
103: // stores frequency of grades in each range of 10 grades
104: int frequency[] = new int[ 11 ];
105:
106: // for each grade, increment the appropriate frequency
107: for ( int grade : grades )
108: ++frequency[ grade / 10 ];
109:
110: // for each grade frequency, print bar in chart
111: for ( int count = 0; count < frequency.length; count++ )
112: {
113: // output bar label ( "00-09: ", ..., "90-99: ", "100: " )
114: if ( count == 10 )
115: System.out.printf( "%5d: ", 100 );
116: else
117: System.out.printf( "%02d-%02d: ",
118: count * 10, count * 10 + 9 );
119:
120: // print bar of asterisks
121: for ( int stars = 0; stars < frequency[ count ]; stars++ )
122: System.out.print( "*" );
123:
124: System.out.println(); // start a new line of output
125: } // end outer for
126: } // end method outputBarChart
127:
128: // output the contents of the grades array
129: public void outputGrades()
130: {
131: System.out.println( "The grades are:\n" );
132:
133: // output each student's grade
134: for ( int student = 0; student < grades.length; student++ )
135: System.out.printf( "Student %2d: %3d\n",
136: student + 1, grades[ student ] );
137: } // end method outputGrades
138: } // end class GradeBook
139:
140:
141: /**************************************************************************
142: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
143: * Pearson Education, Inc. All Rights Reserved. *
144: * *
145: * DISCLAIMER: The authors and publisher of this book have used their *
146: * best efforts in preparing the book. These efforts include the *
147: * development, research, and testing of the theories and programs *
148: * to determine their effectiveness. The authors and publisher make *
149: * no warranty of any kind, expressed or implied, with regard to these *
150: * programs or to the documentation contained in these books. The authors *
151: * and publisher shall not be liable in any event for incidental or *
152: * consequential damages in connection with, or arising out of, the *
153: * furnishing, performance, or use of these programs. *
154: *************************************************************************/