package enumEx; // class representing a date, using enum Month public class Date { // instance vars private Month month; // enums are valid types private int day; // default constructor public Date() { month = Month.JANUARY; day = 1; } // parameterized constructor public Date(Month mval, int dval) { this(); setMonth(mval); setDay(dval); } // parameterized constructor public Date(String mval, int dval) { this(); setMonth(mval); setDay(dval); } // accessor for month public Month getMonth() { return month; } // mutator for month adjusts day if past end of new month public void setMonth(Month m) { month = m; if (!month.possDate(getDay())) { // set to last day possible setDay(month.days()); } } // fake mutator for month // for convenience of strings // must match exactly e.g. "JANUARY" not "jan" // so potentially dangerous public void setMonth(String str) { setMonth(Month.valueOf(str.toUpperCase())); } // accessor for day public int getDay() { return day; } // mutator for day only allows possible days public void setDay(int d) { if(month.possDate(d)) { day = d; } } // toString gives month and date public String toString() { return getMonth() + " " + getDay(); } }