Computer Class Notes 2
Computer Class Notes 2
/*
* This is an example of a multiple line
comment using the slash and asterisk.
* This type of comment can be used to hold a
lot of information or deactivate
* code, but it is very important to remember
to close the comment.
*/
package fibsandlies;
import java.util.Map;
import java.util.HashMap;
/**
* This is an example of a Javadoc comment;
Javadoc can compile documentation
* from this text. Javadoc comments must
immediately precede the class, method,
* or field being documented.
* @author Wikipedia Volunteers
*/
public class FibCalculator extends Fibonacci
implements Calculator {
private static Map<Integer, Integer>
memoized = new HashMap<>();
/*
* The main method written as follows is
used by the JVM as a starting point
* for the program.
*/
public static void main(String[] args) {
memoized.put(1, 1);
memoized.put(2, 1);
System.out.println(fibonacci(12)); //
Get the 12th Fibonacci number and print to
console
}
/**
* An example of a method written in
Java, wrapped in a class.
* Given a non-negative number FIBINDEX,
returns
* the Nth Fibonacci number, where N
equals FIBINDEX.
*
* @param fibIndex The index of the
Fibonacci number
* @return the Fibonacci number
*/
public static int fibonacci(int fibIndex)
{
if (memoized.containsKey(fibIndex))
return memoized.get(fibIndex);
else {
int answer = fibonacci(fibIndex -
1) + fibonacci(fibIndex - 2);
memoized.put(fibIndex, answer);
return answer;
}
}
}