go to previous page   go to home page   go to next page

Answer:

See Below


Finished Methods

The completed methods scan through the arrays, counting and adding up just the valid days. Returning a floating point error flag is somewhat awkward in avgTemp(). Again, Exceptions would be useful here.


import java.util.*;

class Month
{
  // constants
  final int ERRORFLAG = 999;
 
  // instance variables
  private int   month;  // 1 == January
  private int   year;   // year as an int, eg 2017
  private int   daysInMonth;   // number of days in this month
  
  private int[] temp;
  private boolean[] valid; // true, if corresponding day holds data
 
  . . .
 
  // count the number of days with valid data
  public int countValidDays()
  {
    int count = 0;
    for ( int day=1; day<=daysInMonth; day++ )
      if ( valid[day] ) 
        count++ ;
      
    return count;      
  }
  
  // compute the average temperatures for 
  // all valid days  
  public double avgTemp()
  {
    int sum = 0;
    int count = 0;
    for ( int day=1; day <= daysInMonth; day++ )
    {
      if ( valid[day] ) 
      {
        sum += temp[ day ];
        count++ ;
      }
    }
    
    if ( count > 0 )
      return (double)sum/count;
    else
      return ERRORFLAG;
  }
  
  . . . . . .

}


QUESTION 11:

What happens to a Month object when the program finishes?


go to previous page   go to home page   go to next page