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.

Create a simple program using Java that demonstrates the month object with the following requirements:

 That creates a month object using the no argument constructor.
 A second month object is created using the constructor that takes in an integer argument.
 Additionally, use either a do or while loop to get the user to enter a number between 1 and 12 using the setMonth() method on the 1st month object. The loop will continue until they enter a valid number.
 The program will display the month number for both of the objects using the getMonth() method.

Homework Answers

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

    //constructor with no arguments
    Months(){
        this.monthNumber = 1;
    }

    //constructor with one argument monthNumber
    Months(int monthNumber){
        //exception if monthNumber is > 13 and < 0
        try {
            if(monthNumber > 0 && monthNumber < 13){
                this.monthNumber = monthNumber;
            }else {
                throw new IllegalArgumentException();
            }
        }catch (IllegalArgumentException e){
            System.out.println("Invalid month");
        }
    }

    //get method to return monthNumber
    public int getMonthNumber() {
        return monthNumber;
    }

    //set method to set 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 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

import java.util.Scanner;

public class MonthsMain {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Months month1 = new Months();
        Months months = new Months(12);
        int month;
        do{
            System.out.println("Enter a month (between 1-12)");
            month = sc.nextInt();
        }while (month < 1  || month > 12);
        month1.setMonthNumber(month);
        System.out.println("Month1: " + month1.getMonthNumber());
        System.out.println("Month1: " + months.getMonthNumber());
    }
}

Output:

Know the answer?
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
Similar Questions
Java... Write a class named TestScores. The class constructor should accept an array of test scores...
Java... Write a class named TestScores. The class constructor should accept an array of test scores as its argument. The class should have a method that returns the average of the test scores. If any test score in the array is negative or greater than 100, the class should throw an IllegalArgumentException. Demonstrate the class in a program (create a Driver class in the same file). The program should ask the user to input the number of test scores to...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes,...
In Java, create a program and Flowchart, Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example, convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
For this assignment, you'll create a Player class that captures information about players on a sports...
For this assignment, you'll create a Player class that captures information about players on a sports team. We'll keep it generic for this assignment, but taking what you learn with this assignment, you could create a class tailored to your favorite sport. Then, imagine using such a class to track the stats during a game. Player Class Requirements Your class should bet set up as follows: Named Player Is public to allow access from other object and assemblies Has a...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
I need a Flowchart and Code for the following Java Program Write a method that converts...
I need a Flowchart and Code for the following Java Program Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis(long millis) The method returns a string as hours:minutes:seconds. For example,convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Write a test program that prompts the user to enter a long integer for milliseconds and displays a string in the format of hours:minutes:seconds.
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT