0

```
/**
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