Results 1 to 10 of 10
  1. #1
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: AP computer science question

    What do you think?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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)
    Last edited by svo; 04-06-2012 at 08:16 PM.

  4. #4
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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

  5. #5
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: AP computer science question

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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: AP computer science question

    Evidently continued in a new thread:
    can anyone help me out with this problem?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: AP computer science question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    svo
    svo is offline Member
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default 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.

Similar Threads

  1. computer science newbie
    By sixxvirus in forum New To Java
    Replies: 17
    Last Post: 11-18-2011, 08:30 AM
  2. Basic Java Computation Help Ap Computer Science
    By Sean_J in forum New To Java
    Replies: 6
    Last Post: 02-10-2010, 01:10 PM
  3. Replies: 5
    Last Post: 10-25-2008, 08:42 AM
  4. Software Engineer...Computer Science Major
    By giganews35 in forum Introductions
    Replies: 2
    Last Post: 09-14-2008, 09:19 AM
  5. Replies: 1
    Last Post: 12-11-2007, 04:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •