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

I am in a intro class so we are not on arrays yet. We are on methods classes and objects

someone helped me yesterday but the code has errors

Here is what i have :

public class Month { public int monthNumber; public static String monthName;

public Month() {}

public Month(int monthNumber) { setMonthNumber(monthNumber);

} public Month(String monthName) { 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: monthNumber = 1; } }

// if less than 1 or bigger than 12 make it 1 public void setMonthNumber(int monthNumber) { if (monthNumber < 1 || monthNumber > 12) this.monthNumber = 1; else this.monthNumber = monthNumber;

}

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: monthNumber = 1; } return monthNumber; }

// 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 < month1.getMonthNumber()) answer = true; else answer = false; return answer; } }

//demo//

import java.util.Scanner;

public class MonthDemo {

public static void main(String[] args) {

//Declaring variables

int monthNum;

String monthName;

boolean bool;

/*

* Creating an Scanner class object which is used to get the inputs

* entered by the user

*/

Scanner sc = new Scanner(System.in);

//Getting the input entered by the user

System.out.print("Enter the month Number :");

monthNum = sc.nextInt();

Month m1 = new Month(monthNum);

System.out.println("The Corresponding Month Name is :" + m1.getMonthName());

//Getting the input entered by the user

System.out.print("Enter the month Name: ");

monthName = sc.next();

Month m2 = new Month(monthName);

System.out.println("The Corresponding Month Number is: " + m2.getMonthNumber());

System.out.println("Comparing the Month#1 and Month#2 objects");

bool = m1.equals(m2);

if (bool) {

System.out.println(m1.getMonthName() + " month is equal to the month " + m2.getMonthName());

} else {

System.out.println(m1.getMonthName() + " month is not equal to the month " + m2.getMonthName());

}

//Comparing the months

System.out.println(" ** Comparing the Months **");

Month m3 = new Month(7);

Month m4 = new Month("September");

bool = m3.lessThan(m4);

if (bool) {

System.out.println(m3.getMonthName() + " month is less than the month " + m4.getMonthName());

} else {

System.out.println(m3.getMonthName() + " month is not less than the month " + m4.getMonthName());

}

//Comparing the months

Month m5 = new Month(11);

Month m6 = new Month("May");

bool = m5.greaterThan(m6);

if (bool) {

System.out.println(m5.getMonthName() + " month is greater than the month " + m6.getMonthName());

} else {

System.out.println(m5.getMonthName() + " month is not greater than the month " + m6.getMonthName());

}

}

}

The error i get is

Exception in thread "main" java.lang.NullPointerException

at Month.getMonthNumber(Month.java:67)

at MonthDemo.main(MonthDemo.java:43)

This happens when asked to input the month name

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

To solve the issue and implement the Month class and its associated methods lets go through the requirements stepwise and correct any errors 1 Class Definition java public class Month private int mont... View full answer

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!