Constraints:

  • Display any numerical output with 2 digits after the decimal point
  • DO NOT use break and continue
  • DO NOT use exit() to exit a program

Write a program that involves solving problems using OOP principles for processing data for a typical month of the year. Define a class named Month that has the following attributes and methods:

  • A private integer field named monthNumber that holds the number of a month (e.g. January is 1, February is 2, etc.). NOTE that the month name IS NOT an attribute of this class.
  • A constructor that sets the monthNumber field to 1.
  • A constructor that accepts the month number as a parameter and sets the monthNumber field to the passed value. Set the monthNumber to 1 if the passed parameter's value is not a value between 1 and 12.
  • A setMonthName that accepts the name as its paramter, such as "January" as a parameter 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 method that accepts an integer as its parameter 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 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 is 1, then this method will return "January". Note that this method does not accept any parameters. It looks up the monthNumber field to make its decision.
  • A method to display the value of the MonthNumber.

Write a main program (test class) to test various operations on the objects of the Month class. Perform a series of operations to test each of the methods and the constructor. Make sure to interact with the user to ask for input to create objects.

----- The sample interaction ------

Using the constructor to set the month number = 1
Enter a month number to create an object - enter -99 when done
5
The month number set is: 5
Enter a month number to create an object - enter -99 when done
12
The month number set is: 12
Enter a month number to create an object - enter -99 when done
123
The month number set is: 1
Enter a month number to create an object - enter -99 when done
-99
The month name set is: April
The month number set is: 4
Enter the name of a Month - enter done when done
Mayyyyy
The month name set is: January
The month number set is: 1
Enter the name of a Month - enter done when done
done
Enter the name of a Month - enter done when done
April
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.