/**
   Generates the n'th Fibonacci number
 */
import java.util.* ;

public class FibonacciGeneratorTester
{
    public static void main(String[] args)
    {
	System.out.println("The 1st Fibonacci number is: "
			   + getFibonacci(1)) ;
	System.out.println("Expected: 1") ;
	System.out.println("The 3rd Fibonacci number is: "
			   + getFibonacci(3)) ;
	System.out.println("Expected: 2") ;
	System.out.println("The 5th Fibonacci number is: "
			   + getFibonacci(5)) ;
	System.out.println("Expected: 5") ;
	System.out.println("The 10th Fibonacci number is: "
			   + getFibonacci(10)) ;
    }
    /**
       A static method to return the n'th Fibonacci number
       @param n the index of the Fibonacci number
       @return the n'th Fibonacci number
     */
    public static int getFibonacci(int n)
    {
	FibonacciGenerator generator = new FibonacciGenerator() ;
	int result = 0 ;
	if (n == 1 || n == 2)
	    result = 1 ;
	else {
	    	for (n=0; n < 21; n++)
        {
            System.out.println(getFibonacci(n));
        }// todo: a for-loop that runs from 3 to n calling the generator

	}
	return result ;
    }
}
/**
   Generates Fibonacci numbers.
*/
public class FibonacciGenerator
{
    //instance variables
    private int recent ; //one values ago
    private int previous ; //two values ago

    /**
       Constructs the generator by setting the instance variables to 1
    */
    public FibonacciGenerator()
    {
	recent = 1;
        previous = 1;// todo: Fill in the constructor

    }

    /**
       Produces the next number
       @return the next in the sequence
    */
    public int next()
    {
	int result = recent + previous ;
	recent++;
        previous++;
        return result;// todo: Update previous and recent, and return result.

    }
}

im getting an stackoverflow error, dunno what to do :S

Recommended Answers

All 14 Replies

You are not incrementing the values properly in the next() method:

public int next()
{
  int result = recent + previous ;
  previous = recent;
  recent = result;
  return result;// todo: Update previous and recent, and return result. 
}

Your tester is basically a recursion that doesn't do anything - you have created the generator but inside the for loop instead of calling the next() method of the generator you are calling the getFibonacci again - you have an infinite recursion.

Stack overflow usually means you have an infinite recursion. Try stepping through the code in a debugger, or putting in some print statements so you can see where the recursion is not stopping.

Hint: each recursive call should make the problem simpler so eventually the algorithm termindates.

System.out.println(getFibonacci(n));
what should that be?

Maybe you should call generator.next() ?

thanks, i ran it and got this o.o
The 1st Fibonacci number is: 1
Expected: 1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
The 3rd Fibonacci number is: 0
Expected: 2
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
The 5th Fibonacci number is: 0
Expected: 5
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
10946
17711
The 10th Fibonacci number is: 0

In your getFibonacci method where you call generator.next(), what are you doing with the return value?

really dont know, im new at this
the return was already there, i just had o fill in blanks

OK. Try result = generator.next() .
Also, your for loop goes from 0 to 20, but it's supposed to go from 3 to n.

what do i type to make it 3 to n?

if it goes from 3 to n
wont n be an infinite?

No, since now you are not recursing as before, but iterating in a loop until the desired number.

Yes, since 1 and 2 are covered in the if clause, and if you are reaching the for loop it means you are in the else clause, meaning n cannot be 1 or 2, and is starting from 3 until the desired number n.

You need a different variable, like i.

for (int i = 3; i < n; i++) ...

thanks, i get this now
The 1st Fibonacci number is: 2
Expected: 1
The 3rd Fibonacci number is: 2
Expected: 2
2
3
The 5th Fibonacci number is: 5
Expected: 5
2
3
5
8
13
21
34
The 10th Fibonacci number is: 55
i wonder why the first number is wrong

In a previous post, the 1st number was coming out correctly. Did you change the handling of 1 & 2 in your getFibonacci method? If you can't see what changed, post your code again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.