Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2012
    Posts
    7
    Rep Power
    0

    Default Fibonacci Number Calculator

    Hello, I have been learning Java for a while now and I have run into an roadblock with my code. What I am trying to do is run a Fibonacci Number Calculator while using a recursive method and not "for loopers".
    This is what I have so far.


    import java.util.*;
    public class Driver
    {
    private int temp = 0;
    private Scanner in;

    public Driver()
    {
    in = new Scanner(System.in);
    }
    public void getBase()
    {
    System.out.println("Hello and welcome to the Fibonacci Number Calculator!!!");
    System.out.print("First off what number position do us to calculate the Fib Value of? ");
    temp = in.nextInt();
    }
    public static void main(String[] args)
    {
    FibonacciNumbers fnum = new FibonacciNumbers();
    }
    public void displayNum()
    {
    System.out.print("The fib num of " +temp);
    System.out.println(" is " +fnum.calcFibonacci(temp) );
    }
    }

    public class FibonacciNumbers
    {

    public int calcFibonacci(int number)
    {

    if ( ( number == 0 ) || ( number == 1 ) )
    {
    return number;
    }

    else
    {
    return calcFibonacci( number - 1 ) + calcFibonacci( number - 2 );
    }


    }


    }


    The problem I am running into is that the variable I need to display the base also needs to be in the Driver which is "static" while also needing the Scanner in the same method because I need to run and return the value of the method in the other class.
    Any help is appreciated.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Fibonacci Number Calculator

    I am not really understand what your question was. But what I can see in you code is that you cannot run it correctly. What you need to change in my opinion are:

    1. In the main() method you should create an instance of the Driver class.
    2. Call the getBase() method from this instance.
    3. Call the displayNum() method from this instance.
    4. Move the creation of the FibonacciNumbers instance into the displayNum() method.

    Other improvement that you can make is move the temp variable and the scanner into the getBase() method. Instead of returning void this method can return the number inputted by the user. And then pass this number to the displayNum() method by adding a parameter to this method. So you'll have new signature for this method like:

    Java Code:
    public int getBase() {
    ...
    }
    
    public void displayNum(int number) {
    ...
    }

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Fibonacci Number Calculator

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need Help - StackOverflowError - Fibonacci
    By ausglanville in forum New To Java
    Replies: 3
    Last Post: 04-13-2011, 03:43 AM
  2. Fibonacci Help
    By zit1343 in forum New To Java
    Replies: 16
    Last Post: 01-24-2011, 01:09 AM
  3. Fibonacci sequece
    By Bgreen7887 in forum New To Java
    Replies: 10
    Last Post: 12-03-2010, 07:27 PM
  4. Fibonacci sequence
    By ŖΫ ỏ Ңόρę in forum New To Java
    Replies: 6
    Last Post: 03-25-2010, 06:59 AM
  5. help with fibonacci
    By likemine in forum New To Java
    Replies: 8
    Last Post: 01-07-2010, 02:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •