I have done most of the code but am struggling a 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 the following error message
Month.java:81: cannot find symbol
symbol : variable monthNumb
location: class Month
if (month2.getMonthNumber() == monthNumb)

And also how to put in the getLastMonthCreated in

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;

      }
      }

Recommended Answers

All 6 Replies

You declared "private int monthNumber;" but you are using "monthNumb"???

Thankyou, how silly! How should I go about writing in getLastMonthCreated please?

The requirements are somewhat obscure. I would assume that the getLastMonthCreated() is to return the lastMonthCreated value. If you look at the requirement, it said that you are supposed to create the variable as static in your class.

static int lastMonthCreated;

The problem is that the requirement does NOT say anything about how to initialize and update the value! So my assumption is to initialize it to 1 at the declaration.

static int lastMonthCreated=1;

And when to update is when you set the month in setMonthNumber() methods. Before you set the monthNumber, copy the variable value to lastCreatedMonth first, and then set the monthNumber. Not sure... You need to ask your instructor.

The requirement states "the number of the month which was last constructed.", so I would suggest updating it in the constructor(s) not in the set... method.

@James,
First I thought it that way, but then I am not sure about the definition of "created" in this requirement. It is vague because I do not know or see how the variable value is used in a real situation... That's why the OP should ask the instructor... :(

Yes, its not clear, and it has no obvious use. The actual word used was "constructed" not the normal English "created", which I interpreted as a hint.
Maybe the next exercise will move on to some standard pattern like creating a static collection of all the instances that have been created - in which case this is a step on the way???
Either way, you're right, only the instructor can say for certain what's required.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.