Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

If you study the implementation of NextDate carefully, you will see a problem. Look at the Switch clause for 30-day months (4, 6, 9, 11).

If you study the implementation of NextDate carefully, you will see a problem. Look at the Switch clause for 30-day months (4, 6, 9, 11). There is no special action for day = 31. Discuss whether this implementation is correct. Repeat this discussion for the treatment of values of day 29 in the Switch clause for February

public class NextDate { public static SimpleDate nextDate(SimpleDate date) { int tomorrowDay, tomorrowMonth, tomorrowYear; tomorrowMonth = date.month; tomorrowDay = date.day; tomorrowYear = date.year; switch (date.month) { // 31 day months (except Dec.) case 1: case 3: case 5: case 7: case 8: case 10: if (date.day < 31) tomorrowDay = date.day + 1; else { tomorrowDay = 1; tomorrowMonth = date.month + 1; } break; // 30 day months case 4: case 6: case 9: case 11: if (date.day < 30) tomorrowDay = date.day + 1; else { tomorrowDay = 1; tomorrowMonth = date.month + 1; } break; // December case 12: if (date.day < 31) tomorrowDay = date.day + 1; else { tomorrowDay = 1; tomorrowMonth = 1; if (date.year == 2042) System.out.println("Date beyond 2042 "); else Examples 23 tomorrowYear = date.year + 1; } break; // February case 2: if (date.day < 28) tomorrowDay = date.day + 1; else { if (date.day == 28) { if (date.isLeap()) tomorrowDay = 29; else { tomorrowDay = 1; tomorrowMonth = 3; } } else if(date.day == 29) { tomorrowDay = 1; tomorrowMonth = 3; } } break; } return new SimpleDate(tomorrowMonth, tomorrowDay, tomorrowYear); } } public class SimpleDate { int month; int day; int year; public SimpleDate(int month, int day, int year) { if(!rangesOK(month, day, year)) throw new IllegalArgumentException("Invalid Date"); this.month = month; this.day = day; this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getYear() { return year; } 24 Software Testing public void setYear(int year) { this.year = year; } boolean rangesOK(int month, int day, int year) { boolean dateOK = true; dateOK &= (year > 1841) && (year < 2043); // Year OK? dateOK &= (month > 0) && (month < 13); // Month OK? dateOK &= (day > 0) && ( ((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) && day < 32) || ((month == 4 || month == 6 || month == 9 || month == 11) && day < 31) || ((month == 2 && isLeap(year)) && day < 30) || ((month == 2 && !isLeap(year)) && day < 29)); return dateOK; } private boolean isLeap(int year) { boolean isLeapYear = true; if(year % 4 != 0) isLeapYear = false; else if(year % 100 != 0) isLeapYear = true; else if(year % 400 != 0) isLeapYear = false; return isLeapYear; } public boolean isLeap() { return isLeap(year); } @Override public boolean equals(Object obj) { boolean areEqual = false; if(obj instanceof SimpleDate) { SimpleDate simpleDate = (SimpleDate) obj; areEqual = simpleDate.getDay() == getDay() && simpleDate.getMonth() == getMonth() && simpleDate.getYear() == getYear(); } return areEqual; } }

NOTE: There is another post with this question, don't copy paste that answer, if you do then I will mark as spam and downvote

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Microsoft SQL Server 2012 Unleashed

Authors: Ray Rankins, Paul Bertucci

1st Edition

0133408507, 9780133408508

More Books

Students also viewed these Databases questions