# MSIS655 Week12

Document Sample

```					                   MSIS 655

Week 11
Recursion (Ch. 15)

9/15/2012                                   11.1
Introduction
Earlier programs are structured as methods
that call one another in a disciplined,
hierarchical manner
Recursive methods
     Call themselves
     Useful for some problems to define a method to
call itself
     Can be called directly or indirectly through another
method

9/15/2012                                                   11.2
Fig. 15.2 | Recursive evaluation of 5!.
9/15/2012                                             11.3
1    // Fig. 15.3: FactorialCalculator.java
2    // Recursive factorial method.
3
4    public class FactorialCalculator
5    {
6        // recursive method factorial
7        public long factorial( long number )
8        {
9            if ( number <= 1 ) // test for base case
10              return 1; // base cases: 0! = 1 and 1! = 1
11           else // recursion step
12             return number * factorial( number - 1 );
13       } // end method factorial
14
15       // output factorials for values 0-10
16       public void displayFactorials()
17       {
18           // calculate the factorials of 0 through 10
19           for ( int counter = 0; counter <= 10; counter++ )
20              System.out.printf( "%d! = %d\n", counter, factorial( counter ) );
21       } // end method displayFactorials
22 } // end class FactorialCalculator

9/15/2012                                                            11.4
Recursion Concepts
     If method is called with more complex
problem, problem divided into two
pieces—a piece the method knows how
to do (base case) and a piece the
method does not know how to do (called
recursive call or recursion step)

9/15/2012                                       11.5
Recursion Concepts
Recursive problem-solving elements
     Base case
 Recursive method capable of solving only simplest
case—the base case
 If method is called with base case, method returns result
     Recursive call/recursion step
 Must resemble original problem but be slightly simpler or
smaller version
 Method calls fresh copy of itself to work on smaller
problem
 Normally includes return statement

9/15/2012                                                             11.6
Example Using Recursion:
Fibonacci Series
1    // Fig. 15.5: FibonacciCalculator.java
2    // Recursive fibonacci method.
3
4    public class FibonacciCalculator
5    {
6       // recursive declaration of method fibonacci
7       public long fibonacci( long number )
8       {
9          if ( ( number == 0 ) || ( number == 1 ) ) // base cases
10            return number;
11         else // recursion step
12            return fibonacci( number - 1 ) + fibonacci( number - 2 );
13      } // end method fibonacci
14
15      public void displayFibonacci()
16      {
17         for ( int counter = 0; counter <= 10; counter++ )
18            System.out.printf( "Fibonacci of %d is: %d\n", counter,
19               fibonacci( counter ) );
20      } // end method displayFibonacci
21 } // end class FibonacciCalculator

9/15/2012                                                                11.7
Fig. 15.7 | Set of recursive calls for fibonacci( 3 ).

9/15/2012                                                            11.8
Recursion and the Method Call Stack

Method call stack used to keep track of method
calls and local variables within a method call
Just as with nonrecursive programming,
recursive method calls are placed at the top of
the method call stack (push)
As recursive method calls return, their activation
records are popped off the stack and the
previous recursive calls continue executing
Current method executing is always method
whose activation record is at top of stack

9/15/2012                                       11.9
Fig. 15.8 | Method calls made within the call fibonacci( 3 ).

9/15/2012                                                             11.10
Fig. 15.9 | Method calls on the program execution stack.

9/15/2012                                                          11.11
Lab activities (Week 11)
Exercises (pp. 782-783)
15.16, 15.17, 15.18

Fig. 15.12 – p. 758)

9/15/2012                              11.12

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 0 posted: 9/15/2012 language: English pages: 12