creation: 11/25/99; revised 05/28/03, 11/21/2015, 09/04/2017


Chapter 62 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 instance variables 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 toString() method for an object, and it will be necessary to have 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.  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 cells of the array contain data.

Possible Confusion: The length of the 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 will be 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 will initialize 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 of the cells 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 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: " +   + "\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 is both the count of how many array cells have data and the index of the next available array cells. 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
  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 ( )
  {
    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
  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 ( )
  {
    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
  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( )
  {
    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, and to play with it.

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

}

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.

go to home page