creation: 11/25/99; revised 05/28/03

Chapter 48 Fill in the Blanks

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:

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

// print the data
public void  

// 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
     stud. )

     // add a few grades: 90, 95, and 88
     stud. )
     stud. )
     stud. )

     // print out the object with its new values
     stud. )

     // compute and print the average
     System.out.println( "Average grade: " + stud. )

     }

  }
}

You may have some doubts that this is a sensible program. Perhaps it would be a good idea to have the print() 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.

class StudentGrades
{
  final int arraySize = 25;

  // Instance Variables
   name;
   grades ;
   gradeCount;

  // Constructors

  // Methods

}


5.      Complete the Constructor.    The constructor will initialize the instance variables of the object being constructed.

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 =   ;
    grades =  int[ arraySize ] ;
    gradeCount =  ;
  }


  // Methods

}


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.)

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: " +  );

    System.out.println( "Grades:");

    for ( int j=0; j <  ; j++ )
      System.out.println ( "  grade " + j + ": " + grades[ ] );

  }

}


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
  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 () { ..... }

  public void addGrade ( int grade )
  {
    if ( gradeCount < arraySize ) 
      grades[  ] = grade ;

     ++  ;
    
  }

}

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.

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 ) { .... }

  // Methods
  public void print () { ..... }
  public void addGrade ( int grade ) { ..... }

  public double average ( )
  {
    double sum =   ;

    for ( int j=0; j <  ; j++ )  
      sum +=  [ j ] ;

       sum /  ;   
  }

}


9.      Implement minimum() method.     Be sure to look only at the first gradeCount number of grades.

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 ) { .... }

  // Methods
  public void print () { ..... }
  public void addGrade ( int grade ) { ..... }

  public double average ( )
  {
    double sum =  0  ;

    for ( int j=0; j < gradeCount; j++ )  
      sum += grades[ j ] ;

    return   sum / gradeCount ;
  }

  public int minimum( )
  {
    int min =   ;

    for ( int j=0; j <  ; j++ ) 

      if (   < min )
          min =   ;   
  
    return  ;
  }

}


10.      Implement the maximum() method.     This will be nearly the same as the minimum() method.

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 ) { .... }

  // Methods
  public void print () { ..... }
  public void addGrade ( int grade ) { ..... }

  public double average ( ) { ..... }

  public int maximum( )
  {
    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 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.