Review

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:

4

Review

Dynamic Activation Chain

Here is a review of how recursion works with Triangle().

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

Click on the diagram shows the steps of the computation. Each activation of the method is represented by a circle. Each activation has its own parameter and returns a value to its caller.

An activation may need to wait for a returned value before it can continue with a computation. In the diagram this is represented by a circle on the chain that has not returned a value. The circle is labeled with the statement that is waiting for a result.

The bottom circle represents the base case which immediately returns a value to its caller.

When the base case is reached, values start returning to callers, and activations start completing their work.

When an activation is finished with its work, its circle is removed from the chain.

The final picture (which shows nothing) represents the case where the first activation of Triangle() has completed the computation and returned a value to its caller.

QUESTION 16:

(Review: ) What are the two ways to view recursion?