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

Write a class named LastNameMonth, replacing LastName with your last name. The c

ID: 3858347 • Letter: W

Question

Write a class named LastNameMonth, replacing LastName with your last name. The class should have an int field named monthNumber that holds the number of the month. For example, January would be 1, February would be 2, and so forth. In addition , provide the following methods:

A no-arg constructor that sets the monthNumber field to 1.

A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value is less than 1 or greater than 12 is passed, the constructor should set monthNumber to 1.

A constructor that accepts the name of the month, such as "January" or "February", as an argument. It should set the monthNumber field to the correct corresponding value.

A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1.

A getMonthNumber method that returns the value in the monthNumber field.

A getMonthName method that returns the name of the month. For example, if the monthNumber field contains 1, then this method should return "January".

A toString method that returns the same value as the getMonthName method.

An equals method that accepts a Month object as an argument. If the argument object holds the same data as the calling object, this method should return true. Otherwise it should return false.

A greaterThan method that accepts accepts a Month object as an argument. If the calling object's monthNumber field is greater than the argument's monthNumber field. this method should return true. Otherwise it should return false.

A lessThan method that accepts a Month object as an argurment. If the calling object's monthNumber field is less than the argument's monthNumber field, this method should return true. Otherwise, it should return false.

Demonstrate the class in a program that creates a three Month objects. One object is created with the no-arg constructor, one with the month's number and the final one with the month's name. Using any combination of the three objects, demonstrate each of the the remaining methods you created, displaying the output on the screen.

Explanation / Answer

public class Month {

private int monthNumber;

/**

*

*/

public Month() {

// TODOAuto-generatedconstructorstub

this.monthNumber = 1;

}

/**

* @parammonthNumber

*/

public Month(int monthNumber) {

if (monthNumber >= 1 && monthNumber <= 12)

this.monthNumber = monthNumber;

else

this.monthNumber = 1;

}

/**

* @param monthName

*/

public Month(String monthName) {

String[] months = { null, "January", "February", "March", "April",

"May", "June", "July", "August", "September", "October",

"November", "December" };

for (int i = 1; i < months.length; i++) {

if (monthName.equalsIgnoreCase(months[i]))

this.monthNumber = i;

}

}

/**

* @param monthNumber

*/

public void setMonthNumber(int monthNumber) {

if (monthNumber >= 1 && monthNumber <= 12)

this.monthNumber = monthNumber;

else

this.monthNumber = 1;

}

/**

* @return

*/

public int getMonthNumber() {

return monthNumber;

}

/**

* @param monthNumber

* @return

*/

public String getMonthName(int monthNumber) {

String[] months = { null, "January", "February", "March", "April",

"May", "June", "July", "August", "September", "October",

"November", "December" };

return months[monthNumber];

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#toString()

*/

@Override

public String toString() {

return "" + getMonthName(getMonthNumber());

}

/*

* (non-Javadoc)

*

* @see java.lang.Object#equals(java.lang.Object)

*/

@Override

public boolean equals(Object obj) {

// TODO Auto-generated method stub

if (obj instanceof Month) {

Month nameMonth = (Month) obj;

if (this.monthNumber == nameMonth.getMonthNumber())

return true;

else

return false;

} else

return false;

}

/**

* @param nameMonth

* @return

*/

public boolean greaterThan(Month nameMonth) {

// TODO Auto-generated method stub

if (this.monthNumber > nameMonth.getMonthNumber())

return true;

else

return false;

}

/**

* @param nameMonth

* @return

*/

public boolean lessThan(Month nameMonth) {

// TODO Auto-generated method stub

if (this.monthNumber < nameMonth.getMonthNumber())

return true;

else

return false;

}

}

public class MonthTest {

public static void main(String[] args) {

Month[] months = { new Month(), new Month(3), new Month("July") };

System.out.println(months[0]);

System.out.println(months[2]);

int i = 1;

System.out.println(months[i]);

System.out.println(months[i].getMonthName(3));

System.out.println(months[i].greaterThan(months[0]));

System.out.println(months[i].lessThan(months[0]));

System.out.println(months[i].equals(months[0]));

}

}

OUTPUT:

January
July
March
March
true
false
false

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