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

Answer:

Just those that are valid


Average Temperatures

Here are some more skeleton methods.


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] ) 
          ;
      
    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] ) 
      {
      
      }
    }
    
    if ( count > 0 )
      return (double)sum/count;
    else
      return ERRORFLAG;
  }
  
  . . . . . .

}

public class MonthTester
{
  public static void main( String[] args)  
  {
    . . .

    System.out.println( jan );  
    int validDays = jan.countValidDays();
    
    if ( validDays > 0 )
      System.out.println( "Average Temperature: " + jan.avgTemp() );
  }
  
}

QUESTION 10:

Fill in the blanks to complete the methods.


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