// Fig. 4.13: FibonacciTest.java // Recursive fibonacci method import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class FibonacciTest extends Applet implements ActionListener { Label numLabel, resultLabel; TextField num, result; public void init() { numLabel = new Label( "Enter an integer and press return" ); num = new TextField( 10 ); num.addActionListener( this ); resultLabel = new Label( "Fibonacci value is" ); result = new TextField( 15 ); result.setEditable( false ); add( numLabel ); add( num ); add( resultLabel ); add( result ); } public void actionPerformed( ActionEvent e ) { long number, fibonacciValue; number = Long.parseLong( num.getText() ); showStatus( "Calculating ..." ); fibonacciValue = fibonacci( number ); showStatus( "Done." ); result.setText( Long.toString( fibonacciValue ) ); } // Recursive definition of method fibonacci long fibonacci( long n ) { if ( n == 0 || n == 1 ) // base case return n; else return fibonacci( n - 1 ) + fibonacci( n - 2 ); } }