Question

Recursive Tracing. For each call to the following method, indicate what value is returned: public static...

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);

}

Call

Value Returned

mystery(0)

mystery(5)

mystery(13)

mystery(297)

mystery(-3456)

}

Can any one help me with it?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
Recursive Tracing. For each call to the following method, indicate what value is returned: public static...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Consider the following method: Linel: public static int mystery(int n) { Line2: if (n < 10)...

    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...

    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)...

    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....

    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...

    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...

    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)...

  • Write a full program that will accept exactly  three command line arguments that can be interpreted as...

    Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program. Suppose your program is called Add.java. Then running the command java Add 1 2 3 will output 6, and running java Add, for example, will cause the program to shut down. Consider the following recursive method: public static int mystery (int n)...

  • What does this program print? package javaapplication210; public class JavaApplication210 { public static void main(String[]args){ System.out.printf("Result...

    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); } }

  • 24) (3x2 marks) Consider the following method: public static int mysteryl (int a, int b) (...

    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...

  • For each call to the following method, indicate what console output is produced: public void mystery2(int...

    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);

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT