Question

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, etc.)
 A constructor that takes no arguments, and sets the monthNumber to 1.
 Add a second constructor that takes in an integer argument to set the initial monthNumber for the new Month object. Use data protection to prevent the user from entering a number less than 1 or greater than 12. When a non-valid input is entered, throw a new IllegalArgumentException.
 A setMonth() method that takes an integer and uses data protection to prevent the user from entering a number less than 1 or greater than 12. Also throw an IllegalArgumentException if an illegal value is entered.
 A getMonth() method that returns the monthNumber as an integer.
 Add a String array property that holds the values of the month names (e.g. monthNames[3] would hold the value “March”). Remember, you can leave the 0th index blank/null
 Add a toString() method to use the monthNumber property to return the name of the month as a String. Use the private global String array with the names of the months in it to return the proper String based on the monthNumber.
 Add an equals() method that takes in a month object and returns a boolean based on the values of each object’s monthNumber
 Add a compareTo() method that takes in a month object and returns a negative number if the called object is smaller than the passed in object, a positive number if the called object is bigger than the passed in object, and zero (0) if the two objects are equivalent.

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

Following is the answer:

Months.java

public class Months {
    private int monthNumber;
    private String[] monthNames = {"null", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    Months(){
        this.monthNumber = 1;
    }

    Months(int monthNumber){
        try {
            if(monthNumber > 0 && monthNumber < 13){
                this.monthNumber = monthNumber;
            }else {
                throw new IllegalArgumentException();
            }
        }catch (IllegalArgumentException e){
            System.out.println("Invalid month");
        }
    }

    public int getMonthNumber() {
        return monthNumber;
    }

    public void setMonthNumber(int monthNumber) {
        try {
            if(monthNumber > 0 && monthNumber < 13){
                this.monthNumber = monthNumber;
            }else {
                throw new IllegalArgumentException();
            }
        }catch (IllegalArgumentException e){
            System.out.println("Invalid month");
        }
    }

    @Override
    public String toString() {
        return monthNames[this.monthNumber];
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Months months = (Months) o;
        return monthNumber == months.monthNumber;
    }

    public int compareTo(Object o){
        return this.compareTo(o);
    }

}

MonthsMain.java

public class MonthsMain {
    public static void main(String[] args) {
        Months months = new Months(12);
        System.out.println(months.toString());
    }
}
Add a comment
Know the answer?
Add Answer to:
Create a simple Java class for a Month object with the following requirements:  This program...
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
  • Create a simple Java class for a Password with the following requirements:  This program will...

    Create a simple Java class for a Password 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.  One String property: password (protected to only allow secure passwords) o A secure password must be at least 8 characters in length o A secure password must have three of the following four requirements:  A lower case letter ...

  • Write a class named Month. The class should have an int field named monthNumber that holds...

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

  • File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute...

    File Factorials.java contains a program that calls the factorial method of the MathUtils class to compute the factorials of integers entered by the user. In this program, two things can possibly occur: The program always returns 1 if negative integers are entered by the user. Returning 1 as the factorial of any negative integer is not correct. The program also returns negative numbers when the user enters integers greater than 16. Returning a negative number for values over 16 also...

  • 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...

  • 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...

  • Create a java class for an object called Student which contains the following private attributes: a...

    Create a java class for an object called Student which contains the following private attributes: a given name (String), a surname (family name) (String), a student ID number (an 8 digit number, String or int) which does not begin with 0. There should be a constructor that accepts two name parameters (given and family names) and an overloaded constructor accepting two name parameters and a student number. The constructor taking two parameters should assign a random number of 8 digits....

  • 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...

  • put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following:...

    put everything together in an object-oriented manner! create a class named PositiveNumberStatistics that has the following: A private double 1D array instance variable named numbers. A constructor that takes one parameter, a 1D double array. The constructor should throw an IllegalArgumentException with the message "Array length of zero" if the parameter has a length of 0. If the exception is not thrown, the constructor should create the numbers array and copy every element from the parameter into the numbers instance...

  • 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 LIFO class with following methods in java - public LifoList() The default constructor for...

    Create a LIFO class with following methods in java - public LifoList() The default constructor for LifoList class which will initialize your class variables maxSize to 0 and the int array to null. public LifoList(int maxSize) The constructor for LifoList class which takes the int input maxSize to initialize the size of the array. You should NOT reinitialize the array elsewhere. For example, creating an object as new LifoList(5) should create a LifoList whose maximum size is 5. If the...

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