// Programmer: Rance Cleaveland // Date: 4 Oct. 2006 // // This class implements simple operations on dates. public class Date { private int day = 1; // Day part of date private int month = 1; // Month part of date private int year = 1900; // Year part of date private String separator = "/"; // Separator used in printing private String note; // Used for comment on date. // Default constructor Date () { year = 2000; } // Another constructor Date (int newDay, int newMonth, int newYear){ day = newDay; month = newMonth; year = newYear; } // Copy constructor Date (Date d) { day = d.day; month = d.month; year = d.year; separator = d.separator; note = d.note; } // The method for converting dates to strings public String toString () { return (month + separator + day + separator + year); } // The method for printing dates to System.out public void print () { System.out.print (toString ()); } // Method for printing dates followed by carriage return public void println () { print (); System.out.println (); } // "incrementYear" increments the year. public void incrementYear () { year++; } // "incrementMonth" increments the month. public void incrementMonth () { if (month < 12) month++; else { month = 1; year++; } } // "setYear" allows the year to be set to a give value public void setYear (int newYear) { year = newYear; } // "setMonth" allows the month to be set; an error is reported // if the month is invalid public void setMonth (int newMonth) { if ((1 <= newMonth) && (newMonth <= 12)) month = newMonth; else System.out.println ("Bad argument to setMonth: " + newMonth); } // "isLeapYear" returns true if the year is a leap year. public boolean isLeapYear () { return ((year % 4) == 0 && ((year % 100 != 0) || (year % 400 == 0))); } // "numDaysInMonth" returns the number of days in the current month private int numDaysInMonth () { if (month == 1) return 31; else if (month == 2) { if (isLeapYear ()) return 29; else return 28; } else if (month == 3) return 31; else if (month == 4) return 30; else if (month == 5) return 31; else if (month == 6) return 30; else if (month == 7) return 31; else if (month == 8) return 31; else if (month == 9) return 30; else if (month == 10) return 31; else if (month == 11) return 30; else // (month == 12) return 31; } // "setDay" allows the day to be set; an error is reported if the // day value is invalid for the given month public void setDay (int newDay) { if (numDaysInMonth () >= newDay) day = newDay; else System.out.println ("Invalid number of days (month = " + month +"): " + newDay); } // "addDays" adds given number of days to date (must be positive) public void addDays (int future) { if (future < 0) System.out.println("Invalid argument to addDays: " + future); else { int daysLeft = day + future; while (daysLeft > numDaysInMonth()) { daysLeft -= (numDaysInMonth ()); incrementMonth (); } day = daysLeft; } } // "getDay" returns the day public int getDay () { return day; } // "getMonth" returns the month public int getMonth () { return month; } // "getYear" returns the year public int getYear () { return year; } // "isValentinesDay" returns true if the date is Valentines Day public boolean isValentinesDay () { return ((month == 2) && (day == 14)); } // "equals" computes an equality function on dates public boolean equals (Date d) { return (day == d.day && month == d.month && year == d.year); } // "isBefore" returns true if the date is before the inputted date public boolean isBefore (Date d) { if (year < d.year) return true; else if (year == d.year) { if (month < d.month) return true; else if ( (month == d.month) && (day < d.day) ) return true; else return false; } else return false; } }