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

A Second Look at Classes and Objects. Month Class 1,write a class named Month. T

ID: 3547732 • Letter: A

Question

A Second Look at Classes and Objects.


Month Class

1,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 consructor 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 getmonthNumber 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 equal

Explanation / Answer

public class Month
{
private int monthNumber;
public Month()
{
monthNumber = 1;
}
public Month(int month_num)
{
if(month_num < 1 || month_num > 12)
monthNumber = 1;
else
monthNumber = month_num;
}
public Month(String month_name)
{
if(month_name.equalsIgnoreCase("January"))
monthNumber = 1;
else if(month_name.equalsIgnoreCase("February"))
monthNumber = 2;
else if(month_name.equalsIgnoreCase("March"))
monthNumber = 3;
else if(month_name.equalsIgnoreCase("April"))
monthNumber = 4;
else if(month_name.equalsIgnoreCase("May"))
monthNumber = 5;
else if(month_name.equalsIgnoreCase("June"))
monthNumber = 6;
else if(month_name.equalsIgnoreCase("July"))
monthNumber = 7;
else if(month_name.equalsIgnoreCase("August"))
monthNumber = 8;
else if(month_name.equalsIgnoreCase("September"))
monthNumber = 9;
else if(month_name.equalsIgnoreCase("October"))
monthNumber = 10;
else if(month_name.equalsIgnoreCase("November"))
monthNumber = 11;
else if(month_name.equalsIgnoreCase("December"))
monthNumber = 12;
}
public void setMonthNumber(int month_num)
{
if(month_num < 1 || month_num > 12)
monthNumber = 1;
else
monthNumber = month_num;
}
public int getmonthNumber()
{
return monthNumber;
}
public String getMonthName()
{
if(monthNumber==1)
return "January";
else if(monthNumber==2)
return "February";
else if(monthNumber==3)
return "March";
else if(monthNumber==4)
return "April";
else if(monthNumber==5)
return "May";
else if(monthNumber==6)
return "June";
else if(monthNumber==7)
return "July";
else if(monthNumber==8)
return "August";
else if(monthNumber==9)
return "September";
else if(monthNumber==10)
return "October";
else if(monthNumber==11)
return "November";
else if(monthNumber==12)
return "December";
return "January";
}
public String toString()
{
return getMonthName();
}
public boolean equals(Month M)
{
return monthNumber==M.monthNumber;
}
public boolean greaterThan(Month M)
{
return monthNumber>M.monthNumber;
}
public boolean lessThan(Month M)
{
return monthNumber<M.monthNumber;
}
public static void main(String[] args)
{
Month m1 = new Month(2);
Month m2 = new Month(11);
System.out.println("Month m1 name is " + m1);
System.out.println("Month m2 name is " + m2);
System.out.println("is m1 equal to m2 ? " + m1.equals(m2));
}
}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote