Results 1 to 3 of 3
Thread: Fibonacci Number Calculator
- 11-27-2012, 11:26 PM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
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.
- 11-28-2012, 12:45 AM #2
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
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) { ... }Website: Learn Java by Examples
- 11-28-2012, 02:56 AM #3
Re: Fibonacci Number Calculator
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Need Help - StackOverflowError - Fibonacci
By ausglanville in forum New To JavaReplies: 3Last Post: 04-13-2011, 03:43 AM -
Fibonacci Help
By zit1343 in forum New To JavaReplies: 16Last Post: 01-24-2011, 01:09 AM -
Fibonacci sequece
By Bgreen7887 in forum New To JavaReplies: 10Last Post: 12-03-2010, 07:27 PM -
Fibonacci sequence
By ŖàΫ ỏƒ Ңόρę in forum New To JavaReplies: 6Last Post: 03-25-2010, 06:59 AM -
help with fibonacci
By likemine in forum New To JavaReplies: 8Last Post: 01-07-2010, 02:32 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks