Two Views 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:

  • The static view, where a math-like definition is rewritten in Java.
  • The dynamic view, where you think about activations and their parameters.

Two Views of Recursion

You need to be able to use both views. Sometimes when you are working on a problem you need to switch between these two views. You might write the code using the static viewpoint, then test and debug the code using the dynamic viewpoint.

Let us practice this. Here is the math-like definition of Triangle:

  1. Triangle( 1 ) = 1
  2. Triangle( N ) = N + Triangle( N-1 )

And here is another version of the Java method Triangle():

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

Say that you try to run this program..... but it never prints out a result.

QUESTION 17:

From a static viewpoint, what is wrong?

From a dynamic viewpoint, what is wrong?