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.
Following is the answer:
Months.java
public class Months { private int monthNumber; private String[] monthNames = {"null", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; //constructor with no arguments Months(){ this.monthNumber = 1; } //constructor with one argument monthNumber Months(int monthNumber){ //exception if monthNumber is > 13 and < 0 try { if(monthNumber > 0 && monthNumber < 13){ this.monthNumber = monthNumber; }else { throw new IllegalArgumentException(); } }catch (IllegalArgumentException e){ System.out.println("Invalid month"); } } //get method to return monthNumber public int getMonthNumber() { return monthNumber; } //set method to set monthNumber public void setMonthNumber(int monthNumber) { try { if(monthNumber > 0 && monthNumber < 13){ this.monthNumber = monthNumber; }else { throw new IllegalArgumentException(); } }catch (IllegalArgumentException e){ System.out.println("Invalid month"); } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Months months = (Months) o; return monthNumber == months.monthNumber; } public int compareTo(Object o){ return this.compareTo(o); } }
MonthsMain.java
import java.util.Scanner; public class MonthsMain { public static void main(String[] args) { Scanner sc = new Scanner(System.in); Months month1 = new Months(); Months months = new Months(12); int month; do{ System.out.println("Enter a month (between 1-12)"); month = sc.nextInt(); }while (month < 1 || month > 12); month1.setMonthNumber(month); System.out.println("Month1: " + month1.getMonthNumber()); System.out.println("Month1: " + months.getMonthNumber()); } }
Output:
Get Answers For Free
Most questions answered within 1 hours.