01: import javax.swing.JOptionPane;
02: 
03: /**
04:    This program prints trace messages that show how often the
05:    recursive method for computing Fibonacci numbers calls itself.
06: */ 
07: public class FibTrace
08: {
09:    public static void main(String[] args)
10:    {  
11:       String input = JOptionPane.showInputDialog("Enter n: ");
12:       int n = Integer.parseInt(input);
13: 
14:       int f = fib(n);
15: 
16:       System.out.println("fib(" + n + ") = " + f);
17:       System.exit(0);
18:    }
19: 
20:    /**
21:       Computes a Fibonacci number.
22:       @param n an integer
23:       @return the nth Fibonacci number
24:    */
25:    public static int fib(int n)
26:    {
27:       System.out.println("Entering fib: n = " + n);
28:       int f;
29:       if (n <= 2) f = 1;
30:       else f = fib(n - 1) + fib(n - 2);
31:       System.out.println("Exiting fib: n = " + n
32:         + " return value = " + f);
33:       return f;
34:    }
35: }