Write a class named Month based on the UML class diagram shown ABOVE. Class Mont
ID: 3806069 • Letter: W
Question
Write a class named Month based on the UML class diagram shown ABOVE.
Class Month will need an integer instance field named monthNumber that holds the number of the month (For example, 1 is January, 2 is February, etc.)
A static String array may be used to hold the names of the month: "January", "February", etc.
• Include two constructor methods
1. A constructor method that accepts an integer argument from 1..12 corresponding to the month number .
• The method should throw an IllegalArgumentException if the number passed in is outside the range 1..12.
• Set the exception error message to "Illegal month number: " followed by the illegal value passed in
2. A constructor method that accepts a String argument. The argument may take the form "1" or "2", etc. or "January", "February", etc.
• The method should throw an IllegalArgumentException if the String Program 5 passed in is not between "1" .. "12" or not "January", "February", etc.
• Ignore casing, so "JANUARY" or "janUary" should be considered equivalent to "January"
• Include two setter methods
1. setMonthNumber that accepts an integer argument between 1..12. Throw an IllegalArgumentException if the parameter value is outside the range 1..12
2. setMonthName that accepts a String argument "January", "February", etc. Throw an IllegalArgumentException if the parameter is not "January", "February", etc. Ignore casing, so "JANUARY" or "janUary" should be considered equivalent to "January"
• Include two getter methods
1. getMonthNumber that returns an integer value 1..12 corresponding to the month object
2. getMonthName that returns a String value "January", "February", etc. corresponding to the month object
• A toString method that returns the month's name "January", "February", etc.
• A equals method that returns true if the Month argument share the same month, false otherwise
• A next method that returns a Month object set to the next month. For example, a February Month object would return a March Month object
Note: calling next() on a December Month object should return a January Month object
A previous method that returns a Month object set to the previous month. For example, a February Month object would return a January Month object
Note: calling previous() on a January Month object should return a December Month object
Month jGRASP UML class diagram SHOWN BELOW
Note: method <cinit >is automatically generated to create the private static String [] for monthNames private static String[] monthNames = {"January", "February", "March", "April","May", "June", "July", "August", "September", "October", "November", "December"};
Unit testing the Month class Constructor method and getter method unit tests
Constructor method exceptions thrown
Month m4 = new Month(13)
Exception in evaluation thread
java.lang.IllegalArgumentException: Illegal month number: 13
Month m5 = new Month("Saturday")
Exception in evaluation thread
java.lang.IllegalArgumentException: Invalid month name: Saturday
Month m5 = new Month("Jan")
Exception in evaluation thread
java.lang.IllegalArgumentException: Invalid month name: Jan
Month m5 = new Month("-1")
Exception in evaluation thread
java.lang.IllegalArgumentException: Illegal month number: -1
Month m5 = new Month("13")
Exception in evaluation thread
java.lang.IllegalArgumentException: Illegal month number: 13
Setter method unit tests
Setter method exceptions thrown
m1.setMonthNumber(0)
Exception in evaluation thread
java.lang.IllegalArgumentException: Illegal month number: 0
m1.setMonthName("Fall")
Exception in evaluation thread
java.lang.IllegalArgumentException: Invalid month name: Fall
equals, next, previous method unit tests
Main method demo class
Write a MonthDemo class that contains a main method with a try….catch construct to
• Prompt the user to enter a month (as a String). Allow the user to enter the string "1", "2", etc. or "January", etc.
• try to create the Month object, passing in the user input (as a string) o catch any exception thrown from the constructor method o display the exception object's error message o re-prompt the user to enter a month (until they get it right)
• Display the month entered (use the Month object's toString method
MonthDemo main method
GET THE PROGRAM TO RUN WITH CORRECT DATA THEN WORRY ABOUT THE EXCEPTION HANDLING
What to turn in
Your program will be graded directly online. No source printout is required. Ø
For this Assignment, I want a NetBeans Complete Project
UNIT TESTING OUTPUT
Month monthNumber int monthNames StringDE ("January"... Month (m int) Month (s String) setMonthNumber (m int) void setMonthName (name String) void get MonthNumber 0 int getMonthName 0 String toString0 String equals(m Month) boolean next0 Month previous0 MonthExplanation / Answer
PROGRAM CODE:
Month.java
package array2;
public class Month {
private int monthNumber;
private static String monthNames[] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
public Month(int month) {
setMonthNumber(month);
}
public Month(String month)
{
if(Character.isDigit(month.charAt(0)) || month.charAt(0) == '-')
{
try
{
setMonthNumber(Integer.valueOf(month));
}catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid month name: " + month);
}
}
else
{
setMonthName(month);
}
}
public void setMonthNumber(int month)
{
if(month>12 || month <1)
throw new IllegalArgumentException("Illegal month number: " + month);
else
this.monthNumber = month;
}
public void setMonthName(String name)
{
name = name.toLowerCase();
for(int i=0; i<monthNames.length; i++)
{
if(name.equals(monthNames[i].toLowerCase()))
{
monthNumber = i+1;
return;
}
}
throw new IllegalArgumentException("Invalid month name: " + name);
}
public int getMonthNumber()
{
return monthNumber;
}
public String getMonthName()
{
return monthNames[monthNumber-1];
}
@Override
public String toString() {
return getMonthName();
}
@Override
public boolean equals(Object obj) {
Month month = (Month)obj;
return this.monthNumber == month.monthNumber;
}
public Month next()
{
int newMonth = 0;
if(this.monthNumber<12)
newMonth = this.monthNumber+1;
else newMonth = 1;
Month month = new Month(newMonth);
return month;
}
public Month previous()
{
int newMonth = 0;
if(this.monthNumber>1)
newMonth = this.monthNumber-1;
else newMonth = 12;
Month month = new Month(newMonth);
return month;
}
}
MonthDemo.java
package array2;
import java.util.Scanner;
public class MonthDemo {
public static void unitTests()
{
//constructor tests
Month m1 = new Month(5);
System.out.println(m1.toString());
Month m2 = new Month("3");
System.out.println(m2.toString());
Month m3 = new Month("December");
System.out.println(m3.toString());
Month m4 = new Month(13);
System.out.println(m4.toString());
Month m5 = new Month("Saturday");
System.out.println(m5.toString());
Month m6 = new Month("Jan");
System.out.println(m6.toString());
Month m7 = new Month("-1");
System.out.println(m7.toString());
Month m8 = new Month("13");
System.out.println(m8.toString());
//setter methods
m1.setMonthNumber(0);
System.out.println(m1.toString());
m1.setMonthName("Fall");
System.out.println(m1.toString());
}
public static void main(String args[])
{
//call this method to check if the Month class is working fine or not
//unitTests();
Scanner keyboard = new Scanner(System.in);
String monthName;
boolean isWrong = true;
Month m1;
while(isWrong)
{
System.out.print("Enter a month: ");
monthName = keyboard.nextLine();
try
{
m1 = new Month(monthName);
}catch (IllegalArgumentException e) {
System.out.println(e.getMessage());
continue;
}
System.out.println("You entered " + m1.getMonthName());
isWrong = false;
}
}
}
OUTPUT:
Enter a month: -1
Illegal month number: -1
Enter a month: 23
Illegal month number: 23
Enter a month: xxx
Invalid month name: xxx
Enter a month: Jan
Invalid month name: jan
Enter a month: january
You entered January
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.