Java Reference
In-Depth Information
return mystery4(x - y, y);
}
}
For each of the following calls, indicate the value that is returned:
a. mystery4(6, 13)
b. mystery4(14, 10)
c. mystery4(37, 10)
d. mystery4(8, 2)
e. mystery4(50, 7)
12. Consider the following method:
public static int mystery5(int x, int y) {
if (x < 0) {
return -mystery5(-x, y);
} else if (y < 0) {
return -mystery5(x, -y);
} else if (x == 0 && y == 0) {
return 0;
} else {
return 100 * mystery5(x / 10, y / 10) + 10 * (x % 10) + y % 10;
}
}
For each of the following calls, indicate the value that is returned:
a. mystery5(5, 7)
b. mystery5(12, 9)
c. mystery5(-7, 4)
d. mystery5(-23, -48)
e. mystery5(128, 343)
13. Convert the following iterative method into a recursive method:
// Returns n!, such as 5! = 1*2*3*4*5
public static int factorial(int n) {
int product = 1;
for (int i = 1; i <= n; i++) {
product *= i;
}
return product;
}
14. The following method has a bug that leads to infinite recursion. What correction fixes the code?
// Adds the digits of the given number.
// Example: digitSum(3456) returns 3+4+5+6 = 18
Search WWH ::




Custom Search