Static View of Recursion

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Run the program and see!

Static View of Recursion

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

DefinitionTranslation into Java
Triangle( 1 ) = 1
Triangle( N ) = N + Triangle( N-1 )
int Triangle( int N )
{
  if ( N == 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}

In this static view, if the math-like definition is correct, and 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?

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