/** * Write a description of class Date1 here. * * @author (your name) * @version (a version number or a date) */ public class Date5 { // Instance variables // What do we need to keep track of for a date? private int day; private String month; private int year; public Date5() { // illustrate chaining constructors this("January", 1, 2000); } public Date5( String month, int day, int year) { this.day = day; // validate the month and set it setMonth( month); this.year = year; } public Date5( int month, int day, int year) { this.day = day; this.month = getMonthString( month); this.year = year; } // copy constructor public Date5( Date5 otherDate) { day = otherDate.day; month = otherDate.month; year = otherDate.year; } // Get and Set methods (a.k.a. accessor & mutator methods public int getDay() { return day; } public String getMonth() { return month; } public int getYear() { return year; } public void setDay( int theDay) { day = theDay; } public void setMonth( String theMonth) { //convert month string to int, verifying correctness // and setting default to 1 if incorrect if( getMonthNumber( theMonth) == -1) { // month string was incorrect, setting default to "January" theMonth = "January"; } month = theMonth; } private int getMonthNumber( String theMonth) { int result = -1; if( theMonth.equals("January")) result =1; else if ( theMonth.equals("February")) result =2; else if ( theMonth.equals("March")) result =3; else if ( theMonth.equals("April")) result =4; else if ( theMonth.equals("May")) result =5; else if ( theMonth.equals("June")) result =6; else if ( theMonth.equals("July")) result =7; else if ( theMonth.equals("August")) result =8; else if ( theMonth.equals("September")) result =9; else if ( theMonth.equals("October")) result =10; else if ( theMonth.equals("November")) result =11; else if ( theMonth.equals("December")) result =12; else { System.out.println("*** Error, invalid month. \n"); result = -1; } return result; }// end getMonthNumber() private String getMonthString( int month) { String monthName = ""; switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break; case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: System.out.println("*** Invalid month int. Exiting...."); System.exit( -1); break; }//end switch() return monthName; }//end getMonthString(..) public void setYear( int year) { // Problem if we have: // year = year; // since it would only change the local variable // using "this" refers to the instance variable, not the parameter this.year = year; } // create the toString() method to allow formatted printing. public String toString() { return getMonthNumber( this.month) + "/" + day + "/" + year; // alternatively we could print the month text: // sreturn month + "/" + day + "/" + year; } public boolean equals( Date5 theDate) { return ( (theDate.day == this.day) && (theDate.month.equals(month)) && (theDate.year == year) ); } }