Define a class called Month in Java that does the following

Define a class called Month in Java that does the following:

Uses a private variable of type int to represent a month (1-12). This variable should be the ONLY private instance variable for the class.

A default constructor that sets the month to 1

A second constructor that sets the month using an integer as an argument

A third constructor that sets the month using a String as an argument, where the string contains the first three letters of the month (e.g. \"Jan\", \"Feb\", etc.) The constructor should compare the String value to \"Jan\", \"Feb\", etc. and set the month to the corresponding integer value. For example, if \"Jan\" is passed in then the variable should be set to 1.

A method that returns the month as an integer

A method that returns the month as a three letter String

A method that returns the next month as a new Month object. This means the return type will have to be of type Month. Make sure that the next month of December is January.

Use the Month class in a test program that exercises all of your methods and constructors.

Here is some sample code that could go in your main method that demonstrates the desired output:

Solution

MonthTest.java


public class MonthTest {


   public static void main(String[] args) {
       Month m1 = new Month();
Month m2 = new Month(3);
Month m3 = new Month(\"Apr\");
Month m4 = m3.nextMonth();

System.out.println(m1.getMonthNumber()); // Outputs 1
System.out.println(m2.getMonthString()); // Outputs Mar
System.out.println(m3.getMonthNumber()); // Outputs 4
System.out.println(m4.getMonthString()); // Outputs May

   }

}

Month.java


public class Month {
   private int month;
   public Month(){
       month = 1;
   }
   public Month(int m){
       month = m;
   }
   public Month(String s){
       if(s.equalsIgnoreCase(\"Jan\")){
           month = 1;
       }
       else if(s.equalsIgnoreCase(\"Feb\")){
           month = 2;
       }
       else if(s.equalsIgnoreCase(\"Mar\")){
           month = 3;
       }
       else if(s.equalsIgnoreCase(\"Apr\")){
           month = 4;
       }
       else if(s.equalsIgnoreCase(\"May\")){
           month = 5;
       }
       else if(s.equalsIgnoreCase(\"Jun\")){
           month = 6;
       }
       else if(s.equalsIgnoreCase(\"Jul\")){
           month = 7;
       }
       else if(s.equalsIgnoreCase(\"Aug\")){
           month = 8;
       }
       else if(s.equalsIgnoreCase(\"Sep\")){
           month =9 ;
       }
       else if(s.equalsIgnoreCase(\"Oct\")){
           month = 10;
       }
       else if(s.equalsIgnoreCase(\"Nov\")){
           month = 11;
       }
       else if(s.equalsIgnoreCase(\"Dec\")){
           month = 12;
       }

   }
   public int getMonthNumber(){
       return month;
   }
   public String getMonthString(){
       if(month == 1){
           return \"Jan\";
       }
       else if(month == 2){
           return \"Feb\";
       }
       else if(month == 3){
           return \"Mar\";
       }
       else if(month == 4){
           return \"Apr\";
       }
       else if(month == 5){
           return \"May\";
       }
       else if(month == 6){
           return \"Jun\";
       }
       else if(month == 7){
           return \"Jul\";
       }
       else if(month == 8){
           return \"Aug\";
       }
       else if(month == 9){
           return \"Sep\";
       }
       else if(month == 10){
           return \"Oct\";
       }
       else if(month == 11){
           return \"Nov\";
       }
       else if(month == 12){
           return \"Dec\";
       }
       return \"Invalid month\";
       }
   public Month nextMonth(){
       int m = month;
       if(m + 1 > 12){
           m = 1;
       }
       else{
           m = m+1;
       }
       return new Month(m);
   }
}

Output:

1
Mar
4
May

Define a class called Month in Java that does the following: Uses a private variable of type int to represent a month (1-12). This variable should be the ONLY p
Define a class called Month in Java that does the following: Uses a private variable of type int to represent a month (1-12). This variable should be the ONLY p
Define a class called Month in Java that does the following: Uses a private variable of type int to represent a month (1-12). This variable should be the ONLY p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site