Case Study: Class GradeBook Using an Array to Store Grades

Case Study Class GradeBook Using an Array to Store Grades

This section further evolves class GradeBook, introduced in Chapter 4 and expanded in Chapters 56. Recall that this class represents a grade book used by an instructor 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 re-enter 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. 8.15) presented here uses an array of integers to store the grades of several students on a single exam. This eliminates the need to repeatedly input the same set of grades. Array grades is declared as an instance variable in line 8therefore, each GradeBook object maintains its own set of grades. The class's constructor (lines 1115) has two parametersthe name of the course and an array of grades. When an application (e.g., class GradeBookTest in Fig. 8.16) creates a GradeBook object, the application passes an existing int array to the constructor, which assigns the array's reference to instance variable grades (line 14). The size of array grades is determined by the class that passes the array to the constructor. Thus, a GradeBook object can process a variable number of gradesas many as are in the array in the caller. The grade values in the passed array could have been input from a user at the keyboard or read from a file on disk (as discussed in Chapter 18). In our test application, we simply initialize an array with a set of grade values (Fig. 8.16, line 9). Once the grades are stored in instance variable grades of class GradeBook, all the class's methods can access the elements of grades as needed to perform various calculations.

Figure 8.15. Grade book using an array to store test grades.

1 // Fig. 8.15: GradeBook.cs 2 // Grade book using an array to store test grades. 3 using System; 4 5 public class GradeBook 6 { 7 private string courseName; // name of course this GradeBook represents 8 private int[] grades; // array of student grades 9 10 // two-parameter constructor initializes courseName and grades array 11 public GradeBook( string name, int[] gradesArray ) 12 { 13 CourseName = name; // initialize courseName 14 grades = gradesArray; // initialize grades array 15 } // end two-parameter GradeBook constructor 16 17 // property that gets and sets the course name 18 public string CourseName 19 { 20 get 21 { 22 return courseName; 23 } // end get 24 set 25 { 26 courseName = value; 27 } // end set 28 } // end property CourseName 29 30 // display a welcome message to the GradeBook user 31 public void DisplayMessage() 32 { 33 // CourseName property gets the name of the course 34 Console.WriteLine( "Welcome to the grade book for {0}! ", 35 CourseName ); 36 } // end method DisplayMessage 37 38 // perform various operations on the data 39 public void ProcessGrades() 40 { 41 // output grades array 42 OutputGrades(); 43 44 // call method GetAverage to calculate the average grade 45 Console.WriteLine( " Class average is {0:F2}", GetAverage() ); 46 47 // call methods GetMinimum and GetMaximum 48 Console.WriteLine( "Lowest grade is {0} Highest grade is {1} ", 49 GetMinimum(), GetMaximum() ); 50 51 // call OutputBarChart to print grade distribution chart 52 OutputBarChart(); 53 } // end method ProcessGrades 54 55 // find minimum grade 56 public int GetMinimum() 57 { 58 int lowGrade = grades[ 0 ]; // assume grades[ 0 ] is smallest 59 60 // loop through grades array 61 foreach ( int grade in grades ) 62 { 63 // if grade lower than lowGrade, assign it to lowGrade 64 if ( grade < lowGrade ) 65 lowGrade = grade; // new lowest grade 66 } // end for 67 68 return lowGrade; // return lowest grade 69 } // end method GetMinimum 70 71 // find maximum grade 72 public int GetMaximum() 73 { 74 int highGrade = grades[ 0 ]; // assume grades[ 0 ] is largest 75 76 // loop through grades array 77 foreach ( int grade in grades ) 78 { 79 // if grade greater than highGrade, assign it to highGrade 80 if ( grade > highGrade ) 81 highGrade = grade; // new highest grade 82 } // end for 83 84 return highGrade; // return highest grade 85 } // end method GetMaximum 86 87 // determine average grade for test 88 public double GetAverage() 89 { 90 int total = 0; // initialize total 91 92 // sum grades for one student 93 foreach ( int grade in grades ) 94 total += grade; 95 96 // return average of grades 97 return ( double ) total / grades.Length; 98 } // end method GetAverage 99 100 // output bar chart displaying grade distribution 101 public void OutputBarChart() 102 { 103 Console.WriteLine( "Grade distribution:" ); 104 105 // stores frequency of grades in each range of 10 grades 106 int[] frequency = new int[ 11 ]; 107 108 // for each grade, increment the appropriate frequency 109 foreach ( int grade in grades ) 110 ++frequency[ grade / 10 ]; 111 112 // for each grade frequency, print bar in chart 113 for ( int count = 0; count < frequency.Length; count++ ) 114 { 115 // output bar label ( "00-09: ", ..., "90-99: ", "100: " ) 116 if ( count == 10 ) 117 Console.Write( " 100: " ); 118 else 119 Console.Write( "{0:D2}-{1:D2}: ", 120 count * 10, count * 10 + 9 ); 121 122 // print bar of asterisks 123 for ( int stars = 0; stars < frequency[ count ]; stars++ ) 124 Console.Write( "*" ); 125 126 Console.WriteLine(); // start a new line of output 127 } // end outer for 128 } // end method OutputBarChart 129 130 // output the contents of the grades array 131 public void OutputGrades() 132 { 133 Console.WriteLine( "The grades are: " ); 134 135 // output each student's grade 136 for ( int student = 0; student < grades.Length; student++ ) 137 Console.WriteLine( "Student {0,2}: {1,3}", 138 student + 1, grades[ student ] ); 139 } // end method OutputGrades 140 } // end class GradeBook

Figure 8.16. Create a GradeBook object using an array of grades.

(This item is displayed on page 364 in the print version)

1 // Fig. 8.16: GradeBookTest.cs 2 // Create GradeBook object using an array of grades. 3 public class GradeBookTest 4 { 5 // Main method begins application execution 6 public static void Main( string[] args ) 7 { 8 // one-dimensional array of student grades 9 int[] gradesArray = { 87, 68, 94, 100, 83, 78, 85, 91, 76, 87 }; 10 11 GradeBook myGradeBook = new GradeBook( 12 "CS101 Introduction to C# Programming", gradesArray ); 13 myGradeBook.DisplayMessage(); 14 myGradeBook.ProcessGrades(); 15 } // end Main 16 } // end class GradeBookTest  

Welcome to the grade book for CS101 Introduction to C# 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 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: ** 100: *

Method ProcessGrades (lines 3953) contains a series of method calls that result in the output of a report summarizing the grades. Line 42 calls method OutputGrades to print the contents of array grades. Lines 136138 in method OutputGrades use a for statement to output the student grades. A for statement, rather than a foreach, must be used in this case, because lines 137138 use counter variable student's value to output each grade next to a particular student number (see Fig. 8.16). Although array indices start at 0, an instructor would typically number students starting at 1. Thus, lines 137138 output student + 1 as the student number to produce grade labels "Student 1: ", "Student2: " and so on.

Method ProcessGrades next calls method GetAverage (line 45) to obtain the average of the grades in the array. Method GetAverage (lines 8898) uses a foreach statement to total the values in array grades before calculating the average. The iteration variable in the foreach's header (e.g., int grade) indicates that for each iteration, int variable grade takes on a value in array grades. Note that the averaging calculation in line 97 uses grades.Length to determine the number of grades being averaged.

Lines 4849 in method ProcessGrades call methods GetMinimum and GetMaximum to determine the lowest and highest grades of any student on the exam, respectively. Each of these methods uses a foreach statement to loop through array grades. Lines 6166 in method GetMinimum loop through the array, and lines 6465 compare each grade to lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade. When line 68 executes, lowGrade contains the lowest grade in the array. Method GetMaximum (lines 7285) works the same way as method GetMinimum.

Finally, line 52 in method ProcessGrades calls method OutputBarChart to print a distribution chart of the grade data using a technique similar to that in Fig. 8.6. In that example, we manually calculated the number of grades in each category (i.e., 09, 1019, ..., 9099 and 100) by simply looking at a set of grades. In this example, lines 109110 use a technique similar to that in Fig. 8.7 and Fig. 8.8 to calculate the frequency of grades in each category. Line 106 declares and creates array frequency of 11 ints to store the frequency of grades in each grade category. For each grade in array grades, lines 109110 increment the appropriate element of the frequency array. To determine which element to increment, line 110 divides the current grade by 10 using integer division. For example, if grade is 85, line 110 increments frequency[ 8 ] to update the count of grades in the range 8089. Lines 113127 next print the bar chart (see Fig. 8.6) based on the values in array frequency. Like lines 2425 of Fig. 8.6, lines 123124 of Fig. 8.15 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. 8.16 creates an object of class GradeBook (Fig. 8.15) using int array gradesArray (declared and initialized in line 9). Lines 1112 pass a course name and gradesArray to the GradeBook constructor. Line 13 displays a welcome message, and line 14 invokes the GradeBook object's ProcessGrades method. The output reveals the summary of the 10 grades in myGradeBook.

Software Engineering Observation 8 3

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 18) or it can come from a network (as you will see in Chapter 23). After passing this data to the class's constructor to instantiate the object, the test harness should call 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.

Multidimensional Arrays

Категории