Chapter 6 Methods 283 40 (1 on 1 web hosting) // create numberField,

Chapter 6 Methods 283 40 // create numberField, make it uneditable 41 // and attach it to content pane 42 resultField = new JTextField( 15 ); 43 resultField.setEditable( false ); 44 container.add( resultField ); 45 46 } // end method init 47 48 // obtain user input and call method fibonacci 49 public void actionPerformed( ActionEvent e ) 50 { 51 long number, fibonacciValue; 52 53 // obtain user s input and conver to long 54 number = Long.parseLong( numberField.getText() ); 55 56 showStatus( “Calculating …” ); 57 58 // calculate fibonacci value for number user input 59 fibonacciValue = fibonacci( number ); 60 61 // indicate processing complete and display result 62 showStatus( “Done.” ); 63 resultField.setText( Long.toString( fibonacciValue ) ); 64 65 } // end method actionPerformed 66 67 // Recursive definition of method fibonacci 68 public long fibonacci( long n ) 69 { 70 // base case 71 if ( n == 0 || n == 1 ) 72 return n; 73 74 // recursive step 75 else 76 return fibonacci( n -1 ) + fibonacci( n -2 ); 77 78 } // end method fibonacci 79 80 } // end class FibonacciTest Fig. 6.13 Recursively generating Fibonacci numbers (part 2 of 3). Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/3/01

Leave a Reply