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

Given the following Month class in Java, write exception errors for the followin

ID: 3607304 • Letter: G

Question

Given the following Month class in Java, write exception errors for the following error conditions:
-A number less than 1 or greater than 12 is given for the month number.
-An invalid string is given for the name of the month.

Modify the Month class so that it throws the appropriate exception when either of these errors occur.
Then demonstrate the classes in a new program.

public class Month
{
private int monthNumber;

/**
The No-arg constructor initializes the object to
month #1.
*/

public Month()
{
monthNumber = 1;
}

/**
Constructor
@param m The month number to initialize the object to.
*/

public Month(int m)
{
if (m < 1 || m > 12)
monthNumber = 1;
else
monthNumber = m;
}

/**
Constructor
@param name The name of the month to initialize the object to.
*/

public Month(String name)
{
if (name.equalsIgnoreCase("january"))
monthNumber = 1;
else if (name.equalsIgnoreCase("february"))
monthNumber = 2;
else if (name.equalsIgnoreCase("march"))
monthNumber = 3;
else if (name.equalsIgnoreCase("april"))
monthNumber = 4;
else if (name.equalsIgnoreCase("may"))
monthNumber = 5;
else if (name.equalsIgnoreCase("june"))
monthNumber = 6;
else if (name.equalsIgnoreCase("july"))
monthNumber = 7;
else if (name.equalsIgnoreCase("august"))
monthNumber = 8;
else if (name.equalsIgnoreCase("september"))
monthNumber = 9;
else if (name.equalsIgnoreCase("october"))
monthNumber = 10;
else if (name.equalsIgnoreCase("november"))
monthNumber = 11;
else if (name.equalsIgnoreCase("december"))
monthNumber = 12;
else
monthNumber = 1;
}

/**
The setMonthNumber method sets the month.
@param m The number of the month.
*/

public void setMonthNumber(int m)
{
if (m < 1 || m > 12)
monthNumber = 1;
else
monthNumber = m;
}

/**
The getMonthNumber method gets the month number.
@return The number of the month.
*/

public int getMonthNumber()
{
return monthNumber;
}

/**
The getMonthName method gets the name of the month.
@return The name of the month.
*/

public String getMonthName()
{
String name;
  
switch (monthNumber)
{
case 1: name = "January";
break;
case 2: name = "February";
break;
case 3: name = "March";
break;
case 4: name = "April";
break;
case 5: name = "May";
break;
case 6: name = "June";
break;
case 7: name = "July";
break;
case 8: name = "August";
break;
case 9: name = "September";
break;
case 10: name = "October";
break;
case 11: name = "November";
break;
case 12: name = "December";
break;
default: name = "Unknown";
}
  
return name;
}

/**
toString method
@return A reference to a String representation of
the object.
*/

public String toString()
{
return getMonthName();
}

/**
equals method
@param month2 Another Month object.
@return true if the two Month objects contain
the same month, false otherwise.
*/
public boolean equals(Month month2)
{
boolean status;
  
if (month2.getMonthNumber() == monthNumber)
status = true;
else
status = false;
  
return status;
}

/**
greaterThan method
@param month2 Another Month object.
@return true if the calling objects is greater
than the argument, false otherwise.
*/

public boolean greaterThan(Month month2)
{
boolean status;
  
if (monthNumber > month2.getMonthNumber())
status = true;
else
status = false;
  
return status;
}

/**
lessThan method
@param month2 Another Month object.
@return true if the calling objects is less
than the argument, false otherwise.
*/

public boolean lessThan(Month month2)
{
boolean status;
  
if (monthNumber < month2.getMonthNumber())
status = true;
else
status = false;
  
return status;
}
}

Explanation / Answer

Month.java

public class Month {
private int monthNumber;
/**
The No-arg constructor initializes the object to
month #1.
*/
public Month() {
monthNumber = 1;
}
/**
Constructor
@param m The month number to initialize the object to.
*/
public Month(int m) {
if (m < 1 || m > 12)
throw new IllegalArgumentException("**Month must be betwen 1- 12 **");
else
monthNumber = m;
}
/**
Constructor
@param name The name of the month to initialize the object to.
*/
public Month(String name) {
if (name.equalsIgnoreCase("january"))
monthNumber = 1;
else if (name.equalsIgnoreCase("february"))
monthNumber = 2;
else if (name.equalsIgnoreCase("march"))
monthNumber = 3;
else if (name.equalsIgnoreCase("april"))
monthNumber = 4;
else if (name.equalsIgnoreCase("may"))
monthNumber = 5;
else if (name.equalsIgnoreCase("june"))
monthNumber = 6;
else if (name.equalsIgnoreCase("july"))
monthNumber = 7;
else if (name.equalsIgnoreCase("august"))
monthNumber = 8;
else if (name.equalsIgnoreCase("september"))
monthNumber = 9;
else if (name.equalsIgnoreCase("october"))
monthNumber = 10;
else if (name.equalsIgnoreCase("november"))
monthNumber = 11;
else if (name.equalsIgnoreCase("december"))
monthNumber = 12;
else
throw new IllegalArgumentException("**Invalid Month Name **");
}
/**
The setMonthNumber method sets the month.
@param m The number of the month.
*/
public void setMonthNumber(int m) {
if (m < 1 || m > 12)
monthNumber = 1;
else
monthNumber = m;
}
/**
The getMonthNumber method gets the month number.
@return The number of the month.
*/
public int getMonthNumber() {
return monthNumber;
}
/**
The getMonthName method gets the name of the month.
@return The name of the month.
*/
public String getMonthName() {
String name;

switch (monthNumber) {
case 1:
name = "January";
break;
case 2:
name = "February";
break;
case 3:
name = "March";
break;
case 4:
name = "April";
break;
case 5:
name = "May";
break;
case 6:
name = "June";
break;
case 7:
name = "July";
break;
case 8:
name = "August";
break;
case 9:
name = "September";
break;
case 10:
name = "October";
break;
case 11:
name = "November";
break;
case 12:
name = "December";
break;
default:
name = "Unknown";
}

return name;
}
/**
toString method
@return A reference to a String representation of
the object.
*/

public String toString() {
return getMonthName();
}

/**
equals method
@param month2 Another Month object.
@return true if the two Month objects contain
the same month, false otherwise.
*/
public boolean equals(Month month2) {
boolean status;

if (month2.getMonthNumber() == monthNumber)
status = true;
else
status = false;

return status;
}
/**
greaterThan method
@param month2 Another Month object.
@return true if the calling objects is greater
than the argument, false otherwise.
*/

public boolean greaterThan(Month month2) {
boolean status;

if (monthNumber > month2.getMonthNumber())
status = true;
else
status = false;

return status;
}

/**
lessThan method
@param month2 Another Month object.
@return true if the calling objects is less
than the argument, false otherwise.
*/

public boolean lessThan(Month month2) {
boolean status;

if (monthNumber < month2.getMonthNumber())
status = true;
else
status = false;

return status;
}
}

_________________

Test.java

import java.util.Scanner;

public class Test {

public static void main(String[] args) {
// Declaring variables
int month1, month2;
String monthName;

/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);

Month m1 = null, m2 = null;

while (true) {
// Getting the input entered by the user
System.out.print("Enter Month (Between 1-12):");
month1 = sc.nextInt();
try {
m1 = new Month(month1);
break;
} catch (Exception e) {
System.out.println(e);
continue;
}
}

while (true) {
// Getting the input entered by the user
System.out.print("Enter Month name:");
monthName = sc.next();
try {
m2 = new Month(monthName);
break;
} catch (Exception e) {
System.out.println(e);
continue;
}
}

if (m1.getMonthNumber() == m2.getMonthNumber())
System.out.println("The two months are equal");
else if (m1.greaterThan(m2))
System.out.println("Month#" + m1.getMonthNumber() + " is greater than Month#" + m2.getMonthNumber());
else if (m1.lessThan(m2))
System.out.println("Month#" + m1.getMonthNumber() + " is less than Month#" + m2.getMonthNumber());

}

}

___________________

Output:

Enter Month (Between 1-12):13
java.lang.IllegalArgumentException: **Month must be betwen 1- 12 **
Enter Month (Between 1-12):-12
java.lang.IllegalArgumentException: **Month must be betwen 1- 12 **
Enter Month (Between 1-12):6
Enter Month name:Hello
java.lang.IllegalArgumentException: **Invalid Month Name **
Enter Month name:June
The two months are equal

_____________Could you rate me well.Plz .Thank You

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