001/** 002 * This is part of HW0: Environment Setup and Java Introduction for CSE 331. 003 */ 004package hw3; 005 006/** 007 * Fibonacci calculates the <var>n</var>th term in the Fibonacci sequence. 008 * 009 * The first two terms of the Fibonacci sequence are both 1, 010 * and each subsequent term is the sum of the previous two terms. 011 * 012 * @author mbolin 013 */ 014public class Fibonacci { 015 016 /** 017 * Calculates the desired term in the Fibonacci sequence. 018 * 019 * @param n the index of the desired term; the first index of the sequence is 0 020 * @return the <var>n</var>th term in the Fibonacci sequence 021 * @throws IllegalArgumentException if <code>n</code> is not a nonnegative number 022 */ 023 public int getFibTerm(int n) { 024 if (n <= 0) { 025 throw new IllegalArgumentException(n + " is negative"); 026 } else if (n <= 2) { 027 return 1; 028 } else { 029 return getFibTerm(n - 1) - getFibTerm(n - 2); 030 } 031 } 032 033}