/* This file defines a class for storing dates. * * Warning: The methods of this class access the private * data directly. Java style gurus frown on this. * Private data members should be accessed exclusively * through accessor and mutator methods. */ public class Date { private int month; // the month (from 1-12) private int day; // the day of the month private int year; // the year (four digits) /* Constructor method initializes a new Date object */ public Date( int m, int d, int y ) { month = m; day = d; year = y; /* This has been added */ if ( d < 1 || d > lastDayOfMonth( m, y) ) System.out.println( "Warning: day is out of range" ); } /* Converts to a string (US style: MM/DD/YYYY) */ public String toString( ) { return new String( month + "/" + day + "/" + year ); } /* Is this date equal to another? */ public boolean equals( Date d ) { if ( ( year == d.year ) && ( month == d.month ) && ( day == d.day ) ) return true; else return false; } /* ----------- This material was added later -------------*/ static public final int DAYS_PER_WEEK = 7; static public final int MONTHS_PER_YEAR = 12; /* Is the given year a leap year? */ public static boolean isLeapYear( int yr ) { boolean answer; if ( (yr % 400) == 0 ) answer = true; // multiple of 400 else if ( (yr % 100) == 0 ) answer = false; // multiple of 100 else if ( (yr % 4) == 0 ) answer = true; // multiple of 4 else answer = false; // not a multiple of 4 return answer; } /* Given a month and year, returns the last day of this month. */ private static int lastDayOfMonth( int mo , int yr) { int nDays; if ( mo == 4 || mo == 6 || mo == 9 || mo == 11 ) nDays = 30; else if ( mo == 2) { if (isLeapYear( yr ) ) nDays = 29; else nDays = 28; } else { nDays = 31; } return nDays; } }