AP computer science questions
public int multiply( int n )
{
if (n == 0)
return 1;
else if (n % 2 ==1)
return n;
else
return n * multiply( n - 2);
}
a) If the method multiply(8) is called, what is the output? I got 8 as the output but I'm not sure... It's probably either 8 or 0...
b)What do you notice about the bolded text in regards to programming? What is this programming concept called? Explain this concept. Back up your explaination by writing out each step in the code.
-I know that the bolded terms are the same method. The concept is 'Recursion'. Recursion is when a method calls upon itself. But what does it mean when it asks "Back up your explaination by writing out each step in the code."??
Thanks in advance for your help!
Re: AP computer science questions
Quote:
But what does it mean when it asks "Back up your explaination by writing out each step in the code."??
I think it means go through the method as if you were the program. So, what happens when multiply(8) is called? 8 is not equal to zero, and the remainder of 8 / 2 is not 1, either, so the program will go to the last option: 8 * multiply(8-2). What happens after that? You can go through the same thought process as I showed for 8 above and continue expanding the answer until you get a definite result.
Doing that should not only answer part b, but show you the answer to part a too. :)