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.

Re: AP computer science question

What do *you* think?

kind regards,

Jos

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)

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

Re: AP computer science question

I need your help please :[ I'm a super noooob

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

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

Re: AP computer science question

Re: AP computer science question

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.