Recursive Tracing. For each call to the following method, indicate what value is returned:
public static int mystery(int n) {
if (n < 0) {
return -mystery(-n);
} else if (n == 0) {
return 0;
} else {
return mystery(n / 10) * 10 + 9 - (n % 10);
}
|
}
Can any one help me with it?
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new java program with name "main.java" is created, which contains following code.
main.java :
//Main class
public class Main
{ //Main method
public static void main(String[] args) {
//call to method
System.out.println("mystery(5) :
"+mystery(5));
}
//method defination
public static int mystery(int n)
{ //if n is less than 0
if (n < 0)
{
return -mystery(-n);
}
//if n=0
else if (n==0)
{
return 0; //return 0
}
else
{ //return recursive call
return mystery(n / 10) * 10 + 9 - (n % 10);
}
}
}
======================================================
Output : Compile and Run above program , will get the screen as shown below
Screen 1 :Screen when mystery(0) called
Screen 2 :Screen when mystery(5) called
Screen 3 :Screen when mystery(13) called
Screen 4 :Screen when mystery(297) called
Screen 5 :Screen when mystery(-3456) called
Below table shows details
Call |
Value Returned |
mystery(0) |
0 |
mystery(5) |
4 |
mystery(13) |
86 |
mystery(297) |
702 |
mystery(-3456) |
|
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.
Recursive Tracing. For each call to the following method, indicate what value is returned: public static...
Consider the following method: Linel: public static int mystery(int n) { Line2: if (n < 10) { ine3: return n; Line4: } else { Line5: int a = n/10; Line 6: int b = n % 10; Line 7: return mystery(a + b); Line 8: } Line 9: } What is the result of the following call? System.out.println(mystery(648)); 18 8 12
3. Consider the mystery method given. public static int mystery ( int n) [ if (n == 0 ) { return 1; How do we get the values for recurse? else if (n%2 == 0 ) { int recurse = mystery ( n - 1); int result = recurse + n; return result; since n =5, we go to the else statement and do int recurse = mystery(5-1) which equals 4? why is 3 written? else { int recurse =...
these are right answer but i dont know how to do it, can u explain mystery5(12,9) and mystery5(128,343) O BJP3 Self-Check 12.14: mystery5 Language/Type: 血Java recursion recursive tracing Whitaker Brand (on 2013/04/01) Author: For each call to the following method, indicate what value is returned public int mystery5(int x, int y) if(x<0) { } else if (y<) } else if (x && y j else t -mystery$(-х, у); return return -mystery5(x, -y); 0) { return 0; return 100 * mystery5(x...
Let’s work together to develop a call tree for the execution of the following recursive method. (The method allows us to recursively generate the nth integer in the Fibonacci sequence, although you don’t need to be familiar with that sequence to understand this problem.) public static int fib(int n) { if (n == 0 || n == 1) { return 1; } else { int prev1 = fib(n - 2); int prev2 = fib(n - 1); return prev1 + prev2;...
The following recursive method factRecursive computes the factorial of positive integer n. Demonstrate that this method is recursive. public static int factRecursive(int n) { int result = 0; if (n == 0) { result = 1; } else { result = n * factRecursive(n - 1); } return result; }
Java The following questions ask about tracing a binary search. To trace the binary search, list the value of first, last, and mid for each pass through the loop or method. Use one of these methods for your trace. public static int binarySearchIterative(int[] numbers, int target) { boolean found = false; int first = 0; int last = numbers.length - 1; while (first <= last && !found) { int mid = (first + last) / 2; if (numbers[mid] == target)...
24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) ( int result 0: if (a <b) ( else if (a b) else ( return result: result mystery2 (a) mystery2 (a)i result - mystery2 (b) result-ab; public static int mystery2 (int x) f int countx for (int i 0; іск; i++) count +1: return counti What are the values stored in the variable result after the following method calls? a) int result mysteryl(4,1): b) int...
What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result is: % d/n", mystery (-5, -9));//System.out.printf("Result is: % d/n", mystery (-4, -8));//System.out.printf("Result is: % d/n", mystery (-6, -7));//} public static int mystery(int a, int b) { if(b - 1) returns a; else return a + mystery(a, b + 1); } }
For each call to the following method, indicate what console output is produced: public void mystery2(int n) { if (n > 100) { System.out.print(n); } else { mystery2(2 * n); System.out.print(", " + n); } } Sound F/X mystery2(113); mystery2(70); mystery2(42); mystery2(30); mystery2(10);
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...