Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ASSIGNMENT 2 Create a class called Month (Java file called Month,java) with the

ID: 3869079 • Letter: A

Question

ASSIGNMENT 2 Create a class called Month (Java file called Month,java) with the (exact) following fields and methods (these names and caps exact Field/Method Description MonthNumber Month Month An int field that holds the number of the month (values from 1 to 12) A no-argument constructor that sets the MonthNumber field to1 A constructor that accept the number of the month as an argument (1. 2. etc) and sets the MonthNumber field to that value. If a value less than 1 or larger that 12 is passed, the constructor should set MonthNumber to 1 A constructor that accept the name of the month as an argument (January", February", etc) and sets the MonthNumber field to the corresponding number onthNumber Name March Month June ust October GetMonthNumber A method that returns the MonthNumber field valu etMonthNumber A method that accept an int number as an argument and sets the GetMonthName A method that returns the name of the month corresponding to the MonthNumber field value December MonthNumber field to that number MonthName A method that accept a string value as an argument (representing the name of the month: "January" A method that accepts a Month object as an argument and returns true if the data from that object (its A method that accepts a Month object as an argument and returns true if the data from that object (its A method that accepts a Month object as an argument and returns true if the data from that object (its You are going to lose points if you do not name the class, the fields, or methods as requested. You should not have a field for the ame]Assignment2 (replace [YourName] with your actual name) in the same project as the SetMonthName february", etc.) and sets the MonthNumber field to the corresponding number (1. 2 etc) MonthNumber) is the same as the MonthNumber field or false otherwise MonthNumber) is greater than the MonthNumber field or false otherwise MonthNumber) is less than the MonthNumber field or false otherwise Equals GreaterThan LessThan month name: your code should compute the name of the month from the number when needed Create a program/project called [YourN (code) should Month.java. You will need to add the class Month to the project and add your code to the project class main method. The program Create three objects: Month1 one using the first constructor (with no arguments) and Month2 using the second constructor with 2 argument, and Month3 using the third constructor with TOctoberi as an argumen

Explanation / Answer

Here is the code segment of given criteria:-

//Driver.java
//===========================

import java.util.*;

public class Driver
{
public static void main(String[] args)
{
Month month1 = new Month();
Month month2 = new Month(10);
Month month3 = new Month("September");

System.out.println("Month with no name or number given " + month1.toString());
System.out.println("Month with number given " + month2.toString());
System.out.println("Month with name given " + month3.toString());

System.out.println("Comparing month with number given to month with name given: ");

if(month2.compareTo(month3) > 0)
{
System.out.println(month2.getMonthName() + " comes after " + month3.getMonthName());
}
else if(month2.compareTo(month3) < 0)
{
System.out.println(month2.getMonthName() + " comes before " + month3.getMonthName());
}
else
{
System.out.println(month2.getMonthName() + " and " + month3.getMonthName() + " are the same.");
}
}
}

//============================
// Month.java
//==============================

import java.util.*;

public class Month
{
int monthNum;
static String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

//Check if two months are the same
public boolean equals(Month other)
{
if(getmonthNum() == other.getmonthNum())
return true;

return false;
}

//Find which month comes first
public int compareTo(Month other)
{
return getmonthNum() - other.getmonthNum();
}

//Default constructor
public Month()
{
setmonthNum(1);
}

//Constructor accepting month number
public Month(int num)
{
if(num >= 1 && num <= 12)
setmonthNum(num);
else
setmonthNum(1);
}

//Constructor accepting month name
public Month(String name)
{
for(int i = 0; i < months.length; i++)
{
if(name.equals(months[i]))
{
setmonthNum(i + 1);
}
}
}

//Set month number to input
public void setmonthNum(int num)
{
if(num >= 1 && num <= 12)
monthNum = num;
}

//Returns month number
public int getmonthNum()
{
return monthNum;
}


//Returns month name
public String getMonthName()
{
return months[getmonthNum() - 1];
}

//Gives a string output of what happens
public String toString()
{
String output = "Month number: " + getmonthNum() + " ";
output = output + "Month name: " + getMonthName() + " ";

return output;
}
}

//==============================================

Please run these codes into your window and check out the output, and let me knoe if any concern.

Thankyou