Question

Write a class named Month. The class should have a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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;
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a class named Month. The class should have an int field named monthNumber that holds...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks...

    Java Homework Help. Can someone please fix my code and have my program compile correctly? Thanks for the help. Specifications: 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...

  • C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. Write a class named Month. The class should have an integer field named month...

    C++ 1. Start with a UML diagram of the class definition for the following problem defining a utility class named Month. 2. 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...

  • Design a class named Month. The class should have the following private members:

    Design a class named Month. The class should have the following private members:   • name - A string object that holds the name of a month, such as "January", "February", etc.   • monthNumber - An integer variable that holds the number of the month. For example, January would be 1, February would be 2, etc. Valid values for this variable are 1 through 12.  In addition, provide the following member functions:• A default constructor that sets monthNumber to 1 and name...

  • All files require Javadoc documentation comments at the top to include a description of the program...

    All files require Javadoc documentation comments at the top to include a description of the program and an @author tag with your name  only.    -------------------------------------- Develop an exception class named InvalidMonthException that extends the Exception class.   Instances of the class will be thrown based on the following conditions: The value for a month number is not between 1 and 12 The value for a month name is not January, February, March, … December -------------------------------------- Develop a class named Month.  The class should define an integer...

  • Create a simple Java class for a Month object with the following requirements:  This program...

    Create a simple Java class for a Month object with the following requirements:  This program will have a header block comment with your name, the course and section, as well as a brief description of what the class does.  All methods will have comments concerning their purpose, their inputs, and their outputs  One integer property: monthNumber (protected to only allow values 1-12). This is a numeric representation of the month (e.g. 1 represents January, 2 represents February,...

  • java Write a class named Car that has the following fields: • yearModel: The yearModel field...

    java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...

  • Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 -...

    Given the following date class interface: class date {private: int month;//1 - 12 int day;//1 - 28. 29. 30. 31 depending on month & year int year;//4-digit, e.g.. 2017 public: date();//Default constructor (investigate; find what it is used for)//Postcondition: the newly declared date object is initialized to 01/01/2000 date(int mm, int dd, int yyyy);//Second constructor//Postcondition: the newly declared data object is initialized to mm/dd/yyyy void setDate(int mm. int dd. int yyyy);//Postcondition: set the contents of the calling date object to...

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Programming 4_1: Create a class call CellPhone. This class represents a cell phone. The data a...

    Programming 4_1: Create a class call CellPhone. This class represents a cell phone. The data a cellphone should contains: manufact- the name of the phone’s manufacturer; model – the phone’s model number; retailPrice – the phone’s retail price. The class will have the following methods: 1)A constructor that accepts arguments for manufacturer, model number, and retail price. 2)A setManufact method that accepts an argument for the manufacturer. This method will allow us to change the value of manufact field after...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT