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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Fibonacci Help!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Junior Member
    Join Date
    Nov 2012
    Location
    Kolkata, India
    Posts
    8
    My Mood
    Cool
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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;
    	}
    }

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

    GoPredsGo (July 16th, 2013)

  4. #3
    Member Cronus's Avatar
    Join Date
    Feb 2013
    Location
    Gothenburg, Sweden
    Posts
    41
    My Mood
    Inspired
    Thanks
    6
    Thanked 7 Times in 6 Posts

    Default 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!

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

    GoPredsGo (July 16th, 2013)

  6. #4
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Fibonacci Help!

    Quote Originally Posted by KaustavBanerjee View Post
    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?

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

    GoPredsGo (July 16th, 2013)

  8. #5
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

Similar Threads

  1. Fibonacci Series
    By 13cmwest in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2013, 04:39 AM
  2. Fibonacci sequence
    By ssohpkc in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 8th, 2013, 09:29 PM
  3. The Fibonacci sequence
    By Ryuk_93 in forum Android Development
    Replies: 1
    Last Post: March 26th, 2012, 11:56 AM
  4. Fibonacci series
    By seanman in forum Algorithms & Recursion
    Replies: 4
    Last Post: September 11th, 2011, 12:32 PM
  5. Fibonacci Spiral Help?
    By cmh0114 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 12th, 2010, 09:21 PM