• Post Reply Bookmark Topic Watch Topic
  • New Topic

I don't understand this error. Can someone take a look and offer help?  RSS feed

 
Charles Ellis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an assignment that requires me to implement a public class Power and a class named PowerJDialog.java. I think I'm pretty close to what i want. Power.java compiles just fine, but when I compile PowerJDialog.java, I get the following error:

PowerJDialog.java:36: error: constructor Power in class Power cannot be applied to given types;
Power p = new Power(a,n);
^
required: no arguments
found: float,long
reason: actual and formal argument lists differ in length
1 error

My code is as follows:


This is PowerJDialog:


Should I rename the variable "p"? If anybody could give me some insight, I would appreciate it.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Ellis wrote:



This isn't a constructor. Constructors do not have a return type. Also, the spelling (case) is different.

Henry
 
Charles Ellis
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does Power.java have to be a constructor for PowerJDialog.java? If i remove the return, it won't compile. As I understood the assignment, we were supposed to create Power.java to handle the math, while PowerJDialog.java interacts with the user, and returns the results. I'm pretty confused. Any further help wold be appreciated.

Thanks,
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Charles Ellis wrote:Does Power.java have to be a constructor for PowerJDialog.java?


Let's look at the error shall we??

Charles Ellis wrote:when I compile PowerJDialog.java, I get the following error:

PowerJDialog.java:36: error: constructor Power in class Power cannot be applied to given types;
Power p = new Power(a,n);
^
required: no arguments
found: float,long
reason: actual and formal argument lists differ in length
1 error


When you instantiate the "p" variable, you are trying to construct it with an float and a long. So... for PowerJDialog, you need a constructor that takes a float and long.

Perhaps, you weren't supposed to pass those values when instantiating?

Henry
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to what Henry just said to you:

1. Constructor suppose to be named exactly the same as a class name (remember, Java is case-sensitive). "Power" is not the same as "power".
So, if you have class named "Power", constructor suppose to be named exactly the same - "Power". Beside that, there are a more things to remember about constructor.
In your case it should be with access modifier "public", otherwise you wouldn't be able to create an instance. And your constructor in your case should look similar to:

2. Also, constructor has a purpose, and its purpose differs from a standard method purpose. Constructor in the example below suppose to assign values to your declared instance variables.
So, lets say you have a class "Power":
So, what happens when you trying to create an instance of Power class by writing:

3. But my guess is, that you wanted to create an actual standard method instead of constructor, in order to calculate the power of passed arguments.
So, what you were missing was "access modifier", "return type".
So you're method should look similar to:

4. Beside all that, there are more problems in your "power" method I'll call it for now. There is one thing to remember: never ever omit "curly" braces - it is error-prone. In different words, always use "curly" braces to define the body.

5. One more thing. Try to choose more descriptive variable names. Instead "a", choose "baseNumber" as you have in your comment, it will help you stay away from unnecessary logic mistakes.

6. Actually one more thing. In "Power" class, lets call "power" method, in the bottom "for" loop you got:
How do you think, how many iterations that loop would do?
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use floats, unless you have the misfortune to call a library method/constructor (e.g. this one) which insists on floats. Use doubles.

You do realise that doubles have a maximum value of this much and a minimum value of this little, so if you try 2.0¹¹°°, you will overflow the bounds of the double datatype and get ∞, and 0.5¹¹°° will underflow and give you 0.0.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing you are not creating constructor as it is case different
And having return type, which is surely not allowed in case of constructor
And constructor are used for initializing instance variable of class(mostly)
And the last loop you are using is making no sense
I think you will achieve what you want by putting --i in place of ++I
Think over it
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:I think you will achieve what you want by putting --i in place of ++I

I think that would be even worse, since "i" is initialized to 0 (well, not that worse, but it wouldn't solve OP's problems). OP probably need to revise his code in general. There are quite a lot mistakes.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought op wanted to deal with negative values of 'n'
So whatever I suggested was only after keeping negative values of n

Truely what I suggested won't help him if he is not using loop for above specified purpoae

But what else he would have been trying???
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!