go to previous page   go to home page   go to next page hear noise highlighting

Answer:

Run the program and see!


Translating from Math to Java

DefinitionTranslation into Java
Triangle( 1 ) = 1

Triangle( N ) = N + Triangle( N-1 )
public int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}

Writing a recursive Java program can be viewed as translating a math-like definition into Java code. The symbols of the math-like definition are rearranged and some extra syntax is added to create Java code.

If the math-like definition is correct, and if you correctly translate it into Java, then the program is correct. You don't have to think about how Java does the calculation. Just check that you have correctly translated the definition.


QUESTION 10:

Is the following a correct translation of the math-like definition?

public int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N ) - 1;
}

go to previous page   go to home page   go to next page