creation: 11/25/99; revised 05/28/03, 11/21/2015, 09/04/2017, 05/12/2024
Practice writing methods that have arrays as parameters.
1. StudentGrades Class. A StudentGrades object represents 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.
What data will a StudentGrades object have?
Now think of some methods that will be useful for a list of grades.
There should be a toString()
method for an object,
and there should be a way to add a grade to the list.
2. Document the class. Now fill in the documentation of the class:
class // A class that holds the grades for one student. Constructor // A newly constructed object has the student name but holds no grades StudentGrades ( studentName )
Methods // create a String public String // add a grade for this student public void addGrade // compute the average public double average() // compute the minimum public int // compute the maximum public int
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.
class StudentTester { public static void main ( String[] args ) { // create an object for a student StudentGrades stud = new ( "Laura Lyons" ) ; // print out empty object System.out.println( stud. ); // add a few grades: 90, 95, and 88 stud. ) stud. ) stud. ) // print out the object with its new values System.out.println( stud. ); // compute and print the average System.out.println( "Average grade: " + stud. ) } } }
4. Declare Instance Variables. The grades will be kept in an integer array which has room for 25 numbers.
Don't construct the array yet. The constructor for the class will do this. Also, include a count of how many of the cells of the array contain data.
Possible Confusion: The length of an array is the number of cells it has. In this program not all the cells will be filled with grades, so we need a count of how many cells are in use. Cells are used in order, starting with index 0.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private name; private grades ; private gradeCount; // Constructors // Methods }
5. Complete the Constructor. The constructor initializes the instance variables of the object being constructed.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many cells currently have data // Constructor public StudentGrades( String studentName ) { name = ; grades = int[ ARRAYSIZE ] ; gradeCount = ; } // Methods }
6. Complete the toString()
Method.
You want only to include the first gradeCount
number of grades.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many cells currently have data // Constructor public StudentGrades( String studentName ) { name = studentName ; grades = new int[ ARRAYSIZE ] ; gradeCount = 0 ; } // Methods public String toString() { String str = "Name: " + + "\n" ; for ( int j=0; j < ; j++ ) str += "grade " + j + ": " + grades[ ] + ", "; return str; } }
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.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many of the cells have data // Constructor public StudentGrades( String studentName ) { name = studentName ; grades = new int[ ARRAYSIZE ] ; gradeCount = 0 ; } // Methods public String toString() { ..... } public void addGrade ( int grade ) { if ( gradeCount < ARRAYSIZE ) grades[ ] = grade ; ++ ; } }
Study how the variable gradeCount
works.
It both counts how many array cells have data and is the
index of the next available array cell.
It starts out at 0, which means there are 0 grades
in the array and that the next grade added will go in cell 0.
8. Implement the average()
method.
If there are no grades in the array to average, return -1 as an error flag.
Be sure to initialize sum
to zero.
Then add up the first gradeCount
number of grades and
divide by gradeCount
to get the average.
The sum
is double precision so that the
division by count
is floating point division.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many of the cells have data // Constructor Public StudentGrades( String studentName ) { .... } // Methods public String toString() { ..... } public void addGrade ( int grade ) { ..... } public double average ( ) { if ( count==0 ) return -1; double sum = ; for ( int j=0; j < ; j++ ) sum += [ j ] ; sum / ; } }
Possible Confusion: (part 2)
The average is calculated by divising by gradeCount
(because that is how many grades there are).
But the loop counter starts at zero and goes up to gradeCount-1
(because those are the indexes of the valid grades.)
Opportunity for an off-by-one error!
9. Implement minimum() method.
Look only at the first gradeCount
number of grades.
If there are no grades in the array return -1 as an error flag.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many of the cells have data // Constructor public StudentGrades( String studentName ) { .... } // Methods public String toString() { ..... } public void addGrade ( int grade ) { ..... } public double average ( ) { if ( gradeCount==0 ) return -1; double sum = 0 ; for ( int j=0; j < gradeCount; j++ ) sum += grades[ j ] ; return sum / gradeCount ; } public int minimum( ) { if ( gradeCount==0 ) return -1; int min = ; for ( int j=0; j < ; j++ ) if ( < min ) min = ; return ; } }
10. Implement the maximum()
method.
This is nearly the same as the minimum()
method.
If there are no grades in the array return -1 as an error flag.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many of the cells have data // Constructor public StudentGrades( String studentName ) { .... } // Methods public String toString() { ..... } public void addGrade ( int grade ) { ..... } public double average ( ) { ..... } public int maximum( ) { if ( gradeCount==0 ) return -1; int max = ; for ( int j=0; j < ; j++ ) if ( grades[j] max ) max = ; return ; } }
Entire Program, with testing class:
You might
wish to copy this program to a text editor, save it to a file StudentTester.java
,
and to play with it.
Perhaps include more StudentGrades
objects in main()
.
For extra fun,
include an array of StudentGrades
objects.
class StudentGrades { final int ARRAYSIZE = 25; // Instance Variables private String name; // the name of the student private int[] grades; // the array of grades private int gradeCount; // how many of the cells have data // Constructor public StudentGrades( String studentName ) { name = studentName ; grades = new int[ ARRAYSIZE ] ; gradeCount = 0 ; } // Methods public String toString() { String str = "Name: " + name + "\n"; for ( int j=0; j < gradeCount ; j++ ) str += "grade " + j + ": " + grades[ j ] +", "; return str; } public void addGrade ( int grade ) { if ( gradeCount < ARRAYSIZE ) grades[gradeCount] = grade ; gradeCount ++ ; } public double average ( ) { if ( gradeCount==0 ) return -1; double sum = 0 ; for ( int j=0; j < gradeCount; j++ ) sum += grades[ j ] ; return sum / gradeCount ; } public int minimum( ) { if ( gradeCount==0 ) return -1; int min = grades[ 0 ] ; for ( int j=0; j < gradeCount; j++ ) if ( grades[j] < min ) min = grades[j] ; return min ; } public int maximum( ) { if ( gradeCount==0 ) return -1; int max = grades[ 0 ] ; for ( int j=0; j < gradeCount; j++ ) if ( grades[j] > max ) max = grades[j] ; return max ; } } public 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 System.out.println( stud.toString() ); // 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.