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

Consider the following method: public int mystery(int a, int b) { if (b==1) retu

ID: 3918229 • Letter: C

Question

Consider the following method:

public int mystery(int a, int b)

{ if (b==1)    return a; else

   return a + mystery(a,b-1);

}

Draw the decomposition & composition of mystery(6,3)

What would be the value of mystery(6,2), mystery(6,1)? (No need to draw)

After having a look at the results in parts a and b, what do you think this method do?

Explanation / Answer

public int mystery(int a, int b) { if (b==1) return a; else return a + mystery(a,b-1); } mystery(6,3) => 6 + mystery(6,2) => 6 + 6 + mystery(6,1) => 6 + 6 + 6 => 18 mystery(6,2) is 12 mystery(6,1) is 6 this method calculates the product of two input parameters a and b. so, the method calculates a * b