package jspexamples; import java.util.*; import java.io.*; /** * Computes the fiboacci series, returns the next value as a bean. * Developed as an example for SWE 642, November 2000 * * @author Jeff Offutt * @version 2.0 * 22-March-2009 */ public class fiboBean { // These are inner variables, not class variables. private int N, Nminus1, Nminus2; public static void main (String args[]) { // for testing. int fib_val; fiboBean fb = new fiboBean(); for (int i=0;/* loop until exception */ ;i++) { try { fib_val = fb.getFibo (); System.out.println (i + ": " + fib_val); } catch (overflowException e) { System.out.println (e); break; // from for loop } } // for } // end of main /** * Default constructor initializes the recursion. * Nminus1 and Nminus2 are the previous two values. * N counts how many Fibonacci values have been returned. * @author Jeff Offutt */ public fiboBean () { Nminus1 = 1; Nminus2 = 1; N = 0; } /** * The getter computes and returns the next value * in the Fibonacci series. When we overflow, that tells * the client that we are finished. * @author Jeff Offutt */ public int getFibo () throws overflowException { int ret_val; if (N==0 || N==1) { N++; ret_val = 1; } else { ret_val = Nminus1+Nminus2; if (ret_val < 0) throw new overflowException ("Integer overflow"); N++; Nminus2 = Nminus1; Nminus1 = ret_val; } return (ret_val); } // end of GetFibo } //end of class