This section further evolves class GradeBook, introduced in Chapter 3 and expanded in Chapters 45. 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 input the same set of grades. Array grades is declared as an instance variable in line 7therefore, each GradeBook object maintains its own set of grades. The class's constructor (lines 1014) has two parametersthe 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 application 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 variable 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 needed to perform various calculations.
Figure 7.14. GradeBook class using an array to store test grades.
(This item is displayed on pages 307 - 310 in the print version)
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 %s! ", 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( " Class average is %.2f ", getAverage() ); 44 45 // call methods getMinimum and getMaximum 46 System.out.printf( "Lowest grade is %d Highest grade is %d ", 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: " ); 132 133 // output each student's grade 134 for ( int student = 0; student < grades.length; student++ ) 135 System.out.printf( "Student %2d: %3d ", 136 student + 1, grades[ student ] ); 137 } // end method outputGrades 138 } // end class GradeBook |
Figure 7.15. GradeBookTest creates a GradeBook object using an array of grades, then invokes method processGrades to analyze them.
(This item is displayed on pages 311 - 312 in the print version)
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
|
Method processGrades (lines 3751) contains a series of method calls that result in the output of a report summarizing the grades. Line 40 calls method outputGrades to print the contents of the array grades. Lines 134136 in method outputGrades use a for statement to output each student's grade. A counter-controlled for must be used in this case, because lines 135136 use counter variable student's value to output each grade next to a particular student number (see Fig. 7.15). Although array indices start at 0, a professor would typically number students starting at 1. Thus, lines 135136 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 8696) uses an enhanced for statement 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 variable 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 4647 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 5964 in method getMinimum loop through the array, and lines 6263 compare each grade to lowGrade. If a grade is less than lowGrade, lowGrade is set to that grade. When line 66 executes, lowGrade contains the lowest grade in the array. Method getMaximum (lines 7083) works the same way as 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., 09, 1019, ..., 9099 and 100) by simply looking at a set of grades. In this example, lines 107108 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 ints to store the frequency of grades in each grade category. For each grade in array grades, lines 107108 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 8089. Lines 111125 next print the bar chart (see Fig. 7.15) based on the values in array frequency. Like lines 2324 of Fig. 7.6, lines 121122 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 1213 pass a course name and gradesArray to the GradeBook constructor. Line 14 displays a welcome message, and line 15 invokes the GradeBook object's processGrades method. The output reveals the summary of 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. |
Introduction to Computers, the Internet and the World Wide Web
Introduction to Java Applications
Introduction to Classes and Objects
Control Statements: Part I
Control Statements: Part 2
Methods: A Deeper Look
Arrays
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
GUI Components: Part 1
Graphics and Java 2D™
Exception Handling
Files and Streams
Recursion
Searching and Sorting
Data Structures
Generics
Collections
Introduction to Java Applets
Multimedia: Applets and Applications
GUI Components: Part 2
Multithreading
Networking
Accessing Databases with JDBC
Servlets
JavaServer Pages (JSP)
Formatted Output
Strings, Characters and Regular Expressions
Appendix A. Operator Precedence Chart
Appendix B. ASCII Character Set
Appendix C. Keywords and Reserved Words
Appendix D. Primitive Types
Appendix E. (On CD) Number Systems
Appendix F. (On CD) Unicode®
Appendix G. Using the Java API Documentation
Appendix H. (On CD) Creating Documentation with javadoc
Appendix I. (On CD) Bit Manipulation
Appendix J. (On CD) ATM Case Study Code
Appendix K. (On CD) Labeled break and continue Statements
Appendix L. (On CD) UML 2: Additional Diagram Types
Appendix M. (On CD) Design Patterns
Appendix N. Using the Debugger
Inside Back Cover