I need help. "The maximum value of day depends on the month for the date. The maximum day for the month of February also depends on the year value for the ExtendedDate object (i.e. is it a leap year or not?)". How do i go about setting this up within my code?
package date; public class ExtendedDate extends Date { public int newMonth; public int newDay; public int newYear; // Constructor that accepts parameters public ExtendedDate(int newMonth, int newDay, int newYear) { newMonth = 1; newDay = 1; newYear = 1900; } // Method to set the date – override inherited setDate method // If data is invalid, do not change the receiver /** * * @param monthInt * @param dayInt * @param yearInt */ @Override public void setDate(int newMonth, int newDay, int newYear) { this.newMonth = newMonth; this.newDay = newDay; this.newYear = newYear; } // Method to set month ensuring that only valid changes are made /** * * @param newMonth */ public void setMonth(int newMonth) { if(isValidDate(newMonth, newDay, newYear)){ this.newMonth = newMonth; } else { this.newMonth =(1); } } // Method to set day ensuring that only valid changes are made public void setDay(int newDay) { if(isValidDate(newMonth, newDay, newYear)){ this.newDay = newDay; } } // Method to set year ensuring that only valid changes are made public void setYear(int newYear) { if(isValidDate(newMonth, newDay, newYear)){ this.newYear = newYear; } } // New method to verify a date // Valid range is 1-1-1900 through 12-31-2999 /** * * @param newMonth * @param newDay * @param newYear * @return */ public static boolean isValidDate(int newMonth, int newDay, int newYear) { if ( newMonth < 13 || newMonth > 0){ return true; } if (newDay < 31 || newDay >0){ return true; } if (newYear > 1899 || newYear < 2999) { return true; } return false; } // Method to test for a leap year public static boolean isLeapYear(int newYear) { if (newYear == newYear*4){ return true; } return false; } // Instance method version of the above public boolean isLeapYear() { return true; } // Return number of days in a month public int daysInMonth() { return 0; } // Return number of days passed in year (includes current day) public int daysToDate() { return 0; } // Return number of days remaining in year /** * * @return */ @Override public int daysRemainingInYear() { return 0; } }
--- Update ---
Here's an updated code
package date; public class ExtendedDate extends Date { public int newMonth; public int newDay; public int newYear; // Constructor that accepts parameters public ExtendedDate(int newMonth, int newDay, int newYear) { newMonth = 1; newDay = 1; newYear = 1900; } // Method to set the date – override inherited setDate method // If data is invalid, do not change the receiver /** * * @param monthInt * @param dayInt * @param yearInt */ @Override public void setDate(int newMonth, int newDay, int newYear) { this.newMonth = newMonth; this.newDay = newDay; this.newYear = newYear; } // Method to set month ensuring that only valid changes are made /** * * @param newMonth */ public void setMonth(int newMonth) { if(isValidDate(newMonth, newDay, newYear)){ this.newMonth = newMonth; } else { this.newMonth =(1); } } // Method to set day ensuring that only valid changes are made public void setDay(int newDay) { if(isValidDate(newMonth, newDay, newYear)){ this.newDay = newDay; if(newDay == 30 && newMonth == 2 ){ newMonth = 3; } if(newDay == 31 && newMonth == 2 ){ newMonth = 3; } if(newDay == 31 && newMonth == 9 ){ newMonth = 10; } if(newDay == 31 && newMonth == 4 ){ newMonth = 5; } if(newDay == 31 && newMonth == 6 ){ newMonth = 7; } if(newDay == 31 && newMonth == 11 ){ newMonth = 12; } }else { this.newDay =(1); } } // Method to set year ensuring that only valid changes are made public void setYear(int newYear) { if(isValidDate(newMonth, newDay, newYear)){ this.newYear = newYear; } else { this.newYear =(1); } } // New method to verify a date // Valid range is 1-1-1900 through 12-31-2999 /** * * @param newMonth * @param newDay * @param newYear * @return */ public static boolean isValidDate(int newMonth, int newDay, int newYear) { if ( newMonth < 13 || newMonth > 0){ return true; } if (newDay < 31 || newDay >0){ return true; } if (newYear > 1899 || newYear < 2999) { return true; } return false; } // Method to test for a leap year public static boolean isLeapYear(int newYear) { if (newYear == newYear*4){ return true; } return false; } // Instance method version of the above public boolean isLeapYear() { return true; } // Return number of days in a month public int daysInMonth() { return 0; } // Return number of days passed in year (includes current day) public int daysToDate() { return 0; } // Return number of days remaining in year /** * * @return */ @Override public int daysRemainingInYear() { return 0; } }
--- Update ---
Here's another update.
package date; public class ExtendedDate extends Date { public int newMonth; public int newDay; public int newYear; // Constructor that accepts parameters public ExtendedDate(int newMonth, int newDay, int newYear) { newMonth = 1; newDay = 1; newYear = 1900; } // Method to set the date – override inherited setDate method // If data is invalid, do not change the receiver /** * * @param monthInt * @param dayInt * @param yearInt */ @Override public void setDate(int newMonth, int newDay, int newYear) { this.newMonth = newMonth; this.newDay = newDay; this.newYear = newYear; } // Method to set month ensuring that only valid changes are made /** * * @param newMonth */ public void setMonth(int newMonth) { if(isValidDate(newMonth, newDay, newYear)){ this.newMonth = newMonth; } else { this.newMonth =(1); } } // Method to set day ensuring that only valid changes are made public void setDay(int newDay) { if(isLeapYear () && newMonth == 2 && newDay > 29){ newDay = 30; } if(isValidDate(newMonth, newDay, newYear)){ this.newDay = newDay; if(newDay == 30 && newMonth == 2 ){ newMonth = 3; } if(newDay == 31 && newMonth == 2 ){ newMonth = 3; } if (newDay > 29 && newYear %4 == 0 && newMonth == 2){ newDay = 30; } if(newDay == 31 && newMonth == 9 ){ newMonth = 10; } if(newDay == 31 && newMonth == 4 ){ newMonth = 5; } if(newDay == 31 && newMonth == 6 ){ newMonth = 7; } if(newDay == 31 && newMonth == 11 ){ newMonth = 12; } }else { this.newDay =(1); } } // Method to set year ensuring that only valid changes are made public void setYear(int newYear) { if(isValidDate(newMonth, newDay, newYear)){ this.newYear = newYear; } else { this.newYear =(1); } } // New method to verify a date // Valid range is 1-1-1900 through 12-31-2999 /** * * @param newMonth * @param newDay * @param newYear * @return */ public static boolean isValidDate(int newMonth, int newDay, int newYear) { if ( newMonth < 13 || newMonth > 0){ return true; } if (newDay < 31 || newDay >0){ return true; } if (newYear > 1899 || newYear < 2999) { return true; } return false; } // Method to test for a leap year public static boolean isLeapYear(int newYear) { if (newYear % 4 == 0){ return true; } return false; } // Instance method version of the above public boolean isLeapYear() { return true; } // Return number of days in a month public int daysInMonth() { return 0; } // Return number of days passed in year (includes current day) public int daysToDate() { return 0; } // Return number of days remaining in year /** * * @return */ @Override public int daysRemainingInYear() { return 0; } }