/* An instance of this class represents a calendar date. ** ** Author: R. McCloskey ** Date: Feb. 2026 */ public class CalendarDate { // class constant // -------------- public static final int MIN_YEAR = 1583; // First full year in which the // Gregorian calendar was in effect public static final int MAX_MONTH = 12; // instance variables // ------------------ protected int month; protected int dayOfMonth; protected int year; // class invariant: 1 <= month <= MAX_MONTH // class invariant: 1 <= dayOfMonth <= daysInMonth(month, year) // constructor // ----------- /* Initializes this calendar date to represent the date described by ** the three parameters. ** pre: 1 <= m <= MAX_MONTH && ** 1 <= d <= daysInMonth(y,m) && ** MIN_YEAR <= y */ public CalendarDate(int m, int d, int y) { this.month = m; this.dayOfMonth = d; this.year = y; } // observers // --------- @Override /* Returns a string describing this calendar date in the M/D/Y format. */ public String toString() { return String.format("%02d/%02d/%04d", month, dayOfMonth, year); } // mutators (none) // --------------- // utility methods // --------------- /* Reports whether or not the specified year is a leap year. */ public boolean isLeapYear(int y) { return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0); } /* Returns the number of days in the specified month of the specified ** year. ** pre: 1 <= m <= MAX_MONTH && MIN_YEAR <= y */ public int daysInMonth(int m, int y) { int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (m != 2) { return days[m-1]; } else if (isLeapYear(y)) { return days[1] + 1; } else { return days[1]; } } }