Question: DateUtil: Complete the following methods in a class called DateUtil: boolean isLeapYear(int year): returns true if the given year is a leap year. A year

DateUtil: Complete the following methods in a class called DateUtil: boolean 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. boolean isValidDate(int year, int month, int day): returns true if the given year, month and day constitute a given 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. int getDayOfWeek(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. String toString(int year, int month, int day): prints the given date in the format "xxxday d mmm yyyy", e.g., "Tuesday 14 Feb 2012". Assume that the given date is valid. To find the day of the week (Reference: Wiki "Determination of the day of the week"): 1. Based on the first two digit of the year, get the number from the following "century" table. 1700- 1800- 1900- 2000- 2100- 2200- 2300- 2400- 4 2 0 6 4 2 0 6 2. Take note that the entries 4, 2, 0, 6 repeat. 3. Add to the last two digit of the year. 4. Add to "the last two digit of the year divide by 4, truncate the fractional part". 5. Add to the number obtained from the following month table: Non- Leap Y ear Leap Y ear Jan 0 6 Feb 3 2 Mar Apr May Jun Jul Aug Sep Oct Nov Dec 3614625035 same as above 6. Add to the day. 7. The sum modulus 7 gives the day of the week, where 0 for SUN, 1 for MON, ..., 6 for SAT. For example: 2012, Feb, 17 (6 + 12 + 12/4 + 2 + 17) % 7 = 5 (Fri) The skeleton of the program is as follows: /* Utilities for Date Manipulation */ public class DateUtil { // Month's name for printing public static String strMonths[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; // Number of days in each month (for non-leap years) public static int daysInMonths[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // Returns true if the given year is a leap year public static boolean isLeapYear(int year) { ...... } // Return true if the given year, month, day is a valid date // year: 1-9999 // month: 1(Jan)-12(Dec) // day: 1-28|29|30|31. The last day depends on year and month public static boolean isValidDate(int year, int month, int day) { ...... } // Return the day of the week, 0:Sun, 1:Mon, ..., 6:Sat public static int getDayOfWeek(int year, int month, int day) { ...... } // Return String "xxxday d mmm yyyy" (e.g., Wednesday 29 Feb 2012) public static String printDate(int year, int month, int day) { ...... } public static void main(String[] args) { System.out.println(isLeapYear(1900)); // false System.out.println(isLeapYear(2000)); // true System.out.println(isLeapYear(2011)); // false System.out.println(isLeapYear(2012)); // true System.out.println(isValidDate(2012, 2, 29)); // true System.out.println(isValidDate(2011, 2, 29)); // false System.out.println(isValidDate(2099, 12, 31)); // true System.out.println(isValidDate(2099, 12, 32)); // false System.out.println(getDayOfWeek(1982, 4, 24)); // 6:Sat System.out.println(getDayOfWeek(2000, 1, 1)); // 6:Sat System.out.println(getDayOfWeek(2054, 6, 19)); // 5:Fri System.out.println(getDayOfWeek(2012, 2, 17)); // 5:Fri System.out.println(toString(2012, 2, 14)); // Tuesday 14 Feb 2012 } } You can compare the day obtained with the Java's Calendar class as follows: // Construct a Calendar instance with the given year, month and day Calendar cal = new GregorianCalendar(year, month - 1, day); // month is 0-based // Get the day of the week number: 1 (Sunday) to 7 (Saturday) int dayNumber = cal.get(Calendar.DAY_OF_WEEK); String[] calendarDays = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" }; // Print result System.out.println("It is " + calendarDays[dayNumber - 1]); Foot Note: The calendar we used today is known as Gregorian calendar, which came into effect in October 15, 1582 in some countries and later in other countries. It replaces the Julian calendar. 10 days were removed from the calendar, i.e., October 4, 1582 (Julian) was followed by October 15, 1582 (Gregorian). The only difference between the Gregorian and the Julian calendar is the "leap-year rule". In Julian calendar, every four years is a leap year. In Gregorian calendar, a leap year is a year that is divisible by 4 but not divisible by 100, or it is divisible by 400, i.e., the Gregorian calendar omits century years which are not divisible by 400. Furthermore, Julian calendar considers the first day of the year as march 25th, instead of January 1st. This above algorithm work for Gregorian dates only. It is difficult to modify the above algorithm to handle pre- Gregorian dates. A better algorithm is to find the number of days from a known date.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!