Write a class named Month based on the UML class diagram shown ABOVE. Class Mont
ID: 3806150 • 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
import java.util.Scanner;
public class Month {
private int monthNumber;
// private String monthName;
private static String[] monthNames = {"January", "February", "March", "April","May", "June", "July", "August", "September",
"October", "November", "December"};
public Month(String s) throws IllegalAccessException{
boolean found = false;
int ind = 1;
for(String str : monthNames){
if(s.equalsIgnoreCase(str)){
this.monthNumber = ind;
found = true;
break;
}
ind++;
}
if(!found){
throw new IllegalArgumentException("Invalid month name:"+s);
}
}
public Month(int m){
this.monthNumber = m;
if(m<1 || m>12){
throw new IllegalArgumentException("Illegal month number:"+m);
}
}
public void setMonthNumber(int m){
if(m<1 || m>12){
throw new IllegalArgumentException("Illegal month number:"+m);
}
this.monthNumber = m;
}
public void setMonthName(String s){
int ind = 1;
boolean found = false;
for(String str : monthNames){
if(s.equalsIgnoreCase(str)){
this.monthNumber = ind;
found = true;
break;
}
ind++;
}
if(!found){
throw new IllegalArgumentException("Invalid month name:"+s);
}
}
public int getMonthNumber(){
return this.monthNumber;
}
public String getMonthName(){
return monthNames[this.monthNumber-1];
}
public String toString(){
return monthNames[this.monthNumber-1];
}
public boolean equals(Month month){
if(month.getMonthNumber()==this.monthNumber)
return true;
else
return false;
}
public Month next(){
int val = this.monthNumber;
if(val==12){
Month month = new Month(1);
return month;
}else{
return new Month(val+1);
}
}
public Month previous(){
int val = this.monthNumber;
if(val==1){
Month month = new Month(12);
return month;
}else{
return new Month(val-1);
}
}
public static void main(String args[]) throws IllegalAccessException{
boolean run = true;
while(run){
System.out.println("Enter a month");
Scanner sc = new Scanner (System.in);
String str = sc.next();
int m;
try{
m = Integer.parseInt(str);
if(m<1 || m>12)
System.out.println("Illegal Month Number:"+m);
else{
System.out.println("You entered"+new Month(m).toString());
run = false;
}
}catch(Exception e){
boolean found = false;
for(String s : monthNames){
if(s.equalsIgnoreCase(str)){
System.out.println("You entered"+new Month(s).toString());
run = false;
break;
}
}
if(!found){
throw new IllegalArgumentException("Invalid month name:"+str);
}
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.