O'Reilly logo

Java™ How to Program, Seventh Edition by P. J. Deitel - Deitel & Associates, Inc., H. M. Deitel - Deitel & Associates, Inc.

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercises

15.7What does the following code do?
1 public int mystery(int a, int b)
2 {
3    if (b == 1)
4       return a;
5    else
6       return a + mystery(a, b - 1);
7 } // end method mystery
15.8Find the error(s) in the following recursive method, and explain how to correct it (them). This method should find the sum of the values from 0 to n.
 1 public int sum(int n)
 2 {
 3    if ( n == 0 )
 4       return 0;
 5    else
 6       return n + sum( n );
 7 } // end method sum
15.9(Recursive power Method) Write a recursive method power(base, exponent) that, when called, returns

baseexponent

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

baseexponent = base · base exponent ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required