I have done most of the code but am struggling with one bit now, please help
The brief is
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. The class should also have a static field named lastMonthCreated that holds the number of the month which was last constructed. 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 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 getLastMonthCreated method that returns the value in the lastMonthCreated field.
•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.
The help I would like is with how to put in the getLastMonthCreated in
The driver for this part is
public class MonthDemo3
{
public static void main(String[] args)
{
// Use the 3rd constructor to create three objects.
Month m1 = new Month("March");
Month m2 = new Month("December");
Month m3 = new Month("Bad Month");
System.out.println("Month " + m1.getMonthNumber() +
" is " + m1);
System.out.println("Month " + m2.getMonthNumber() +
" is " + m2);
System.out.println("Month " + m3.getMonthNumber() +
" is " + m3);
Month m4 = new Month("May");
System.out.println("The last month created" +
" is " + m4.getLastMonthCreated());
}
}
My code is
public class Month
{
private int monthNumber;
public Month(int m)
{
if (m < 1 || m > 12)
monthNumber = 1;
else
monthNumber = m;
}
public Month(String name)
{
if (name.equalsIgnoreCase("january")) monthNumber = 1;
else if (name.equalsIgnoreCase("february")) monthNumber = 2;
else if (name.equalsIgnoreCase("march")) monthNumber = 3;
else if (name.equalsIgnoreCase("april")) monthNumber = 4;
else if (name.equalsIgnoreCase("may")) monthNumber = 5;
else if (name.equalsIgnoreCase("june")) monthNumber = 6;
else if (name.equalsIgnoreCase("july")) monthNumber = 7;
else if (name.equalsIgnoreCase("august")) monthNumber = 8;
else if (name.equalsIgnoreCase("september"))monthNumber = 9;
else if (name.equalsIgnoreCase("october")) monthNumber = 10;
else if (name.equalsIgnoreCase("november")) monthNumber = 11;
else if (name.equalsIgnoreCase("december")) monthNumber = 12;
else monthNumber = 1;
}
public void setMonthNumber(int m)
{
if (m < 1 || m > 12)
System.out.println("Invalid Month Number");
else monthNumber = m;
}
public int getMonthNumber()
{
return monthNumber;
}
public String getMonthName()
{
String name;
switch (monthNumber)
{
case 1: name = "January";
break;
case 2: name = "February";
break;
case 3: name = "March";
break;
case 4: name = "April";
break;
case 5: name = "May";
break;
case 6: name = "June";
break;
case 7: name = "July";
break;
case 8: name = "August";
break;
case 9: name = "September";
break;
case 10: name = "October";
break;
case 11: name = "November";
break;
case 12: name = "December";
break;
default: name = "Unknown";
}
return name;
}
public String toString()
{
return getMonthName();
}
public boolean equals(Month month2)
{
boolean status;
if (month2.getMonthNumber() == monthNumb)
status = true;
else
status = false;
return status;
}
public boolean greaterThan(Month month2)
{
boolean status;
if (monthNumber > month2.getMonthNumber())
status = true;
else
status = false;
return status;
}
public boolean lessThan(Month month2)
{
boolean status;
if (monthNumber < month2.getMonthNumber())
status = true;
else
status = false;
return status;
}
}