| Name: | _______________________________ |
| Student ID: | _______________________________ |
class Date { int year, month, day; }
class Person { Date birthDate; String name; }
class Student extends Person { float GPA; }
class Employee extends Person { }
public class Fibonacci {
long[] cache; // pre-calculated Fibs
final int DEFAULT_SIZE = 100; // default arg to constructor
private void clearCache() {
cache[0] = cache[1] = 1;
for (int i = 2; i lt; cache.length; i++) {
cache[i] = 0;
}
}
Fibonacci( int size) {
if (size lt; 2) size = 2;
cache = new long[size];
clearCache();
}
long fib( int n ) {
if (n > cache.length) return 0;
if (cache[n] != 0) return cache[n];
cache[n] = fib(n-2) + fib(n-1);
return cache[n];
}
public static void main( String args[] ) {
Fibonacci f = new Fibonacci(51);
System.out.println("The 50th Fibonacci number is " + f.fib(50));
}
}