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

The questions in chegg didn\'t work with me plz don\'t use them again Write a pr

ID: 3723541 • Letter: T

Question

The questions in chegg didn't work with me plz don't use them again

Write a program that converts dates from numerical month/day/year format to nor-

mal “month day, year” format (for example, 12/25/2000 corresponds to December

25, 2000). You will define three exception classes, one called

MonthException

,

another called

DayException

, and a third called

YearException

. If the user enters anything other than a legal month number (integers from

1 to 12

), your program will throw and catch a

MonthException

and ask the user to reenter the month.

Similarly, if the user enters anything other th an a valid day number (integers from

1 to either 28, 29, 30, or 31

, depending on the month and year), then your program will

throw and catch a

DayException

and ask the user to reenter the day. If the user enters

a year that is not in the range 1000 to 3000 (inclusive), then your program will throw

and catch a

YearException

and ask the user to reenter the year. (There is nothing

very special about the numbers 1000 and 3000 other than giving a good range of likely

dates.) See Self-Test Exercise 19 in Chapter 4 for details on leap years.

Explanation / Answer

Hello, I have a solution for you. Implemented everything as per the requirements. Defined following things in this answer.

// MonthException.java

public class MonthException extends Exception {

                public MonthException() {

                                /**

                                * Passing exception message to super class

                                */

                                super("ERROR: invalid month");

                }

}

// DayException.java

public class DayException extends Exception {

                public DayException() {

                                /**

                                * Passing exception message to super class

                                */

                                super("ERROR: invalid day");

                }

}

// YearException.java

public class YearException extends Exception{

                public YearException() {

                                /**

                                * Passing exception message to super class

                                */

                                super("ERROR: invalid year");

                }

}

// Test.java

import java.util.Scanner;

public class Test {

                static Scanner scanner;

                /**

                * Array to store month names

                */

                static String months[] = { "January", "February", "March", "April", "May",

                                                "June", "July", "August", "September", "October", "November",

                                                "December" };

                /**

                * Array to store days in each month

                */

                static int daysInMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

                public static void main(String[] args) {

                                scanner = new Scanner(System.in);

                                /**

                                * prompting the user to enter date in mm/dd/yyyy format

                                */

                                System.out.println("Enter a date ( mm/dd/yyyy ) :");

                                String dateString = scanner.nextLine();

                                String[] fields = dateString.split("/");/* splitting the input by '/' */

                                if (fields.length != 3) {

                                                /**

                                                * Invalid format is given

                                                */

                                                System.out.println("Invalid input given");

                                } else {

                                                boolean done;

                                                // stripping the year part

                                                int year = Integer.parseInt(fields[2]);

                                                /**

                                                * checking if the year is valid, if not, loops until a valid year

                                                * has been entered

                                                */

                                                if (year < 1000 || year > 3000) {

                                                                done = false;

                                                                /**

                                                                * loops until a valid year entered

                                                                */

                                                                while (!done) {

                                                                                try {

                                                                                                year = getYear();

                                                                                                /**

                                                                                                * if the year is invalid, getYear() method will throw

                                                                                                * YearException, so the control wont reach here. If the

                                                                                                * year is read without any exceptions, breaking the

                                                                                                * loop

                                                                                                */

                                                                                                done = true;

                                                                                } catch (YearException e) {

                                                                                                System.out.println(e.getMessage());/*

                                                                                                                                                                                                                                                * displaying the

                                                                                                                                                                                                                                                * error msg

                                                                                                                                                                                                                                                */

                                                                                }

                                                                }

                                                }

                                                /**

                                                * Checking if the year is leap year, if yes, adjusting the days in

                                                * February

                                                */

                                                if (isLeapYear(year)) {

                                                                daysInMonth[1] = 29;

                                                }

                                                /**

                                                * Stripping the month field

                                                */

                                                int month = Integer.parseInt(fields[0]);

                                                /**

                                                * repeating the similar validation as above

                                                */

                                                if (month < 1 || month > 12) {

                                                                done = false;

                                                                while (!done) {

                                                                                try {

                                                                                                month = getMonth();

                                                                                                done = true;

                                                                                } catch (MonthException e) {

                                                                                                System.out.println(e.getMessage());

                                                                                }

                                                                }

                                                }

                                                int day = Integer.parseInt(fields[1]);

                                                if (day < 1 || day > daysInMonth[month - 1]) {

                                                                done = false;

                                                                while (!done) {

                                                                                try {

                                                                                                day = getDay(month);

                                                                                                done = true;

                                                                                } catch (DayException e) {

                                                                                                System.out.println(e.getMessage());

                                                                                }

                                                                }

                                                }

                                                /**

                                                * Displaying the validated year

                                                */

                                                System.out.println("The entered date is " + months[month - 1] + " "

                                                                                + day + ", " + year);

                                }

                }

                /**

                * method to prompt the user to enter a valid month

                *

                * @return - a month number between 1-12

                * @throws MonthException

                *             - if input month <1 or >12

                */

                static int getMonth() throws MonthException {

                                System.out.println("Enter a valid month: ");

                                int month = Integer.parseInt(scanner.nextLine());

                                if (month < 1 || month > 12) {

                                                throw new MonthException();

                                }

                                return month;

                }

                /**

                * method to prompt the user to enter a valid day

                *

                * @param month

                *            - specified month

                * @return - a day number between 1-maximum days in specified month

                * @throws DayException

                *             - if input day <1 or >maximum days in specified month

                */

                static int getDay(int month) throws DayException {

                                System.out.println("Enter a valid day: ");

                                int day = Integer.parseInt(scanner.nextLine());

                                if (day < 1 || day > daysInMonth[month - 1]) {

                                                throw new DayException();

                                }

                                return day;

                }

                /**

                * method to prompt the user to enter a valid year

                *

                * @return - a year between 1000-3000

                * @throws YearException

                *             - if input day <1000 or >3000

                */

                static int getYear() throws YearException {

                                System.out.println("Enter a valid year (1000-3000): ");

                                int year = Integer.parseInt(scanner.nextLine());

                                if (year < 1000 || year > 3000) {

                                                throw new YearException();

                                }

                                return year;

                }

                /**

                * method to check if a year is leap year or not

                */

                static boolean isLeapYear(int year) {

                                if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {

                                                //leap year

                                                return true;

                                }

                                //not a leap year

                                return false;

                }

}

/*OUTPUT 1*/

Enter a date ( mm/dd/yyyy ) :

02/05/2018

The entered date is February 5, 2018

/*OUTPUT 2*/

Enter a date ( mm/dd/yyyy ) :

5/32/1994

Enter a valid day:

25

The entered date is May 25, 1994

/*OUTPUT 3*/

Enter a date ( mm/dd/yyyy ) :

35/55/99

Enter a valid year (1000-3000):

8

ERROR: invalid year

Enter a valid year (1000-3000):

1994

Enter a valid month:

13

ERROR: invalid month

Enter a valid month:

8

Enter a valid day:

11

The entered date is August 11, 1994

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote