Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## Fibonacci Help!

First I will give an outline what I have to do.
The example below covers the case of n>0. You need to cover both >0 and <0.

class Power{

float power (float x,long n){

float pow=1;
for(int i=0;i<n;++i)
pow=pow*x;
}
return pow;
}

}

Now for my code
```import java.util.Scanner;

public class Power
{
//variables declarations
int base;
int power;
int pow;

public Power (int a, int n) //constructors for powers
{
base = a;
power = n;
}

public int getBase()
{
return base;
}

public int getExponent()
{
return power;
}

float power (float a,long n) //test for <
{
float pow=1;
for(int i=0;i<n;++i)
pow=pow*a;

return pow;

}

float power (float a,long n) //test for >
{
float pow=1;
for(int i=1;i>n;++i)
pow=pow*a;

return pow;
}
}```
My question is about the i>n statement of where to place it and I keep getting the error power(float,long) is already defined in Power
float power (float a,long n) //test for >

I really need help with this part, I just can not understand what to do here. Thanks  Reply With Quote

3. ## Re: Fibonacci Help!

Hello @GoPredsGo,

Actually your code is not wrong. But, both your member functions are having the same name, same return type, and same parameter list. So, the method overloading part isn't working (if at all you are trying to do it). For method overloading, function names are kept same but at least the return type or the parameter list must be different.

Now, if you are totally unaware of method overloading, just change the function names to power1 and power2. You wouldn't get the error.

Try this,

```import java.util.Scanner;

public class Power
{
//variables declarations
int base;
int power;
int pow;

public Power (int a, int n) //constructors for powers
{
base = a;
power = n;
}

public int getBase()
{
return base;
}

public int getExponent()
{
return power;
}

float power1 (float a,long n) //test for <
{
float pow=1;
for(int i=0;i<n;++i)
pow=pow*a;

return pow;

}

float power2 (float a,long n) //test for >
{
float pow=1;
for(int i=1;i>n;++i)
pow=pow*a;

return pow;
}
}```

or you can try this....

```import java.util.Scanner;

public class Power
{
//variables declarations
int base;
int power;
int pow;

public Power (int a, int n) //constructors for powers
{
base = a;
power = n;
}

public int getBase()
{
return base;
}

public int getExponent()
{
return power;
}

float power (float a,long n) //test for <
{
float pow=1;
for(int i=0;i<n;++i)
pow=pow*a;

return pow;

}

double power (float a,long n) //test for >
{
float pow=1;
for(int i=1;i>n;++i)
pow=pow*a;

return pow;
}
}```  Reply With Quote

4. ## The Following User Says Thank You to KaustavBanerjee For This Useful Post:

GoPredsGo (July 16th, 2013)

5. ## Re: Fibonacci Help!

Hi GoPredsGo, welcome to the forum! I hope you will stick around.

First of all, the code is not compilable, because it contains a duplicate. I'm talking about the last two methods:

```float power (float a,long n) //test for <
{
float pow=1;
for(int i=0;i<n;++i)
pow=pow*a;

return pow;

}

float power (float a,long n) //test for >
{
float pow=1;
for(int i=1;i>n;++i)
pow=pow*a;

return pow;
}```

What you will wan't to do, is to either combine these two methods or rename one of them to something else. I would go with the first one. If you need any sample code, I'll gladly give you some. Good luck!  Reply With Quote

6. ## The Following User Says Thank You to Cronus For This Useful Post:

GoPredsGo (July 16th, 2013)

7. ## Re: Fibonacci Help! Originally Posted by KaustavBanerjee or you can try this....

```import java.util.Scanner;

public class Power
{
//variables declarations
int base;
int power;
int pow;

public Power (int a, int n) //constructors for powers
{
base = a;
power = n;
}

public int getBase()
{
return base;
}

public int getExponent()
{
return power;
}

float power (float a,long n) //test for <
{
float pow=1;
for(int i=0;i<n;++i)
pow=pow*a;

return pow;

}

double power (float a,long n) //test for >
{
float pow=1;
for(int i=1;i>n;++i)
pow=pow*a;

return pow;
}
}```
Does that compile?  Reply With Quote

8. ## The Following User Says Thank You to pbrockway2 For This Useful Post:

GoPredsGo (July 16th, 2013)

9. ## Re: Fibonacci Help!

Not that did not compile, but I was able to edit my code from the suggestions here. I want to think everyone for there help, this is one of those times that no matter what I did I just could not comprehend what I was doing.  Reply With Quote