In: Computer Science
Create a simple Java class for a Month object with the following requirements:
This program will have a header block comment with your name, the
course and section, as well as a brief description of what the
class does.
All methods will have comments concerning their purpose, their
inputs, and their outputs
One integer property: monthNumber (protected to only allow values
1-12). This is a numeric representation of the month (e.g. 1
represents January, 2 represents February, etc.)
A constructor that takes no arguments, and sets the monthNumber
to 1.
Add a second constructor that takes in an integer argument to set
the initial monthNumber for the new Month object. Use data
protection to prevent the user from entering a number less than 1
or greater than 12. When a non-valid input is entered, throw a new
IllegalArgumentException.
A setMonth() method that takes an integer and uses data
protection to prevent the user from entering a number less than 1
or greater than 12. Also throw an IllegalArgumentException if an
illegal value is entered.
A getMonth() method that returns the monthNumber as an
integer.
Add a String array property that holds the values of the month
names (e.g. monthNames[3] would hold the value “March”). Remember,
you can leave the 0th index blank/null
Add a toString() method to use the monthNumber property to return
the name of the month as a String. Use the private global String
array with the names of the months in it to return the proper
String based on the monthNumber.
Add an equals() method that takes in a month object and returns a
boolean based on the values of each object’s monthNumber
Add a compareTo() method that takes in a month object and returns
a negative number if the called object is smaller than the passed
in object, a positive number if the called object is bigger than
the passed in object, and zero (0) if the two objects are
equivalent.
Create a simple program using Java that demonstrates the month object with the following requirements:
That creates a month object using the no argument
constructor.
A second month object is created using the constructor that takes
in an integer argument.
Additionally, use either a do or while loop to get the user to
enter a number between 1 and 12 using the setMonth() method on the
1st month object. The loop will continue until they enter a valid
number.
The program will display the month number for both of the objects
using the getMonth() method.
Display the month names using toString() for the months created,
and see whether they are the same or not.
Additionally, use the equals() method created above to show
whether the two months are equivalent to each other or not.
Use the compareTo() method created above to show which object is
the biggest.
Use appropriate try and catch statements to properly handle
erroneous input by the user.
/* The java program that demonstrates the
methods
* of Month class. Then print the results of the program
* on console output.
* */
//MonthTester.java
import java.util.Scanner;
public class MonthTester
{
public static void main(String[] args)
{
Scanner console=new
Scanner(System.in);
//default constructor object
Month one=new Month();
//second Month object with month
number =1
Month second=new Month(1);
//declare month variable
int month = 0;
//do-while loop
do
{
//try-catch
block to handle invalid input
try
{
System.out.printf("Enter month number :
");
month=Integer.parseInt(console.nextLine());
if(month<1 || month>12)
System.out.println("Invalid
month number");
else
second.setMonth(month);
}
catch (Exception
e)
{
System.out.println("invalid input");
}
}while(month<1 ||month>12);
//print one and second
objects
System.out.println("Default month
object");
System.out.println(one.toString());
System.out.println("Second month
object");
System.out.println(second.toString());
//checking if equal method
one two objects
if(one.equals(second))
System.out.println(one+" and "+second+" are same objects");
else
System.out.println(one+" and "+second+" are not same
objects");
//calling compareTo method on two
month objects
if(second.compareTo(one)>0)
System.out.println(second+" is more than "+one);
else
if(second.compareTo(one)<0)
System.out.println(second+" is less than "+one);
else
System.out.println(second+" and "+one+" are same
objects");
}
}
---------------------------------------------------------------------------------------------------------
/** The month class has an instance variable
monthNumber
* The constructor and parameterized constructor is to set
* the default and parameter values to the month class.
* The class has setter and getter method for instance
variable,
* monthNumber.
* */
//Month.java
public class Month implements Comparable
{
//declare an instance variable
private int monthNumber;
//declare an array of month names
private String months[]= {"",
"January","February","March","April","May","June","July",
"August","September","October","November","December"};
//default constructor
public Month()
{
monthNumber=1;
}
//parameter constructor
public Month(int month)
{
if(month<1 || month>12)
monthNumber=1;
else
monthNumber=month;
}
//settter method for month
public void setMonth(int month)
{
if(month<1 || month>12)
monthNumber=1;
else
monthNumber=month;
}
//getter method for month
public int getMonth()
{
return monthNumber;
}
/*Override the toString method*/
public String toString()
{
return String.format("Month Name:
%s", months[monthNumber]);
}
/*Override the equals method*/
public boolean equals(Object obj)
{
Month other=(Month)obj;
return
monthNumber==other.getMonth();
}
/*Override the compareTo method*/
public int compareTo(Object obj)
{
Month other=(Month)obj;
return
monthNumber-other.getMonth();
}
}
---------------------------------------------------------------------------------------------------------
Sample Output:
RUn1:
Enter month number : 3
Default month object
Month Name: January
Second month object
Month Name: March
Month Name: January and Month Name: March are not same
objects
Month Name: March is more than Month Name: January
Run2:
Enter month number : 1
Default month object
Month Name: January
Second month object
Month Name: January
Month Name: January and Month Name: January are same objects
Month Name: January and Month Name: January are same
objects