Define a class called Month that is an abstract data type for a month. Your clas
ID: 3798158 • Letter: D
Question
Define a class called Month that is an abstract data type for a month. Your class should have a private intager variable that represents the month (1 for January, 2 for February, etc.).
Include all the following member functions:
- A default constructor that set's the month to January.
- A constructor to set the month using the first three letters in the name of the month as three arguments .
- A constructor to set the month using an integer as an argument .
- A mutator function that sets the month by passing in an integer .
- An accessor function that returns the value of the month as an integer .
- An accessor function that returns the first three letters in the name of the month.
- A Member function that returns the next month as a value of type Month.
Make sure your class validates user input to verify that a valid month was entered. In main(), test your program by creating 3 different Month objects . The first month object should use an integer value entered by the user as the argment to the constructor . Then, use the Month class member functions to print out the first three letters of the corresponding month and the first three letters of the next month. The second month object should take 3 letters that the user enters as arguments to the constructor . Then, use the Month class member functions to print out the value of that month as an integer , as well as the first three letters of the next month. The third month object should be created using the default constructor . Print out the first three letters of that month and the next month.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public class Month
{
private int monthNumber = 0;
public Month()
{
monthNumber = 1;
}
public Month(int num)
{
monthNumber = num;
}
public Month(String month)
{
month = month.toLowerCase();
switch (month) {
case "jan":
monthNumber = 1;
break;
case "feb":
monthNumber = 2;
break;
case "mar":
monthNumber = 3;
break;
case "apr":
monthNumber = 4;
break;
case "may":
monthNumber = 5;
break;
case "jun":
monthNumber = 6;
break;
case "jul":
monthNumber = 7;
break;
case "aug":
monthNumber = 8;
break;
case "sep":
monthNumber = 9;
break;
case "oct":
monthNumber = 10;
break;
case "nov":
monthNumber = 11;
break;
case "dec":
monthNumber = 12;
break;
default:
monthNumber = 1;
}
}
public void setMonth(int newMonth)
{
monthNumber = newMonth;
}
public int getMonth()
{
return monthNumber;
}
public int getNextMonth(){
if(monthNumber < 12)
return (monthNumber+1);
else
return 1;
}
public String getMonthStr()
{
switch(monthNumber)
{
case 1: return "Jan";
case 2: return "Feb";
case 3: return "Mar";
case 4: return "Apr";
case 5: return "May";
case 6: return "Jun";
case 7: return "Jul";
case 8: return "Aug";
case 9: return "Sep";
case 10: return "Oct";
case 11: return "Nov";
case 12: return "Dec";
default: return "No month specified";
}
}
}
import java.util.Scanner;
public class UseMonth
{
public static void main(String[] args)
{
Scanner kbd = new Scanner(System.in);
//kbd.nextLine();
System.out.print("Please enter a numeric representaion of the Month? (ex. 1 for January)");
int newMonth = kbd.nextInt();
Month month1 = new Month(newMonth);
System.out.println("You entered: " + newMonth + ", which is the month of " + month1.getMonthStr());
System.out.println("Next month : "+month1.getNextMonth());
Month month2 = new Month("feb");
System.out.println("Month Number: "+month2.getMonth());
System.out.println("Next Month : "+month2.getNextMonth());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.