Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a class named Month. The class should have an integer field named month

ID: 3666162 • Letter: 1

Question

1. Write a class named Month. The class should have an integer field named monthNumber that holds the number of the month (January is 1, February is 2, etc.). Also provide the following functions: • A default constructor that sets the monthNumber field to 1. • A constructor that accepts the number of month as an argument and sets the monthNumber field to the passed value. Set the monthNumber to 1 if the passed argument’s value is not a value between 1 and 12. • A constructor that accepts the name, such as “January” as an argument and sets the monthNumber field to the correct corresponding value. Make sure this is not case sensitive, so entering “January” will return the same value as if “JaNUary” was entered. If an invalid month is entered, set the monthNumber to 1. • A setMonthNumber function that accepts an integer argument to assign to the monthNumber field. Set the monthNumber to 1 if the passed argument’s value is not a value between 1 and 12. • A getMonthNumber function that returns the value in the monthNumber field. • A getMonthName function that returns the name of the month. For example, if the monthNumber field is 1, then this function will return “January”. Note that this function does not accept any parameters. It looks up the monthNumber field to make its decision. • A function named equalMonth that accepts a Month object as an argument. If the argument object holds the same data as the calling object, the function should return true. Otherwise, it should return false. • A function named greaterThan that accepts a Month object as an argument. If the calling object’s monthNumber field is greater than the argument’s monthNumber field, it should return true. Otherwise, it should return false. • A function named lessThan that accepts a Month object as an argument. If the calling object’s monthNumber field is less than the argument’s monthNumber field, it should return true. Otherwise, it should return false. • Write a main program that uses the class Month and test various operations on the objects of the class Month. Declare an array of 10 components of type Month and show how to set and get values for the array elements. Perform a series of operations to test each of the functions and the constructors. Make sure to prompt the user to enter a value for monthNumber as you test your code

Explanation / Answer

// Month.java

public class Month {
   private int monthNumber;

   //Default constructor
   public Month() {
       this.monthNumber = 1;
   }
   //Parameterized constructor
   public Month(int monthNumber) {
       if(isValid(monthNumber))
           this.monthNumber = monthNumber;
       else
           this.monthNumber = 1;
   }
  
   public Month(String monthName) {
       this.monthNumber = monthNumber(monthName);
   }

   private boolean isValid(int monthNumber) {
       if(monthNumber >=1 && monthNumber <=12)
           return true;
       return false;
   }
  
   private int monthNumber(String monthName) {
       int monthNumber = 1;
       monthName = monthName.toLowerCase();
       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;
       }
       return monthNumber;
   }
   private String monthName(int monthNumber) {
       String monthName = "january";
       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;
       }
       return monthName;
   }
   //getter setter
   public int getMonthNumber() {
       return monthNumber;
   }

   public void setMonthNumber(int monthNumber) {
       this.monthNumber = monthNumber;
   }
  
   public String getMonthName() {
       return monthName(this.monthNumber);
   }
  
   public boolean equalMonth(Month month) {
       return this.monthNumber == month.monthNumber;
   }
  
   public boolean greaterThan(Month month) {
       return this.monthNumber > month.monthNumber;
   }
  
   public boolean lessThan(Month month) {
       return this.monthNumber < month.monthNumber;
   }

}

//Test.java

public class Test {

   public static void main(String[] args) {
      
       Month month1 = new Month("may");
       System.out.println(month1.getMonthNumber());
       System.out.println(month1.getMonthName());
   }

}