# Fibonacci Help!

• July 15th, 2013, 10:43 PM
GoPredsGo
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
Code Java:

```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
• July 16th, 2013, 12:29 AM
KaustavBanerjee
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,

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 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....

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;   }   double power (float a,long n) //test for > { float pow=1; for(int i=1;i>n;++i) pow=pow*a;   return pow; } }```
• July 16th, 2013, 12:30 AM
Cronus
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:

Code :

```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!
• July 16th, 2013, 02:01 AM
pbrockway2
Re: Fibonacci Help!
Quote:

Originally Posted by KaustavBanerjee
or you can try this....

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;   }   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?
• July 16th, 2013, 06:47 PM
GoPredsGo
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.