Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 934
  • Last Modified:

Java Constructor issue

Hi all
i am doing a Java Program for Uni and i am almost finshed and i cant workout why i am getting this error The constructor Month() is undefined

I have two other classes that are pointing to the main file and i getting no issues if i could please get a hand that would be great

main code

public class Month {
	
	private int monthNumber; // The month number
	static int lastMonthCreated;
	

		
	   public Month(int m)
	   {
	        if (m < 1 || m > 12)
	            monthNumber = 1;
	        else
	            monthNumber = m;
	   }
	
	 public Month(String name)
	  {
         if (name.equalsIgnoreCase("january")) monthNumber = 1;
         else if (name.equalsIgnoreCase("february")) monthNumber = 2;
         else if (name.equalsIgnoreCase("march")) monthNumber = 3;
         else if (name.equalsIgnoreCase("april")) monthNumber = 4;
         else if (name.equalsIgnoreCase("may")) monthNumber = 5;
         else if (name.equalsIgnoreCase("june")) monthNumber = 6;
         else if (name.equalsIgnoreCase("july")) monthNumber = 7;
         else if (name.equalsIgnoreCase("august")) monthNumber = 8;
         else if (name.equalsIgnoreCase("september"))monthNumber = 9;
         else if (name.equalsIgnoreCase("october")) monthNumber = 10;
         else if (name.equalsIgnoreCase("november")) monthNumber = 11;
         else if (name.equalsIgnoreCase("december")) monthNumber = 12;
         else monthNumber = 1;
     }
	 
	 
	 
	

	public String getMonthName()

	   {

	      String name;



	      switch (monthNumber)

	      {

	         case 1:  name = "January";

	                  break;

	         case 2:  name = "February";

	                  break;

	         case 3:  name = "March";

	                  break;

	         case 4:  name = "April";

	                  break;

	         case 5:  name = "May";

	                  break;

	         case 6:  name = "June";

	                  break;

	         case 7:  name = "July";

	                  break;

	         case 8:  name = "August";

	                  break;

	         case 9:  name = "September";

	                  break;

	         case 10: name = "October";

	                  break;

	         case 11: name = "November";

	                  break;

	         case 12: name = "December";

	                  break;

	         default: name = "Unknown";

	      }



	      return name;

	   }
	 
	 public String toString()

	   {

	      return getMonthName();

	   }
	 
	 
	 public void setMonthNumber(int m)


	   {

	      if (m < 1 || m > 12)

	         System.out.println("Invalid Month Number");

	      else

	         monthNumber = m;

	   }
	 
	 public int getMonthNumber()

	   {

	      return monthNumber;

	   }
	 


	   public boolean equals(Month month2)

	   {

	      boolean status;



	      if (month2.getMonthNumber() == monthNumber)

	         status = true;

	      else

	         status = false;



	      return status;

	   } 
	   
	 


	   public boolean greaterThan(Month month2)

	   {

	      boolean status;



	      if (monthNumber > month2.getMonthNumber())

	         status = true;

	      else

	         status = false;



	      return status;

	   }
	   
	   /**

	    * lessThan method

	    */



	   public boolean lessThan(Month month2)

	   {

	      boolean status;



	      if (monthNumber < month2.getMonthNumber())

	         status = true;

	      else

	         status = false;



	      return status;

	   }

	public int getLastMonthCreated() {
		
		return monthNumber;
	}
	   
	   
}

Open in new window


2nd class code that doesnt work

/**
   Demo program #1 for the Month Class
   Comp131 S2-2011 Assignment P5
*/

public class MonthDemo1
{
   public static void main(String[] args)
   {
      // Use the no-arg constructor.
      Month m = new Month();
      System.out.println("Month " + m.getMonthNumber() +
                         " is " + m);
      // Set the month number to the values 0 through 12
      // (0 is invalid), and display the resulting month name.
      for (int i = 0; i <= 12; i++)
      {
         m.setMonthNumber(i);
         System.out.println("Month " + m.getMonthNumber() +
                         " is " + m);
      }
   }
}

Open in new window


Third that does work

/**
   Demo program #2 for the Month Class
   Comp131 S2-2011 Assignment P5
*/

public class MonthDemo2
{
   public static void main(String[] args)
   {
      // Use the 2nd constructor to create two objects.
      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);

      // Test for equality.
      if (m1.equals(m2))
         System.out.println(m1 + " and " + m2 + " are equal.");
      else
         System.out.println(m1 + " and " + m2 + " are NOT equal.");

      // Is m1 greater than m2?
      if (m1.greaterThan(m2))
         System.out.println(m1 + " is greater than " + m2);
      else
         System.out.println(m1 + " is NOT greater than " + m2);

      // Is m1 less than m2?
      if (m1.lessThan(m2))
         System.out.println(m1 + " is less than " + m2);
      else
         System.out.println(m1 + " is NOT less than " + m2);
   }
}

Open in new window

0
awolarczuk
Asked:
awolarczuk
  • 7
  • 6
  • 4
2 Solutions
 
krakatoaCommented:
Month m = new Month();

You've got to give the constructor either an int or String argument.
0
 
awolarczukAuthor Commented:
Hi krakatoa
Sorry i am so Sleepy atm where would i have to change something and where
0
 
awolarczukAuthor Commented:
krakatoa i could be wrong and i am sure i am bu Public Month is a string
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
n2fcCommented:
You are missing a "no arg" constructor...

You have one for an int...
You have one for a string...
BUT... none for the "no arg" referenced in your failing sample!

Add it in the Month class (after the other 2 constructors):
(after line 32 in your code)

public Month()
	   {
	        this(0);   /* pick your default value here  */
	   }

Open in new window

0
 
krakatoaCommented:
Which is what I already said of course,  n2fc.

Just look through your source code and find the constructor I quoted, and you will see that it has an empty parameter list. You need to call your constructor(s) either with an int or a String, because you haven't made a 3rd constructor that has no parameters.
0
 
krakatoaCommented:
Line 11 would be a good place to look in your '2nd class code' [sic].
0
 
awolarczukAuthor Commented:
krakatoa
Sorry i am a bit confused on this one with what you are sating i am sure you are right i just cant get my head arountit

n2fc
so were do i need to put this mate and what do i need to put there
0
 
krakatoaCommented:
Ok one more time.

When you call a constructor explicitly, depending on what you want to do when you make the new object in question, (which is what the constructor is for), you call it like any other method, with or without parameters. You require objects called Months to be instantiated, and you have defined two ways of making a new Month - one way calls a constructor using an int as an argument, the other makes a Month using a String as an constructor argument. (Having multiple constructors is called method overloading, and you can have as many as you like, as long as the compiler can differentiate between them based on the different variety and or number of arguments you provide).

You have - on Line 11 in the middle piece of your quoted code - called a Month constructor, but you haven't given it any arguments - so it doesn't know whether you want to make a Month based on int, or a Month based on String (Jan-Dec). So the constructor call at that line calls nothing. And that's it.
0
 
awolarczukAuthor Commented:
ok i kinda have it working but for some reason by results have a Invalid Month Number

Month 1 is January
Invalid Month Number
Month 1 is January
Month 1 is January
Month 2 is February
Month 3 is March
Month 4 is April
Month 5 is May
Month 6 is June
Month 7 is July
Month 8 is August
Month 9 is September
Month 10 is October
Month 11 is November
Month 12 is December

public class Month {
	
	private int monthNumber; // The month number
	static int lastMonthCreated;
	

		
	   public Month(int m)
	   {
	        if (m < 1 || m > 12)
	            monthNumber = 1;
	        else
	            monthNumber = m;
	   }
	
	 public Month(String name)
	  {
         if (name.equalsIgnoreCase("january")) monthNumber = 1;
         else if (name.equalsIgnoreCase("february")) monthNumber = 2;
         else if (name.equalsIgnoreCase("march")) monthNumber = 3;
         else if (name.equalsIgnoreCase("april")) monthNumber = 4;
         else if (name.equalsIgnoreCase("may")) monthNumber = 5;
         else if (name.equalsIgnoreCase("june")) monthNumber = 6;
         else if (name.equalsIgnoreCase("july")) monthNumber = 7;
         else if (name.equalsIgnoreCase("august")) monthNumber = 8;
         else if (name.equalsIgnoreCase("september"))monthNumber = 9;
         else if (name.equalsIgnoreCase("october")) monthNumber = 10;
         else if (name.equalsIgnoreCase("november")) monthNumber = 11;
         else if (name.equalsIgnoreCase("december")) monthNumber = 12;
         else monthNumber = 1;
     }
	 
	 
	 
	

	public Month() {
		monthNumber = 1; 
	}

	public String getMonthName()

	   {

	      String name;



	      switch (monthNumber)

	      {

	         case 1:  name = "January";

	                  break;

	         case 2:  name = "February";

	                  break;

	         case 3:  name = "March";

	                  break;

	         case 4:  name = "April";

	                  break;

	         case 5:  name = "May";

	                  break;

	         case 6:  name = "June";

	                  break;

	         case 7:  name = "July";

	                  break;

	         case 8:  name = "August";

	                  break;

	         case 9:  name = "September";

	                  break;

	         case 10: name = "October";

	                  break;

	         case 11: name = "November";

	                  break;

	         case 12: name = "December";

	                  break;

	         default: name = "Unknown";

	      }



	      return name;

	   }
	 
	 public String toString()

	   {

	      return getMonthName();

	   }
	 
	 
	 public void setMonthNumber(int m)


	   {

	      if (m < 1 || m > 12)

	         System.out.println("Invalid Month Number");

	      else

	         monthNumber = m;

	   }
	 
	 public int getMonthNumber()

	   {

	      return monthNumber;

	   }
	 


	   public boolean equals(Month month2)

	   {

	      boolean status;



	      if (month2.getMonthNumber() == monthNumber)

	         status = true;

	      else

	         status = false;



	      return status;

	   } 
	   
	 


	   public boolean greaterThan(Month month2)

	   {

	      boolean status;



	      if (monthNumber > month2.getMonthNumber())

	         status = true;

	      else

	         status = false;



	      return status;

	   }
	   
	   /**

	    * lessThan method

	    */



	   public boolean lessThan(Month month2)

	   {

	      boolean status;



	      if (monthNumber < month2.getMonthNumber())

	         status = true;

	      else

	         status = false;



	      return status;

	   }

	public int getLastMonthCreated() {
		
		return monthNumber;
	}
	   
	   
}

Open in new window

0
 
awolarczukAuthor Commented:
One of my requiments is to make a no-arg constructor that sets the monthNumber Field to 1

I am trying to do this but just cant seem to work it out as i have partly got it working
0
 
n2fcCommented:
krakatoa:   You said he was calling the constructor wrong... Maybe, but it DOESN'T address the ISSUE (which is a MISSING no-arg constructor)!

awolarczuk: You have added the no-arg constructor as per your requirements... I was giving an example of showing how a constructor can call another (the no-arg constructor calls the (int) constructor)... The way you did it also works!

In other words, your no-arg can either do:
monthNumber = 1; 

Open in new window

(which is OK)
or

this(1); 

Open in new window

which accomplishes the same thing by calling the (int) constructor to set the value to 1!


The "invalid month number" output is generated by line 127 when you call
from 2nd class at line  18:
   m.setMonthNumber(i);

Open in new window

when you call it with a zero, (since it is <1) you log the error and set it to 1!

You can either change the if statement, or not bother with the error!
0
 
awolarczukAuthor Commented:
you both havebeen great i have fixed it all and working great i am going to give n2fc 300 and krakatoa 200.  krakatoa you did help but i just personal feel i got the problem fixed quicker with the help from n2fc  he/she just seemed to understand better sorry
0
 
awolarczukAuthor Commented:
Both were great
0
 
n2fcCommented:
Analysis of output:
Month 1 is January                       // generated by no-arg constructor
Invalid Month Number                // generated by setmonth from loop with 0
Month 1 is January                      // generated by setmonth from loop with 0
Month 1 is January                      // generated by setmonth from loop with 1
Month 2 is February                   // generated by setmonth from loop with 2
Month 3 is March                       // generated by setmonth from loop with 3
Month 4 is April                        // generated by setmonth from loop with 4
Month 5 is May                        // generated by setmonth from loop with 5
Month 6 is June                       // generated by setmonth from loop with 6
Month 7 is July                        // etc.
Month 8 is August
Month 9 is September
Month 10 is October
Month 11 is November
Month 12 is December                          // generated by setmonth from loop with 12

Open in new window

0
 
n2fcCommented:
Thanks for the comment...

Good to know I can still teach Java...
(Did so for many years!)
0
 
krakatoaCommented:
'Good Assist'. Lol.

the help from n2fc  he/she just seemed to understand better sorry

Open in new window


I gave you the correct answer first off.

You should have opened another question for your 'supplementary'.
0
 
krakatoaCommented:
krakatoa:   You said he was calling the constructor wrong... Maybe, but it DOESN'T address the ISSUE (which is a MISSING no-arg constructor)!

You can't say in the case of someone else's code, that the constructor was "missing". The code already has two constructors with arguments, and it's not up to you to decide whether there should be another one or not.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now