See Below
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, Exception
s 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; } . . . . . . }
What happens to a Month
object when the program finishes?