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 */ 014@SuppressWarnings("nullness") 015public class Fibonacci { 016 017 /** 018 * Calculates the desired term in the Fibonacci sequence. 019 * 020 * @param n the index of the desired term; the first index of the sequence is 0 021 * @return the <var>n</var>th term in the Fibonacci sequence 022 * @throws IllegalArgumentException if <code>n</code> is not a nonnegative number 023 */ 024 public int getFibTerm(int n) { 025 if (n <= 0) { 026 throw new IllegalArgumentException(n + " is negative"); 027 } else if (n <= 2) { 028 return 1; 029 } else { 030 return getFibTerm(n - 1) - getFibTerm(n - 2); 031 } 032 } 033 034}