Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Quick Code analysis please

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Quick Code analysis please

    Can someone please give me a quick code analysis of what is outputted when the method f1(5) is called?
     public in f1( int x )
     
     {
     
     if ( x == 0 )
     
     return 0;
     
     else
     
     return f2( x - 2 );
     
     }
     
     public in f2( int x )
     
     {
     
     if ( x == 1 )
     
     return 1;
     
     else 
     
     return f1( x + 1 ) + x;
     
    }
    Last edited by svo; April 7th, 2012 at 12:33 AM. Reason: please use [code] tags


  2. #2
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    I got this far:
    - Return f2(3) is called
    - then in the 3 is passed into the f2(int x) right?
    - then it's supposed to do the f1( x + 1) + x .... i'm confused about the " + x" ... how does it work?

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Quick Code analysis please

    What happens when the code is compiled and executed?
    If you want to see the values of intermediate results, add println statements to print them.
    If you don't understand my answer, don't ignore it, ask a question.

  4. The Following User Says Thank You to Norm For This Useful Post:

    svo (April 6th, 2012)

  5. #4
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    D: I'm a nooobb.... soo... and well, I'm supposed to be able to do this in my head... So I was wondering if I could get some help seeing the logic behind what is happening in the code..

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    I think I sort of see the logic and kinda-sorta understand it up until the last line of "return f1( x + 1 ) + x;" ... :[

  7. #6
    Junior Member lunix's Avatar
    Join Date
    Apr 2012
    Location
    England
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Quick Code analysis please

    postfix/prefix i think(the order you do the math), if your wanting it to add the x either put it in the brackets that exist or put another set of brackets around the +1. my thinking is that its just returning the value of x along with the result from the function call, as the function take only one argument, unless its in brackets it concatenating(adding) the results together; i.e when all is said and done, we will probably end up with 11, as in x = 1(result from all the function calls), x =1 put together....

    5 enters f2 as 3, goes back to 4 and becomes 2 goes up to 3 and finally finishes at 1, essentially taking 2 originally and then going down one at a time.
    Feels good man

  8. The Following User Says Thank You to lunix For This Useful Post:

    svo (April 6th, 2012)

  9. #7
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    Oh so I followed the logic in your last sentence and it seems to make sense except i couldn't help but notice that when you went through the "return f1( x + 1 ) + x;", you ignored the "+ x" ? is that allowed? was that on purpose?

  10. #8
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Quick Code analysis please

    No, it cannot be ignored.

    f1(x+1) returns some number. Simply substitute that result, perform the +x, and return the result.

    Try stepping through the code in debug mode or adding print statements to your code to see how the result as each line of code gets executed.

  11. The Following User Says Thank You to helloworld922 For This Useful Post:

    svo (April 7th, 2012)

  12. #9
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    Ahhh THanks! That makes WAY more sense. I don't know... The way you bolded them but explained them... It made so much sense. No, I'm not actually writing this code, it's a comprehension question on the AP computer science exam so I must be able to do it in my head or on paper. Wait, oh adding print statements would actually be a great idea. Thanks again!

  13. #10
    Junior Member
    Join Date
    Apr 2012
    Posts
    9
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Quick Code analysis please

    [SOLVED]
    Thanks everyone!

Similar Threads

  1. Big O - Algorithm Analysis
    By PeskyToaster in forum Java Theory & Questions
    Replies: 4
    Last Post: March 15th, 2012, 11:48 PM
  2. Reading Big File analysis
    By tcstcs in forum Java Theory & Questions
    Replies: 1
    Last Post: March 9th, 2012, 07:35 AM
  3. Sales Analysis for various products
    By faridzul90 in forum Java Theory & Questions
    Replies: 3
    Last Post: September 22nd, 2011, 08:36 AM
  4. An online source offers project analysis
    By talha06 in forum The Cafe
    Replies: 0
    Last Post: June 20th, 2010, 12:49 PM
  5. Just Need Some quick help with code
    By mulkman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 14th, 2010, 11:11 AM