Case Study: Class GradeBook Using an Array to Store Grades
7.8 Case Study: Class GradeBook Using an Array to Store Grades
This section further evolves class GradeBook , introduced in Chapter 3 and expanded in Chapters 4–5. Recall that this class represents a grade book used by a professor to store and
analyze a set of student grades. Previous versions of the class process a set of grades entered by the user, but do not maintain the individual grade values in instance variables of the
class. Thus, repeat calculations require the user to reenter the same grades. One way to solve this problem would be to store each grade entered in an individual instance of the
class. For example, we could create instance variables grade1 , grade2 , …, grade10 in class GradeBook to store 10 student grades. However, the code to total the grades and determine the class average would be cumbersome, and the class would not be able to process any more than 10 grades at a time. In this section, we solve this problem by storing grades in an array.
Storing Student Grades in an Array in Class GradeBook The version of class GradeBook (Fig. 7.14) presented here uses an array of integers to store the grades of several students on a single exam. This eliminates the need to repeatedly in- put the same set of grades. Array grades is declared as an instance variable in line 7— therefore, each GradeBook object maintains its own set of grades. The class’s constructor (lines 10–14) has two parameters—the name of the course and an array of grades. When an application (e.g., class GradeBookTest in Fig. 7.15) creates a GradeBook object, the ap- plication passes an existing int array to the constructor, which assigns the array’s reference to instance variable grades (line 13). The size of the array grades is determined by the class that passes the array to the constructor. Thus, a GradeBook object can process a vari- able number of grades. The grade values in the passed array could have been input from a user or read from a file on disk (as discussed in Chapter 14). In our test application, we simply initialize an array with a set of grade values (Fig. 7.15, line 10). Once the grades are stored in instance variable grades of class GradeBook , all the class’s methods can access the elements of grades as often as needed to perform various calculations.
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
Fig. 7.14 | GradeBook class using an array to store test grades. (Part 1 of 4.)
318 Chapter 7 Arrays
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
Fig. 7.14 | GradeBook class using an array to store test grades. (Part 2 of 4.)
7.8 Case Study: Class GradeBook Using an Array to Store Grades
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()
System.out.println( "Grade distribution:" ); 102 103
// stores frequency of grades in each range of 10 grades 104
int frequency[] = new int[ 11 ];
// for each grade, increment the appropriate frequency 107
for ( int grade : grades ) 108
++frequency[ grade / 10 ];
// for each grade frequency, print bar in chart 111
for ( int count = 0 ; count < frequency.length; count++ )
// output bar label ( "00-09: ", ..., "90-99: ", "100: " ) 114
if ( count == 10 )
System.out.printf( "%5d: " , 100 );
116 else 117
System.out.printf( "%02d-%02d: " ,
count * 10 , count * 10 + 9 );
Fig. 7.14 | GradeBook class using an array to store test grades. (Part 3 of 4.)
320 Chapter 7 Arrays
120 // print bar of asterisks 121
for ( int stars = 0 ; stars < frequency[ count ]; stars++ )
System.out.print( "*" );
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
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
Fig. 7.14 | GradeBook class using an array to store test grades. (Part 4 of 4.) Method processGrades (lines 37–51) contains a series of method calls that output a
report summarizing the grades. Line 40 calls method outputGrades to print the contents of the array grades . Lines 134–136 in method outputGrades use a for statement to output the students’ grades. A counter-controlled for must be used in this case, because lines 135–136 use counter variable student ’s value to output each grade next to a partic- ular student number (see Fig. 7.15). Although array indices start at 0, a professor would typically number students starting at 1. Thus, lines 135–136 output student + 1 as the student number to produce grade labels "Student 1: " , "Student 2: " , and so on.
Method processGrades next calls method getAverage (line 43) to obtain the average of the grades in the array. Method getAverage (lines 86–96) uses an enhanced for state- ment to total the values in array grades before calculating the average. The parameter in the enhanced for ’s header (e.g., int grade ) indicates that for each iteration, the int vari- able grade takes on a value in the array grades . Note that the averaging calculation in line
95 uses grades.length to determine the number of grades being averaged. Lines 46–47 in method processGrades calls methods getMinimum and getMaximum to determine the lowest and highest grades of any student on the exam, respectively. Each of these methods uses an enhanced for statement to loop through array grades . Lines 59–
64 in method getMinimum loop through the array. Lines 62–63 compare each grade to lowGrade ; if a grade is less than lowGrade , lowGrade is set to that grade. When line 66 exe- cutes, lowGrade contains the lowest grade in the array. Method getMaximum (lines 70–83) works similarly to method getMinimum .
Finally, line 50 in method processGrades calls method outputBarChart to print a distribution chart of the grade data using a technique similar to that in Fig. 7.6. In that example, we manually calculated the number of grades in each category (i.e., 0–9, 10–19, …, 90–99 and 100) by simply looking at a set of grades. In this example, lines 107–108 use a technique similar to that in Fig. 7.7 and Fig. 7.8 to calculate the frequency of grades in each category. Line 104 declares and creates array frequency of 11 int s to store the fre-
7.8 Case Study: Class GradeBook Using an Array to Store Grades
quency of grades in each grade category. For each grade in array grades , lines 107–108 increment the appropriate element of the frequency array. To determine which element to increment, line 108 divides the current grade by 10 using integer division. For example,
if grade is 85 , line 108 increments frequency[ 8 ] to update the count of grades in the range 80–89. Lines 111–125 next print the bar chart (see Fig. 7.15) based on the values in array frequency . Like lines 23–24 of Fig. 7.6, lines 121–122 of Fig. 7.14 use a value in array frequency to determine the number of asterisks to display in each bar.
Class GradeBookTest That Demonstrates Class GradeBook The application of Fig. 7.15 creates an object of class GradeBook (Fig. 7.14) using the int array gradesArray (declared and initialized in line 10). Lines 12–13 pass a course name and gradesArray to the GradeBook constructor. Line 14 displays a welcome message, and
1 // Fig. 7.15: GradeBookTest.java 2 // Creates GradeBook object using an array of grades.
3 4 public class GradeBookTest 5 {
6 // main method begins program execution 7 public static void main( String args[] ) 8 {
9 // array of student grades
10 int gradesArray[] = { 87 , 68 , 94 , 100 , 83 , 78 , 85 , 91 , 76 , 87 };
11 12 GradeBook myGradeBook = new GradeBook(
13 "CS101 Introduction to Java Programming" , gradesArray );
14 myGradeBook.displayMessage(); 15 myGradeBook.processGrades();
16 } // end main 17 } // end class GradeBookTest
Welcome to the grade book for CS101 Introduction to Java Programming!
The grades are: Student 1: 87
Student 2: 68 Student 3: 94 Student 4: 100 Student 5: 83 Student 6: 78 Student 7: 85 Student 8: 91 Student 9: 76 Student 10: 87
(continued…)
Fig. 7.15 | GradeBookTest creates a GradeBook object using an array of grades, then invokes method processGrades to analyze them. (Part 1 of 2.)
322 Chapter 7 Arrays
Class average is 84.90 Lowest grade is 68 Highest grade is 100
Grade distribution: 00-09: 10-19: 20-29: 30-39: 40-49: 50-59: 60-69: * 70-79: ** 80-89: **** 90-99: **
Fig. 7.15 | GradeBookTest creates a GradeBook object using an array of grades, then invokes method processGrades to analyze them. (Part 2 of 2.)
line 15 invokes the GradeBook object’s processGrades method. The output summarizes the 10 grades in myGradeBook .
Software Engineering Observation 7.1
A test harness (or test application) is responsible for creating an object of the class being tested and providing it with data. This data could come from any of several sources. Test data can be placed directly into an array with an array initializer, it can come from the user at the keyboard, it can come from a file (as you will see in Chapter 14), or it can come from a network (as you will see in Chapter 24). After passing this data to the class’s constructor to instantiate the object, the test harness should call upon the object to test its methods and manipulate its data. Gathering data in the test harness like this allows the class to manipulate data from several sources.