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

Write a class called DateConverter which has a static method that takes in a dat

ID: 3567548 • Letter: W

Question

Write a class called DateConverter which has a static method that takes in a date as a string (such as 01/21 or 1/21) and then returns a string with the date formatted with the alphanumeric month (such as January 21). This method throws two different exceptions DayException and MonthException, which you need to write yourself. The MonthException is thrown if the numeric month is not 1-12, and the DateException is thrown if the day is less than 0 or greater than the max possible day for that month. In other words:

     January 31 days

     February 29 days

     March 31 days

     April 30 days

     May 31 days

     June 30 days

     July 31 days

     August 31 days

     September 30 days

     October 31 days

     November 30 days

     December 31 days

Finally write a driver DateConverterDriver that prompts the user for a date and then using the DateConverter either converts it or returns the exception message. Keep running this until they choose to quit.

Example Dialog:

Welcome to the date converter!

Enter a numeric date formatted as month/day or Exit to quit.

1/21

The date is January 21

Enter a numeric date formatted as month/day or Exit to quit.

-1/30

Month Exception: Months must between 1 and 12 inclusively.

Enter a numeric date formatted as month/day or Exit to quit.

4/31

Day Exception: This day is in the wrong range for the month provided.

Enter a numeric date formatted as month/day or Exit to quit.

Quit

Good bye

Explanation / Answer

Here you go :)

Its a good practise to keep all the userdefind exceptions under one super class

Here DateException is superclass of DayException and MonthException

//DateException

public class DateException extends Exception
{
   public DateException(String error)
   {
       super(error);
   }
}

//DayException


public class DayException extends DateException
{
   public DayException(String error)
   {
       super("Day Exception: "+error);
   }
}

//MonthException


public class MonthException extends DateException
{
   public MonthException(String error)
   {
       super("Month Exception: "+error);
   }
}

//DateConverter class

public class DateConverter {
   static int[] days = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
   static String[] names = { "January", "February", "March", "April", "May", "June",
           "July", "August", "September", "October", "December" };

   public static String getInput(String in) throws DateException {
       String ret = "";
       String[] subs = in.split("/");
       int month = Integer.parseInt(subs[0]);
       int date = Integer.parseInt(subs[1]); // you can add try catch to
                                               // NumberFormatException

       if (month < 0 || month > 12) {
           throw new MonthException(
                   "Months must between 1 and 12 inclusively.");
       }
       if (date < 0 || date > days[month - 1]) {
           throw new DayException(
                   "This day is in the wrong range for the month provided.");
       }
       System.out.println("The date is " + names[month - 1] + " " + date);
       return ret;
   }

}

//Driver class

import java.util.Scanner;

public class DateConverterDriver {
   public static void main(String[] args) {
       Scanner keyboard = new Scanner(System.in);

       boolean quit = false;
       System.out.println("Welcome to the date converter!");
       while (quit == false) {
           System.out
                   .println("Enter a numeric date formatted as month/day or

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