This exercise will give you practice in writing methods that do things with arrays.
1. Design of the StudentGrades Class. An object of this class will represent the grades of a single student. For simplicity, say that all grades count equally and that a student might have up to 25 of them.
Think of three items of data that will be needed for a StudentGrades object.
Now think of some methods that will be useful to apply to a list of grades. It is almost always a good idea to have a way to print the data of an object, and it will be necessary to add a grade to the list.
2. Document the class. Now fill in the documentation of the class:
3. Checking the Design. To check the design, write a small program that uses the class. Of course, the program can't be compiled and run until the class is written, but you can get a feel for if the class design is sensible by doing this.
You may have some doubts that this is a sensible program.
Perhaps it would be a good idea to have the spread()
method print out a message
each time it is used.
4. Fill in Instance Variables. Fill in the data type of each instance variable. The grades will be kept in an integer array which will have room for 25 numbers. Don't construct the array yet. There will also be an instance variable that counts how many of the slots of the array contain data.
Possible Confusion: The length of the array is the number of slots it has. In this program not all the slots will be filled with grades, so we need a count of how many slots are used. Slots will be used in order, starting with index 0.
5. Complete the Constructor. The constructor will initialize the instance variables of the object being constructed.
6. Complete the print()
Method.
You want to only print out the first gradeCount
number of grades.
You do not want to print out every slot of the array (this is unlike the
print()
method in the chapter.)
7. Implement the addGrade()
method.
This method will add one integer grade to the list of grades for the student.
Be sure to increase the gradeCount.
Carefully study how the variable gradeCount
works.
It is both the count of how many array slots have data and the
index of the next available array slot.
This is a common programming "trick."
8. Implement the average()
method.
Be sure to initialize sum
to zero.
Then add up the first gradeCount
number of grades and
divide by gradeCount
to get the average.
9. Implement minimum() method.
Be sure to look only at the first gradeCount
number of grades.
10. Implement the maximum()
method.
This will be nearly the same as the minimum()
method.
Entire Program, with testing class: You might wish to copy this program to NotePad, save it to a file, and to play with it.
class StudentGrades { final int arraySize = 25; // Instance Variables String name; // the name of the student int[] grades; // the array of grades int gradeCount; // how many of the slots have data // Constructor StudentGrades( String studentName ) { name = studentName ; grades = new int[ arraySize ] ; gradeCount = 0 ; } // Methods public void print () { System.out.println ( "Name: " + name ); System.out.println( "Grades:"); for ( int j=0; j < gradeCount ; j++ ) System.out.println ( " grade " + j + ": " + grades[ j ] ); } public void addGrade ( int grade ) { if ( gradeCount < arraySize ) grades[gradeCount] = grade ; gradeCount ++ ; } public double average ( ) { double sum = 0 ; for ( int j=0; j < gradeCount; j++ ) sum += grades[ j ] ; return sum / gradeCount ; } public int minimum( ) { int min = grades[ 0 ] ; for ( int j=0; j < gradeCount; j++ ) if ( grades[j] < min ) min = grades[j] ; return min ; } public int maximum( ) { int max = grades[ 0 ] ; for ( int j=0; j < gradeCount; j++ ) if ( grades[j] > max ) max = grades[j] ; return max ; } } class StudentTester { public static void main ( String[] args ) { // create a student object StudentGrades stud = new StudentGrades( "Laura Lyons" ) ; // add a few grades stud.addGrade( 90 ) ; stud.addGrade( 95 ) ; stud.addGrade( 88 ) ; stud.addGrade( 78 ) ; stud.addGrade( 82 ) ; stud.addGrade( 97 ) ; // print out the object with its new values stud.print() ; // compute and print the average, minimum and maximum System.out.println( "Average grade: " + stud.average() + " Minimum: " + stud.minimum() + " Maximum: " + stud.maximum() ); } }
End of the Exercise. If you want to do it again, click on "Refresh" in your browser window. Click here to go back to the main menu.