Home Segments Top Top Previous Next

440: Mainline

Suppose, for example, that you want to write a method, recursivePowerOf2, that computes the nth power of 2 recursively. One way to start is to define recursivePowerOf2 in terms of the powerOf2 method already provided in Chapter 24:

public static int recursivePowerOf2 (int n) { 
 return powerOf2(n); 
} 

Once you see that you can define recursivePowerOf2 in terms of powerOf2, you are ready to learn how gradually to turn recursivePowerOf2 into a recursive method that does not rely on powerOf2.