Question: pls use java as easy as possible, since I am a first-year colleger P2: Month class. Write a class named Month. The class should have
pls use java as easy as possible, since I am a first-year colleger
P2: Month class. 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 1 or greater than 12 is passed, the constructor should set monthNumber to 1.
A constructor that accepts the name of the month, such as January or February as an argument. It should set the monthNumber field to the correct corresponding value.
A setMonthNumber method that accepts an int argument, which is assigned to the monthNumber field. If a value less than 1 or greater than 12 is passed, the method should set monthNumber to 1.
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 contains 1, then this method should return January.
A toString method that returns the same value as the getMonthName method.
An equals method that accepts a Month object as an argument. If the argument object holds the same data as the calling object, this method should return true. Otherwise, it should return false.
Use the MonthTest Class attached below to test the Month class methods. You need to complete the code.
// Month Test import java.util.Scanner;
public class MonthTest { public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
Month monthA,monthB,monthC;
int nbA, nbB;
String nC;
System.out.println("Enter number of Month A: ");
nbA = keyboard.nextInt();
keyboard.nextLine();
// create a new Month object for monthA
System.out.println("Enter number of Month B: ");
nbB = keyboard.nextInt();
keyboard.nextLine();
// create a new Month object for monthB
// compare monthA and monthB.
// If they are equal print "equal" and print the values of monthA and monthB.
System.out.println("Enter the name of Month C: ");
nC = keyboard.nextLine();
// create a new Month object for monthC
// use getMonthNumber() and getMonthName() to print the values of monthC
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
