New Activation

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:

Another Triangle() is activated with a parameter of 3.

New Activation

The statement:

    return N + Triangle( N-1 );

must get a value for Triangle(N-1) before the addition can be done. So the first activation of Triangle() causes a second activation of Triangle(), this time with a parameter of 3. Here is how this is pictured:

two activations

QUESTION 12:

Look at the code again. Think about the latest activation. What does the activation Triangle(3) do?

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