# Exercises

**18.7**What does the following code do?**1**public int mystery(int a, int b) {**2**if (b == 1) {**3**return a;**4**}**5**else {**6**return a + mystery(a, b - 1);**7**}**8**}**18.8**Find 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**if (n == 0) {**3**return 0;**4**}**5**else {**6**return n + sum(n);**7**}**8**}**18.9**Write a recursive method*(Recursive*`power`

Method)`power(base, exponent)`

that, when called, returns$${\mathit{\text{base}}}^{\text{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$${\mathit{\text{base}}}^{\text{exponent}}=\mathit{\text{base}}\cdot {\mathit{\text{base}}}^{\text{exponent}-1}$$and the terminating condition ...

Get *Java How to Program, Early Objects, 11th Edition* now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.