I have a program that i need to complete. I wrote the first one (FibonacciGenerator) with stated variables - everything worked fine
I now want to split it up so that the user can give a number and the program gives the correct number back. (ie, fib(5)=5 AND NOT fib(5)=2)
Please help as I know what i need to do but the how escapes me.
constructor portion
/* Class used to generate a Fibonacci number with a given input */ public class FibonacciGenerator { /** Construct a FibonacciGenerator object to generate a Fibonacci number */ public FibonacciGenerator() { initialize(); } /** Initializes the FibonacciGenerator object. Allows the object to be reset and reused. */ public void initialize() { fold1 = 1; fold2 = 1; fnew = 1; count = 0; } /** Method used to calculate a fibonacci number @return fnew the fibonacci number */ public int nextNumber() { if(count < 2) { count++; return 1; } else { fnew = fold1 + fold2; fold2 = fold1; fold1 = fnew; return fnew; } } private int fold1; private int fold2; private int count; private int fnew; }
Test Program (I know i need to return the input to the constructor file but how??)
import javax.swing.JOptionPane; /** Test driver class for FibonacciGenerator class */ public class FibonacciGeneratorTester { public static void main(String[] args) { String input = JOptionPane.showInputDialog( "Enter a Number or hit ''Cancel'' to exit:"); int n = Integer.parseInt(input); FibonacciGenerator fg = new FibonacciGenerator(); int next = n; if (n < 2) next = 1; else { for (int i = 3; i <= n; i++) { next = fg.nextNumber(); } } System.out.println("fib(" + n + ") = " + next); System.exit(0); } }
Any help is appreciated