Write a class named Month The class should have an int field

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 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. F or example, if the monthNumber field contains 1. then this method should return \"January\". 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 greater Than 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. middot 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.

Solution

public class Month {

   // Class variable
   private static final String[] MONTH_NAME = {\"January\", \"February\", \"March\", \"April\", \"May\", \"June\",
                                           \"July\", \"August\", \"September\", \"October\", \"November\", \"December\"};
   // Instance variables
   private int monthNumber;

   /**
   * Default constructor
   */
   public Month() {
       this.monthNumber = 1;
   }

   /**
   * Parameterized constructor
   * @param monthNumber
   */
   public Month(int monthNumber) {
       setMonthNumber(monthNumber);
   }
  
   /**
   * Parameterized constructor
   * @param monthNumber
   */
   public Month(String monthName) {
       int monthNumber = 0;
       for (String month : Month.MONTH_NAME) {
           monthNumber += 1;
           if(monthName.equalsIgnoreCase(month)) {
               this.monthNumber = monthNumber;
               break;
           }
       }
   }

   /**
   * Returns the monthNumber
   */
   public int getMonthNumber() {
       return monthNumber;
   }

   /**
   * @param monthNumber the monthNumber to set
   */
   public void setMonthNumber(int monthNumber) {
       if((monthNumber < 1) || (monthNumber > 12))
           this.monthNumber = 1;
       else
           this.monthNumber = monthNumber;
   }
  
   /**
   * Returns the monthName
   */
   public String getMonthName() {
       return MONTH_NAME[this.monthNumber - 1];
   }

   @Override
   public String toString() {
       return getMonthName();
   }

   public boolean equals(Month month) {
       if(month != null) {
           if((this == month) || (this.monthNumber == month.monthNumber))
               return true;
       }
      
       return false;
   }
  
   /**
   * Returns true if the calling object\'s month is greater than the argument\'s month
   * @param month
   * @return
   */
   public boolean greaterThan(Month month) {
       return this.monthNumber > month.monthNumber;
   }
  
   /**
   * Returns true if the calling object\'s month is less than the argument\'s month
   * @param month
   * @return
   */
   public boolean lessThan(Month month) {
       return this.monthNumber < month.monthNumber;
   }
}

 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 w
 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 w

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site