Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MyDate exercise A class called MyDate, which models a date instance. The MyDate

ID: 3558472 • Letter: M

Question

MyDate exercise

A class called MyDate, which models a date instance.

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 28|29|30|31, 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:
- strMonths (String[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} )
- strDays (String[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}), )
- dayInMonths (int[] = {31,28,31,30,31,30,31,31,30,31,30,31):

The MyDate class has the following public static methods :
- 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): returns true 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 28|29|30|31 depending on the month and whether it is a leap year on Feb.
- getDayOfWeek(int year, int month, int day): returns the day of the week, you can use the java.util.Calendar

The MyDate class has one constructor, which takes 3 parameters: year, month and day. It shall invoke setDate() method (described below) 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. 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. Throw an IllegalArgumentException with the message "Invalid year!".)
- setMonth(int month): It shall verify that the given month is between 1 and 12. 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. 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 mmm dd, yyyy", e.g., "Tuesday, Feb 14, 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. Throw an IllegalStateException with the message "Year out of range!" if year > 9999.)
- previousDay(), previousMonth(), previousYear(): similar to nextDay(), nextMonth(), nextYear() from above.


Write a test class that asks the user to enter a date in MM DD YYYY format. (example: 'Please enter a date (format: mm dd yyyy):')
In your test class, return the results of calling the methods to the console with a description of what data is returning.
Then ask the user: Would you like to enter another date? (Enter 'Y' for yes and 'N' for no)
if they enter 'Y' then continue with 'Please enter a date (format: mm dd yyyy):'
if they entered 'N' then exit the program.


Example:
Please enter a date (format: mm dd yyyy): 06 19 2014
Jun 19, 2014 is not a leap year
Day of the week is: Thursday
Next day is: Friday, Jun 20, 2014
Next month is: Jul
Next year is: 2015
Previous day is: Wednesday, Jun 18, 2014
Previous month is: May
Previous year is: 2013
Would you like to enter another date? (Enter 'Y' for yes and 'N' for no)

Explanation / Answer

public class MyDate

{

    public static void main(String args[])

    {

        /*Give the year here which needs to be checked for leap year

         * (Assuming that year given here >= 1582 and corresponds to a year in Gregorian calendar)

         */

        int year = 2000;

        //Flag to store the test result

        boolean isLeapYear = false;

        if(year % 400 == 0)

        {

            isLeapYear = true;

        }

        else if (year % 100 == 0)

        {

            isLeapYear = false;

        }

        else if(year % 4 == 0)

        {

            isLeapYear = true;

        }

        else

        {

            isLeapYear = false;

        }

        //Output the test result

        if(isLeapYear)

        {

            System.out.println("Year "+year+" is a Leap Year");

        }

        else

        {

            System.out.println("Year "+year+" is not a Leap Year");

        }

    }

}