Lab 5

(week of Feb 23-27)


The purpose of this lab is to practice recursion.

There is only one part in Lab 6. No debug questions in Lab 6.


Question 1:

Write a Java program that implements a recursive method power( base, exponent ) that, when called, returns

base exponent

For example, power( 3, 4 ) = 3 * 3 * 3 * 3. Assume that exponent is an integer greater than or equal to 1. (Hint: The recursion step should use the relationship

base exponent = base · base exponent – 1

and the terminating condition occurs when exponent is equal to 1, because

base1 = base


Question 2:

Continue on Question 1, when computing

     base exponent

If exponent is even, then

     base exponent =(base exponent/2)2

Improve your program in Question 1 by making use of the fact above.



Question 3:

Write a Java Program , using recursion, compute the sum of all values in an array.

Your program skeleton may look like the one below:

public class DataSet {

    public DataSet (int [] anArray) {.....}

    public int getSum ( ) { .....   }

    ....... ....

}