Develop an exception class named InvalidMonthException that extends the Exceptio
ID: 3708756 • Letter: D
Question
Develop an exception class named InvalidMonthException that extends the Exception class. Instances of the class will be thrown based on the following conditions:
The value for a month number is not between 1 and 12
The value for a month name is not January, February, March, … December
Develop a class named Month. The class should define an integer field named monthNumber that holds the number of a month. For example, January would be 1, February would be 2, and so forth. In addition, provide the following methods:
A no-argument constructor that sets the monthNumber to 1.
An overloaded constructor that accepts the number of the month as an argument. The constructor should set the monthNumber field to the parameter value if the parameter contains the value 1 – 12. Otherwise, throw an InvalidMonthException exception back to the caller. The exception should note that the month number was incorrect.
An overloaded constructor that accepts a string containing the name of the month, such as “January” and sets the monthNumber based on the parameter string. When the parameter value does not match a valid name of a month, the constructor throws an InvalidMonthException exception back to the caller. The exception should note an invalid month name was used.
Accessor and mutator methods for the monthNumber field. The mutator method must validate the monthNumberparameter and throw an InvalidMonthException exception back to the caller when an invalid value is given.
A getMonthName method that returns the name of the month based upon the monthNumber field.
A toString method that returns the same value as the getMonthName method.
An equals method that accepts a Month object as a parameter and compares the object to the calling object to determine if the objects contain the same value for the monthNumber field.
Develop a test client to thoroughly test the Month class and handle the exceptions thrown.
Can you write this in java using jgrasp?
Explanation / Answer
InvalidMonthException.java
public class InvalidMonthException extends Exception {
InvalidMonthException(String ex)
{
System.out.println(ex);
}
}
_______________
Month.java
public class Month {
private int monthNumber;
public Month() {
this.monthNumber=1;
}
Month(int month) throws InvalidMonthException
{
if(month>=1 && month<=12)
this.monthNumber=month;
else
throw new InvalidMonthException("The month number was incorrect");
}
Month(String monthName) throws InvalidMonthException
{
if(monthName.equalsIgnoreCase("january"))
{
monthNumber=1;
}
else if(monthName.equalsIgnoreCase("february"))
{
monthNumber=2;
}
else if(monthName.equalsIgnoreCase("march"))
{
monthNumber=3;
}
else if(monthName.equalsIgnoreCase("april"))
{
monthNumber=4;
}
else if(monthName.equalsIgnoreCase("may"))
{
monthNumber=5;
}
else if(monthName.equalsIgnoreCase("june"))
{
monthNumber=6;
}
else if(monthName.equalsIgnoreCase("july"))
{
monthNumber=7;
}
else if(monthName.equalsIgnoreCase("august"))
{
monthNumber=8;
}
else if(monthName.equalsIgnoreCase("september"))
{
monthNumber=9;
}
else if(monthName.equalsIgnoreCase("october"))
{
monthNumber=10;
}
else if(monthName.equalsIgnoreCase("november"))
{
monthNumber=11;
}
else if(monthName.equalsIgnoreCase("december"))
{
monthNumber=12;
}
else
{
throw new InvalidMonthException("Invalid month name was used");
}
}
public int getMonthNumber() {
return monthNumber;
}
public void setMonthNumber(int monthNumber) throws InvalidMonthException {
if(this.monthNumber>=1 && monthNumber<=12)
this.monthNumber = monthNumber;
else
throw new InvalidMonthException("The month number was incorrect");
}
public String getMonthName() throws InvalidMonthException
{
String monthName="";
switch(monthNumber)
{
case 1:{
monthName="January";
break;
}
case 2:{
monthName="February";
break;
}
case 3:{
monthName="March";
break;
}
case 4:{
monthName="April";
break;
}
case 5:{
monthName="May";
break;
}
case 6:{
monthName="June";
break;
}
case 7:{
monthName="July";
break;
}
case 8:{
monthName="August";
break;
}
case 9:{
monthName="September";
break;
}
case 10:{
monthName="October";
break;
}
case 11:{
monthName="November";
break;
}
case 12:{
monthName="December";
break;
}
default:{
throw new InvalidMonthException("The month number was incorrect");
}
}
return monthName;
}
@Override
public String toString() {
String monthname="";
try {
monthname=getMonthName();
} catch (InvalidMonthException e) {
e.printStackTrace();
}
return monthname;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + monthNumber;
return result;
}
public boolean equals(Month other) {
if (monthNumber != other.monthNumber)
return false;
return true;
}
}
_____________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
int monthNumber;
String monthName;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
while (true) {
//Getting the input entered by the user
System.out.print("Enter the month Number :");
monthNumber = sc.nextInt();
try {
//Creating an instance of Month class
Month m = new Month(monthNumber);
System.out.println("Month Name :"+m.getMonthName());
break;
} catch (InvalidMonthException e) {
continue;
}
}
while (true) {
//Getting the input entered by the user
System.out.print("Enter the month Name :");
monthName = sc.next();
try {
//Creating an instance of Month class
Month m = new Month(monthName);
System.out.println("Month Number :"+m.getMonthNumber());
break;
} catch (InvalidMonthException e) {
continue;
}
}
}
}
__________________
Output:
Enter the month Number :14
The month number was incorrect
Enter the month Number :0
The month number was incorrect
Enter the month Number :6
Month Name :June
Enter the month Name :Hello
Invalid month name was used
Enter the month Name :May
Month Number :5
_______________Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.