Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Java --method question..need confirmation?

The downUp method is defined as follows:

public static int downUp( int t )

{

if ( t == 0 )

return 1;

int n = downUp( t - 1 );

return n + 1;

}

When the statement --downUp( 3 )-- is executed, would the result be 3?

I am a begginner in computer science and I am really paranoid when it comes to figuring out the results of codes. I do not have Java installed on my computer.

Similarly, in this code below, would the result also be 3?

(when the statment executed is prevPlus ( 3) )

public static int prevPlus( int t )

{

if ( t == 0 )

return 0;

return prevPlus( t - 1 ) + 1;

}

Thanks for your help :)

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    The first one returns one more than the number supplied, so downUp(3) would be 4. Look at the code. downUp(0) is clearly 1. downup(1) is downu(0)+1, or 2. And so on.

    The second one returns the number supplied. prevPlus(0) is clearly 0. prevPlus(1)=prevPlus(0)+1 or 1, and so on. So prevPlus(3) is 3.

  • Anonymous
    1 decade ago

    In this case, you should just trace the code, with all the recursive calls.

Still have questions? Get your answers by asking now.