See below.
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; // day 1 in temp[1]
private boolean[] valid;
// constructors
public Month( int month, int year)
{
this.month = month;
this.year = year;
temp = new int[ 32 ] ;
valid = new boolean[ 32 ] ;
switch ( month )
{
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
daysInMonth = 31;
break;
case 4: case 6: case 9: case 11:
daysInMonth = 30;
break;
case 2:
if ( isLeapYear( year ) )
daysInMonth = 29;
else
daysInMonth = 28;
break;
default :
days = 0;
}
}
public boolean isLeapYear( int year )
{
return ((year%4==0) && (year%100!=0)) || (year%400==0);
}
public String toString()
{
return month + "/" + year;
}
}
Why are the arrays of length 32?
Common Programming Trick: It is convenient to use array index 1 for day 1, array index 2 for day 2, and so on. So use length 32.
Array index zero is not used. You might think this is a waste, but it simplifies the programming, which is almost always a good idea. The programming logic needed to use indexes one less than the day number would require many more bytes than the four-byte unused cell.So, why do all months use 32, even though some months have fewer than 31 days? For the same reason as above. Unused bytes at the end don't hurt.
The variable daysInMonth contains the number of days in a particular month.
The constructor computes this based on the year and month supplied as parameters.
The isLeapYear() method is used in this.
Does this constructor do error checking?