Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

by java Exercise 1 A class called MyDate, which models a date instance, is defined as shown in the class diagram. The MyDate class contains

image text in transcribedimage text in transcribedimage text in transcribedby java

Exercise 1 A class called MyDate, which models a date instance, is defined as shown in the class diagram. The MyDate class contains the following private instance variables: - year (int): Between 1 to 9999. - month (int): Between 1 (Jan) to 12 (Dec). - day (int): Between 1 to 28293031, where the last day depends on the month and whether it is a leap year for Feb (28|29). It also contains the following private static variables (drawn with underlined in the class diagram): - strMonths (String[]), strDays (String[]), and dayInMonths (int []): static variables, initialized as shown, which are used in the methods. The MyDate class has the following public static methods (drawn with underlined in the class diagram): - isleapYear(int year): returns true if the given year is a leap year. A year is a leap year if it is divisible by 4 but not by 100, or it is divisible by 400 . - isValidDate(int year, int month, int day): returnstrue if the given year, month, and day constitute a valid date. Assume that year is between 1 and 9999, month is between 1 (Jan) to 12 (Dec) and day shall be between 1 and 28293031 depending on the month and whether it is a leap year on Feb. - getDay0fWeek(int year, int month, int day): returns the day of the week, where 0 for Sun, 1 for Mon, ..., 6 for Sat, for the given date. Assume that the date is valid. - The MyDate class has one constructor, which takes 3 parameters: year, month and day. It shall invoke setDate() method (to be described later) to set the instance variables. The MyDate class has the following public methods: - setDate(int year, int month, int day): It shall invoke the static method isValidDate() to verify that the given year, month and day constitute a valid date. (Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid year, month, or day!".) - setYear(int year): It shall verify that the given year is between 1 and 9999. (Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid year!".) - setMonth(int month): It shall verify that the given month is between 1 and 12. (Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid month!".) - setDay(int day): It shall verify that the given day is between 1 and dayMax, where daymax depends on the month and whether it is a leap year for Feb. (Advanced: Otherwise, it shall throw an IllegalArgumentException with the message "Invalid month!".) - getYear(), getMonth(), getDay ( ): return the value for the year, month and day, respectively. - toString ( ): returns a date string in the format "xxxday d mmm yyyy", e.g., "Tuesday 14 Feb 2012". - nextDay ( ): update this instance to the next day and return this instance. Take note that nextDay () for 31 Dec 2000 shall be 1 Jan 2001. - nextmonth ( ): update this instance to the next month and return this instance. Take note that nextMonth() for 31 Oct 2012 shall be 30 Nov 2012. - nextYear ( ): update this instance to the next year and return this instance. Take note that nextYear() for 29 Feb 2012 shall be 28 Feb 2013. (Advanced: throw an IllegalStateException with the message "Year out of range!" if year > 9999.) - previousDay(), previousMonth(), previousYear( ): similar to the above. Write the code for the MyDate class. Use the following test statements to test the MyDate class

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

Step: 3

blur-text-image

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

Graph Databases New Opportunities For Connected Data

Authors: Ian Robinson, Jim Webber, Emil Eifrem

2nd Edition

1491930896, 978-1491930892

More Books

Students also viewed these Databases questions