Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

[JAVA] Study Guide Problems 3. Recursion Mystery . Consider the following method

ID: 3834451 • Letter: #

Question

[JAVA] Study Guide Problems

3. Recursion Mystery. Consider the following method:

public int mystery3(int n)
{
if (n < 0)
return -mystery3(-n);
  
else if (n < 10)
return n;
  
else
return mystery3(n/10 + n % 10);
}

For each call below, indicate what value is returned:

Method Call Value Returned

mystery3(6) _____________

mystery3(17) _____________

mystery3(259) _____________

mystery3(977) _____________

mystery3(-479) ____________

Link to Source PDF:

https://www.dropbox.com/s/1lrjfjs38plzska/Practice_Final_Exam.pdf?dl=0

Explanation / Answer

Method Call Value Returned

mystery3(6) ________6_____

mystery3(17) ________8_____

mystery3(259) ________7_____

mystery3(977) _________5____

mystery3(-479) _________-2___

How?

every time mystery3 calls : it return single digit number

example: 259

n / 10 + n % 10 ==> 25 + 9 = 34

34 is greater then 10 , so

n / 10 + n % 10 ==> 3 + 4 = 7

return value is 7

For negitive number

converted in positive number using this statement

-mystery3(-n);

then it just perform the operation and then return the single digit number with negitive symbol added

Example -479:

convert to 479 :

n / 10 + n % 10 ==> 47 + 9 = 56

n / 10 + n % 10 ==> 5 + 6 = 11

n / 10 + n % 10 ==> 1 + 1 = 2

return -2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote