Question: Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example, January would

Write a class named Month. 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 to 1. * A constructor that accepts the number of the month as an argument. It would set the monthNumber field to the value passed as the argument. If a value less that 1 or greater than 12 is passed, the constructor should set monthNumber to 1. * A constructor that accepts the name of the month, case insensitive, such as "January" or "february" as 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" (First letter capitalized and the rest lower-case). * 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 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 argument. 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.

I need to make a demo but I am getting stuck. //i need to print the corresponding month to the given number and visa versa //i also need to check if they are greater than, less than or the same

What I have so far:

***MAIN PROGRAM***

public class Month {

public int monthNumber;

public static String monthName;

// if less than 1 or bigger than 12 make it 1

public void setMonthNumber(int monthNumber) {

if (monthNumber < 1 || monthNumber > 12)

monthNumber = 1;

}

public int getMonthNumber() {

int monthNumber = 0;

switch (monthName) {

case "January":

monthNumber = 1;

break;

case "February":

monthNumber = 2;

break;

case "March":

monthNumber = 3;

break;

case "April":

monthNumber = 4;

break;

case "May":

monthNumber = 5;

break;

case "June":

monthNumber = 6;

break;

case "July":

monthNumber = 7;

break;

case "August":

monthNumber = 8;

break;

case "September":

monthNumber = 9;

break;

case "October":

monthNumber = 10;

break;

case "November":

monthNumber = 11;

break;

case "December":

monthNumber = 12;

break;

default:

System.out.println("Error");

}

return monthNumber;

}

public int toInt() {

return getMonthNumber();

}

// set number to corresponding month

public String getMonthName() {

String monthName;

switch (monthNumber) {

case 1:

monthName = "January";

break;

case 2:

monthName = "February";

break;

case 3:

monthName = "March";

break;

case 4:

monthName = "April";

break;

case 5:

monthName = "May";

break;

case 6:

monthName = "June";

break;

case 7:

monthName = "July";

break;

case 8:

monthName = "August";

break;

case 9:

monthName = "September";

break;

case 10:

monthName = "October";

break;

case 11:

monthName = "November";

break;

case 12:

monthName = "December";

break;

default:

monthName = "Error";

}

return monthName;

}

public String toString() {

return getMonthName();

}

// greater than argument

public boolean greaterThan(Month month) {

boolean answer;

if (monthNumber > month.getMonthNumber())

answer = true;

else

answer = false;

return answer;

}

// less than argument

public boolean lessThan(Month month1) {

boolean answer;

if (monthNumber < month.getMonthNumber())

answer = true;

else

answer = false;

return answer;

}

}

***DEMO***

import java.util.Scanner;

public class MonthDemo {

public static void main(String[] args) {

System.out.println("Enter a month number: ");

Scanner monthNumberInput = new Scanner(System.in);

int monthNumber;

monthNumber = monthNumberInput.nextInt();

System.out.println(" The Number of the Month you choose is: " + monthNumber);

}

}

//i need to print the corresponding month to the given number and visa versa //i also need to check if they are greater than, less than or the same

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!