public class GradeBook
1: // Fig. 7.18: GradeBook.java
2: // Grade book using two-dimensional array to store grades.
3:
4: public class GradeBook
5: {
6: private String courseName; // name of course this grade book represents
7: private int grades[][]; // two-dimensional 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 methods getMinimum and getMaximum
43: System.out.printf( "\n%s %d\n%s %d\n\n",
44: "Lowest grade in the grade book is", getMinimum(),
45: "Highest grade in the grade book is", getMaximum() );
46:
47: // output grade distribution chart of all grades on all tests
48: outputBarChart();
49: } // end method processGrades
50:
51: // find minimum grade
52: public int getMinimum()
53: {
54: // assume first element of grades array is smallest
55: int lowGrade = grades[ 0 ][ 0 ];
56:
57: // loop through rows of grades array
58: for ( int studentGrades[] : grades )
59: {
60: // loop through columns of current row
61: for ( int grade : studentGrades )
62: {
63: // if grade less than lowGrade, assign it to lowGrade
64: if ( grade < lowGrade )
65: lowGrade = grade;
66: } // end inner for
67: } // end outer for
68:
69: return lowGrade; // return lowest grade
70: } // end method getMinimum
71:
72: // find maximum grade
73: public int getMaximum()
74: {
75: // assume first element of grades array is largest
76: int highGrade = grades[ 0 ][ 0 ];
77:
78: // loop through rows of grades array
79: for ( int studentGrades[] : grades )
80: {
81: // loop through columns of current row
82: for ( int grade : studentGrades )
83: {
84: // if grade greater than highGrade, assign it to highGrade
85: if ( grade > highGrade )
86: highGrade = grade;
87: } // end inner for
88: } // end outer for
89:
90: return highGrade; // return highest grade
91: } // end method getMaximum
92:
93: // determine average grade for particular student (or set of grades)
94: public double getAverage( int setOfGrades[] )
95: {
96: int total = 0; // initialize total
97:
98: // sum grades for one student
99: for ( int grade : setOfGrades )
100: total += grade;
101:
102: // return average of grades
103: return (double) total / setOfGrades.length;
104: } // end method getAverage
105:
106: // output bar chart displaying overall grade distribution
107: public void outputBarChart()
108: {
109: System.out.println( "Overall grade distribution:" );
110:
111: // stores frequency of grades in each range of 10 grades
112: int frequency[] = new int[ 11 ];
113:
114: // for each grade in GradeBook, increment the appropriate frequency
115: for ( int studentGrades[] : grades )
116: {
117: for ( int grade : studentGrades )
118: ++frequency[ grade / 10 ];
119: } // end outer for
120:
121: // for each grade frequency, print bar in chart
122: for ( int count = 0; count < frequency.length; count++ )
123: {
124: // output bar label ( "00-09: ", ..., "90-99: ", "100: " )
125: if ( count == 10 )
126: System.out.printf( "%5d: ", 100 );
127: else
128: System.out.printf( "%02d-%02d: ",
129: count * 10, count * 10 + 9 );
130:
131: // print bar of asterisks
132: for ( int stars = 0; stars < frequency[ count ]; stars++ )
133: System.out.print( "*" );
134:
135: System.out.println(); // start a new line of output
136: } // end outer for
137: } // end method outputBarChart
138:
139: // output the contents of the grades array
140: public void outputGrades()
141: {
142: System.out.println( "The grades are:\n" );
143: System.out.print( " " ); // align column heads
144:
145: // create a column heading for each of the tests
146: for ( int test = 0; test < grades[ 0 ].length; test++ )
147: System.out.printf( "Test %d ", test + 1 );
148:
149: System.out.println( "Average" ); // student average column heading
150:
151: // create rows/columns of text representing array grades
152: for ( int student = 0; student < grades.length; student++ )
153: {
154: System.out.printf( "Student %2d", student + 1 );
155:
156: for ( int test : grades[ student ] ) // output student's grades
157: System.out.printf( "%8d", test );
158:
159: // call method getAverage to calculate student's average grade;
160: // pass row of grades as the argument to getAverage
161: double average = getAverage( grades[ student ] );
162: System.out.printf( "%9.2f\n", average );
163: } // end outer for
164: } // end method outputGrades
165: } // end class GradeBook
166:
167:
168: /**************************************************************************
169: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
170: * Pearson Education, Inc. All Rights Reserved. *
171: * *
172: * DISCLAIMER: The authors and publisher of this book have used their *
173: * best efforts in preparing the book. These efforts include the *
174: * development, research, and testing of the theories and programs *
175: * to determine their effectiveness. The authors and publisher make *
176: * no warranty of any kind, expressed or implied, with regard to these *
177: * programs or to the documentation contained in these books. The authors *
178: * and publisher shall not be liable in any event for incidental or *
179: * consequential damages in connection with, or arising out of, the *
180: * furnishing, performance, or use of these programs. *
181: *************************************************************************/