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

Create a class called Month (java file called Month.java) with the (exact) follo

ID: 3866987 • Letter: C

Question

Create a class called Month (java file called Month.java) with the (exact) following fields and methods (these names and caps exactly) Field/Method Description MonthNumber Month Month An int field that holds the number of the month (values from 1 to 12) A no-argument constructor that sets the MonthNumber field to 1 A constructor that accept the number of the month as an argument (1, 2, 3 etc) and sets the MonthNumber field to that value. If a value less than 1 or4 larger that 12 is passed, the constructor should set MonthNumber to 1 A constructor that accept the name of the month as an argument 7 ("January" February", etc) and sets the MonthNumber field to the8 corresponding number MonthNumber Name Februa March ril Ma June Month August September October November December 10 GetMonthNumberAethod that returns the MonthNumber field value SetMonthNumber | A method that accept an int number as an argument and sets the | 12 MonthNumber field to that number GetMonthName A method that returns the name of the month corresponding to the MonthNumber field value SetMonthName A method that accept a string value as an argument (representing the name of the month: "January", February", etc.) and sets the MonthNumber field to the corresponding number (1, 2, etc) A method that accepts a Month object as an argument and returns true if the data from that object (its MonthNumber) is the same as the MonthNumber field or false otherwise A method that accepts a Month object as an argument and returns true if the data from that object (its MonthNumber) is greater than the MonthNumber field or false otherwise A method that accepts a Month object as an argument and returns true if the data from that object (its MonthNumber) is less than the MonthNumber field or false otherwise Equals GreaterThan LessThan

Explanation / Answer

Hi,

Pleasesee below the answer:

Thanks!

Month.java

public class Month {

private int monthNumber;

//no arg constructor

public Month(){

this.monthNumber = 1;

}

//parameterised constructor

public Month(int monthNumber){

if(monthNumber> 0 && monthNumber<=12){

this.monthNumber = monthNumber;

}

else{

this.monthNumber = 1;

}

}

//parameterised constructor

public Month(String monthName){

if("January".equalsIgnoreCase(monthName)){

this.monthNumber = 1;

}

else if("February".equalsIgnoreCase(monthName)){

this.monthNumber = 2;

}

else if("March".equalsIgnoreCase(monthName)){

this.monthNumber = 3;

}

else if("April".equalsIgnoreCase(monthName)){

this.monthNumber = 4;

}

else if("May".equalsIgnoreCase(monthName)){

this.monthNumber = 5;

}

else if("June".equalsIgnoreCase(monthName)){

this.monthNumber = 6;

}

else if("July".equalsIgnoreCase(monthName)){

this.monthNumber = 7;

}

else if("August".equalsIgnoreCase(monthName)){

this.monthNumber = 8;

}

else if("September".equalsIgnoreCase(monthName)){

this.monthNumber = 9;

}

else if("October".equalsIgnoreCase(monthName)){

this.monthNumber = 10;

}

else if("November".equalsIgnoreCase(monthName)){

this.monthNumber = 11;

}

else if("Decemeber".equalsIgnoreCase(monthName)){

this.monthNumber = 12;

}

}

public int getMonthNumber() {

return monthNumber;

}

public void setMonthNumber(int monthNumber) {

this.monthNumber = monthNumber;

}

public String getMonthName() {

String monthName = "";

if(1==this.monthNumber){

monthName = "January";

}

else if(2==this.monthNumber){

monthName = "February";

}

else if(3==this.monthNumber){

monthName = "March";

}

else if(4==this.monthNumber){

monthName = "April";

}

else if(5==this.monthNumber){

monthName = "May";

}

else if(6==this.monthNumber){

monthName = "June";

}

else if(7==this.monthNumber){

monthName = "July";

}

else if(8==this.monthNumber){

monthName = "August";

}

else if(9==this.monthNumber){

monthName = "September";

}

else if(10==this.monthNumber){

monthName = "October";

}

else if(11==this.monthNumber){

monthName = "November";

}

else if(12==this.monthNumber){

monthName = "Decemeber";

}

return monthName;

}

public void setMonthName(String monthName) {

if("January".equalsIgnoreCase(monthName)){

this.monthNumber = 1;

}

else if("February".equalsIgnoreCase(monthName)){

this.monthNumber = 2;

}

else if("March".equalsIgnoreCase(monthName)){

this.monthNumber = 3;

}

else if("April".equalsIgnoreCase(monthName)){

this.monthNumber = 4;

}

else if("May".equalsIgnoreCase(monthName)){

this.monthNumber = 5;

}

else if("June".equalsIgnoreCase(monthName)){

this.monthNumber = 6;

}

else if("July".equalsIgnoreCase(monthName)){

this.monthNumber = 7;

}

else if("August".equalsIgnoreCase(monthName)){

this.monthNumber = 8;

}

else if("September".equalsIgnoreCase(monthName)){

this.monthNumber = 9;

}

else if("October".equalsIgnoreCase(monthName)){

this.monthNumber = 10;

}

else if("November".equalsIgnoreCase(monthName)){

this.monthNumber = 11;

}

else if("Decemeber".equalsIgnoreCase(monthName)){

this.monthNumber = 12;

}

}

public boolean equals(Month month){

if(this.monthNumber == month.getMonthNumber()){

return true;

}

return false;

}

public boolean greaterThan(Month month){

if(month.getMonthNumber() > this.monthNumber){

return true;

}

return false;

}

public boolean lessThan(Month month){

if(month.getMonthNumber() < this.monthNumber){

return true;

}

return false;

}

}

Assignment2.java

public class Assignment2 {

public static void main(String [] args){

Month Month1 = new Month();

Month Month2 = new Month(2);

Month Month3 = new Month("October");

System.out.println("Month1: "+Month1.getMonthNumber());

System.out.println("Month2: "+Month2.getMonthNumber());

System.out.println("Month3: "+Month3.getMonthNumber());

System.out.println("Month1: "+Month1.getMonthName());

System.out.println("Month2: "+Month2.getMonthName());

System.out.println("Month3: "+Month3.getMonthName());

Month1.setMonthNumber(9);

Month2.setMonthName("September");

System.out.println("Month1: "+Month1.getMonthName());

System.out.println("Month2: "+Month2.getMonthName());

System.out.println("Month3: "+Month3.getMonthName());

System.out.println("Month1 Equals Month2 ? "+Month1.equals(Month2));

System.out.println("Month2 greaterThan Month3 ? "+Month2.equals(Month3));

System.out.println("Month3 lessThan Month1 ? "+Month3.equals(Month1));

Month Month4 = new Month();

for(int i=1;i<=12;i++){

Month4.setMonthNumber(i);

System.out.println("Month Number : "+i+",Month Name : "+Month4.getMonthName());

}

}

}

Sample output:

Month1: 1
Month2: 2
Month3: 10
Month1: January
Month2: February
Month3: October
Month1: September
Month2: September
Month3: October
Month1 Equals Month2 ? true
Month2 greaterThan Month3 ? false
Month3 lessThan Month1 ? false
Month Number : 1,Month Name : January
Month Number : 2,Month Name : February
Month Number : 3,Month Name : March
Month Number : 4,Month Name : April
Month Number : 5,Month Name : May
Month Number : 6,Month Name : June
Month Number : 7,Month Name : July
Month Number : 8,Month Name : August
Month Number : 9,Month Name : September
Month Number : 10,Month Name : October
Month Number : 11,Month Name : November
Month Number : 12,Month Name : Decemeber

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