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

Could you make the second part work with the first part of this program please..

ID: 3551099 • Letter: C

Question

Could you make the second part work with the first part of this program please...


***************** FIRST PART*************************************


public class MonthDemo

{

public static void main(String[] args)

{

Month m = new Month();

System.out.println(" Month " + m.getMonthNumber() +

" is " + m);

System.out.println();

  

for (int i = 1; i <= 12; i++)

{

m.setMonthNumber(i);

System.out.println("Month " + m.getMonthNumber() +

" is " + m);

}

  

  

Month m1 = new Month(10);

Month m2 = new Month(5);

System.out.println(" Month " + m1.getMonthNumber() +

" is " + m1);

System.out.println("Month " + m2.getMonthNumber() +

" is " + m2);

  

Month m3 = new Month(Month.MARCH);

Month m4 = new Month(Month.DECEMBER);

System.out.println(" Month " + m3.getMonthNumber() +

" is " + m3);

System.out.println("Month " + m4.getMonthNumber() +

" is " + m4);

  

  

if (m3.equals(m4))

System.out.println(m3 + " and " + m4 + " are equal.");

else

System.out.println(m3+ " and " + m4 + " are NOT equal.");

  

  

if (m3.greaterThan(m4))

System.out.println(m3+ " is greater than " + m4);

else

System.out.println(m3 + " is NOT greater than " + m4);

  

  

if (m3.lessThan(m4))

System.out.println(m3+ " is less than " + m4);

else

System.out.println(m3+ " is NOT less than " + m4);

}

}



***************************************************SECOND PART*****************************************************


public class Month

{

  

private int monthNumber;

  

public Month()

{

monthNumber = 1;

}

  

public Month(int number)

{

setMonthNumber(number);

}

  

public Month (String m)

{

  

String upper = m. toUpperCase();

if( upper.equals("JANUARY") )

setMonthNumber(1);

else if( upper.equals("FEBRUAURY") )

setMonthNumber(2);

else if( upper.equals("MARCH") )

setMonthNumber(3);

else if( upper.equals("APRIL") )

setMonthNumber(4);

else if( upper.equals("MAY") )

setMonthNumber(5);

else if( upper.equals("JUNE") )

setMonthNumber(6);

else if( upper.equals("JULY") )

setMonthNumber(7);

else if( upper.equals("AUGUST") )

setMonthNumber(8);

else if( upper.equals("SEPTEMBER") )

setMonthNumber(9);

else if( upper.equals("OCTOBER") )

setMonthNumber(10);

else if( upper.equals("NOVEMBER") )

setMonthNumber(11);

else if( upper.equals("DECEMBER") )

setMonthNumber(12);

}   

  

public void setMonthNumber(int number)

{

  

if ( (number >= 1) && (number <= 12) )

monthNumber = number;

  

else

monthNumber = 1;

  

}

  

  

public int getMonthNumber()

{

return monthNumber;

  

}

  

public String getMonthName()

{

switch (monthNumber)

{

case 1:

return "JANUARY";

case 2:

return "FEBRUARY";

case 3:

return "MARCH";

case 4:

return "APRIL";

case 5:

return "MAY";

case 6:

return "JUNE";

case 7:

return "JULY";

case 8:

return "AUGUST";

case 9:

return "SEPTEMBER";

case 10:

return "OCTOBER";

case 11:

return "NOVEMBER";

case 12:

return "DECEMBER";

default :

return "JANUARY";

  

}

  

}


public String toString()

{

return getMonthName();

}


public boolean equals(Month m)

{


if (m.getMonthNumber () == this.getMonthNumber())

return true;

else

return false;

}


public boolean greaterThan(Month m)

{

if ( this.getMonthNumber() > m.getMonthNumber() )

return true;

else

return false;

}

public boolean lessThan(Month m)

{

if ( this.getMonthNumber() < m.getMonthNumber() )

return true;

else

return false;

}


}



Explanation / Answer

Problems with the code :


1. First of all, You cannot have 2 public classes in a single file, because that would defeat the whole purpose of having spreading a program into modules. Delete the "public" keyword from one of the classes to fix this issue.

Let's say delete "public" from "public class Month" i.e change it to "class Month" only.


2. Lines causing error are:

         Month m3 = new Month(Month.MARCH);

Month m4 = new Month(Month.DECEMBER);

       

        these lines are in the FIRST part of the code


         change them to :

        

        Month m3 = new Month("MARCH");

Month m4 = new Month("DECEMBER");


        as the constructor defined in the Month class takes input as string.


Make the changes I proposed in above two points and your program will then run fine.


If you want the modified file , I'm giving posting the dropbox public link


https://dl.dropboxusercontent.com/u/78383401/modified_code.txt

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