Two Parts to 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.
go to previous page   go to home page   hear noise   go to next page

Answer:

The problem has been solved when you are at the mall entrance.

Two Parts to Recursion

Of course you know how to cross a parking lot. You would probably describe the process as "...keep walking until you reach the entrance."

Here is the solution in more detail:

  1. If you are one step from the mall, take that step and you are done.
  2. If you are further than one step from the mall, divide the distance into two parts:
    • a single step, and
    • the remaining distance.
    Now take a step and then cross the remaining distance.

I've described the solution in a recursive style. There are two parts to recursion:

  1. If the problem is easy, solve it immediately.
  2. If the problem can't be solved immediately, divide it into easier problems, then:
    • Solve the easier problems.

The over-all strategy is to find some easily applied action that breaks the problem into smaller pieces. Some of the pieces can be dealt with immediately; others may need to be further broken up.

QUESTION 4:

Say that you have a small rock that you wish to destroy. How can you do this?