# AP computer science question

• 04-06-2012, 09:04 PM
svo
AP computer science question
public int multiply( int n )

{

if (n == 0)

return 1;

else if (n % 2 ==1)

return n;

else

return n * multiply( n - 2);

}

b) What do you notice about the bolded text in regards to programming? What is this programming concept called? Explain this concept.
• 04-06-2012, 09:07 PM
JosAH
Re: AP computer science question
What do you think?

kind regards,

Jos
• 04-06-2012, 09:10 PM
svo
Re: AP computer science question
Ahh well I tihnk that the second bolded 'multiply' is calling on the first 'multiply' method. But I don't know what the concept is at all. Is it something like a constructo

*edit* OHHH Is it method overloading?!?! (i'm looking around in my AP barrons book... my new best friend)
• 04-06-2012, 09:12 PM
svo
Re: AP computer science question
part a) was that if you call the method multiply(8) then what is the output? I put that it was 8 and here is my reasoning:
1a)
-You plug in 8 as the ‘int n’
-if ‘n’ equals zero, it will return a value of one, however n is not zero it is eight so we move on
-now if 8 modulus 2 is equal to one, meaning that ‘n’ is an odd integer, it returns the value of ‘n’ but it is not as 8 is an even number… so…
-finally you do 8 * multiply( 8-2)
- multiply( 8-2)
(go through the same process for that)
-multiply(6) --> not equal to zero and is an even number so it will end up like this
- return 8 * multiply(6-2)
-multiply(4) --> not zero and is not odd so we can see that this pattern will eventually lead to: return 8 * multiply(o)
-multiply(0) returns a value of 1 so 8*1 is 8
• 04-06-2012, 09:27 PM
svo
Re: AP computer science question
• 04-06-2012, 09:48 PM
JosAH
Re: AP computer science question
It's not a constructor because constructors don't have a return type and this method has one (int); in the body of the method it calls itself; it is named 'recursion'. Google is your friend here.

kind regards,

Jos
• 04-06-2012, 10:02 PM
svo
Re: AP computer science question
Ohhhh Myyyy Gosshhhhh!!! I seee!! Yeah "Recursion"... Chapter 7 in the Barrons Book. Okay! thanks I will look into it
• 04-08-2012, 12:52 PM
DarrylBurke
Re: AP computer science question
Evidently continued in a new thread:
http://www.java-forums.org/new-java/...t-problem.html

db
• 04-08-2012, 12:52 PM
DarrylBurke
Re: AP computer science question
• 04-08-2012, 07:29 PM
svo
Re: AP computer science question
[SOLVED] a long time ago. It's not my fault someone else posted the same thing. But this thread is dead.