Based on Programming Challenges #4: Month Class Exceptions on page 765 of your textbook.
Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. The UML class diagram for Month is given below.
Write two exception classes for the following error conditions.
InvalidMonthNumberException if a number less than 1 or greater than 12 is given for the month numberInvalidMonthNameException if an invalid string is given for the name of a monthDesign the Month class so it throws the appropriate exception when either of these errors occurs. Use the Driver code below to test your Month class.
public class Month
{
private int monthNumber;
private static String[] monthNames =
{"January","February","March","April","May", "June",
"July","August","September","October","November","December"};
// No-arg constructor: Sets monthNumber to 1
public Month()
// Constructor: accepts month number as integer
public Month(int number) throws InvalidMonthNumberException
// Constructor: accepts month name as String
public Month(String name) throws InvalidMonthNameException
// Setter method: accepts month number as integer
public void setMonth(int number) throws InvalidMonthNumberException
// Setter method: accepts month name as String
public void setMonth(String name) throws InvalidMonthNameException
// Getter method: returns monthNumber as int
public int getMonthNumber()
// Getter method: returns appropriate month name as String
public String getMonthName()
}
import java.util.Scanner;
public class Driver
{
public static void main(String[] args)
{
// Declare and instantiate a Scanner object for user input
Scanner keyboard = new Scanner(System.in);
// Declare a Month object to test program
Month nextMonth;
// Prompt for month and read from user
System.out.print("Enter a month (either 1..12, a month name, or quit to stop): ");
String userInput = keyboard.nextLine();
// Keep looping until the user wants to quit
while (!userInput.equalsIgnoreCase("quit"))
{
try
{
try
{
// try to accept month as an integer value
int monthNumber = Integer.parseInt(userInput);
nextMonth = new Month(monthNumber);
System.out.println("Created month " +
nextMonth.getMonthNumber() + " as " +
nextMonth.getMonthName());
}
catch (NumberFormatException e)
{
// Month not valid integer, process as month name
nextMonth = new Month(userInput);
System.out.println("Created month " +
nextMonth.getMonthName() + " as " +
nextMonth.getMonthNumber());
}
}
catch (InvalidMonthNumberException e)
{
System.out.println(e.getMessage());
}
catch (InvalidMonthNameException e)
{
System.out.println(e.getMessage());
}
// Prompt for and read in next month
System.out.print("\nEnter a month (either 1..12, a month name, or quit to stop): ");
userInput = keyboard.nextLine();
}
}
}
Enter a month (either 1..12, a month name, or quit to stop):4Created month 4 as April Enter a month (either 1..12, a month name, or quit to stop):MayCreated month May as 5 Enter a month (either 1..12, a month name, or quit to stop):13Error: 13 is an invalid month number Enter a month (either 1..12, a month name, or quit to stop):MondayError: Monday is an invalid month name Enter a month (either 1..12, a month name, or quit to stop):quit
u:\Program5\ folder50 points maximum
InvalidMonthNumberException with complete exception message (5)InvalidMonthNameException with complete exception message (5)Month class methods avoid duplication of code
InvalidMonthNumberException derived from Exception InvalidMonthNameException derived from Exception U:\Program5\ created correctly in your network cs111 account and project files saved correctly in the project folder (2)