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

Answer:

This is a good place for an array


Temperature Array

If there were many data recorded per day (perhaps the temperature and barometric pressure for every hour of the day) you might define a class Day that represents each day and have an array of Days in each Month. But for us, let's store the temperatures in an array of ints.

A Month might not have valid data for every day. Some days might be in the future, and other days might have been missed. The array valid contains a true/false value for each day which says if the temperature for that day is valid.

Here is a start on the class:


public class Month
{
  // 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
  
  // temperature data
  private int[] temp;
  private boolean[] valid;
  
  // constructors
  public Month( int month, int year)
  {
    this.month = month;
    this.year  = year;

    temp   = new int[    ] ;   
    valid = new int[   ] ;   
  
  }

}

The temperature for each day will be added after the Month object has been built. The constructor only needs to know the year and month number. It initializes those and creates the two arrays.

Review: int arrays are automatically initialized with a value of 0 in each cell.


QUESTION 3:

Pick a size for each array.


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