220 Methods Chapter 6 15 16 private System.Windows.Forms.Label (Photo web hosting)
220 Methods Chapter 6 15 16 private System.Windows.Forms.Label displayLabel; 17 private System.Windows.Forms.Label promptLabel; 18 19 // Visual Studio .NET generated code 20 21 // call Fibonacci and display results 22 protected void calculateButton_Click( 23 object sender, System.EventArgs e ) 24 { 25 int number = Convert.ToInt32( inputTextBox.Text ); 26 int fibonacciNumber = Fibonacci( number ); 27 displayLabel.Text = 28 “Fibonacci Value is ” + fibonacciNumber; 29 } 30 31 // calculates Fibonacci number 32 public int Fibonacci( int number ) 33 { 34 if ( number == 0 || number == 1 ) 35 return number; 36 else 37 return Fibonacci( number - 1 ) + Fibonacci( number -2 ); 38 } 39 40 // main entry point for the application 41 [STAThread] 42 static void Main() 43 { 44 Application.Run( new FibonacciTest() ); 45 } 46 47 } // end of class FibonacciTest Fig. 6.16 Recursively generating Fibonacci numbers. (Part 2 of 2.) The call to Fibonacci (line 26) from calculateButton_Click is not a recursive call, but all subsequent calls to Fibonacci from line 37 are recursive. Each time Fibonacci is invoked, it immediately tests for the base case number equal to 0 or 1 (line 34). If this is true, Fibonacci returns number(fibonacci(0) is 0 and fibonacci(1) is 1). Interestingly, if number is greater than 1, the recursion step generates two recursive calls (line 37), each of which is for a slightly simpler problem than the original call to Fibonacci. Figure 6.17 shows how method Fibonacci would evaluate Fibonacci(3).
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision J2ee Web Hosting services.