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.
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()); } }
Create a simple Java class for a Month object with the following requirements: This program...
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...
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...
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...
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....
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...
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...
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...
( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...
Create a class called Student. This class will hold the first name, last name, and test grades for a student. Use separate files to create the class (.h and .cpp) Private Variables Two strings for the first and last name A float pointer to hold the starting address for an array of grades An integer for the number of grades Constructor This is to be a default constructor It takes as input the first and last name, and the number...
Implement a Java class that is called RainFall that uses a double array to store the amount of rainfall for each month of the year. The class should have only one instance variable of type double[]. Implement the following methods in the RainFall class: 1. A constructor that creates and initializes all entries in the array to be -1. 2. toString: a method that returns a one-line String representation of the object that includes 12 double numbers that represent the...