Gradually Increasing the Interest Rate

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   go to next page

In the above dialog, the user keeps trying interest rates until one is found where the account reaches the million dollar goal. In terms of computer programming, what is the user doing?

Answer:

The user is acting like a result-controlled loop.

Gradually Increasing the Interest Rate

The user is acting like a result-controlled loop. We can write the program do this. Here is an outline of a program that starts the interest rate out at zero percent, then increases it in tenth percent increments until the million dollar goal is reached:

class  MillionDollarInterestRate
{

  public static void main( String[] args ) 
  {
    double initialAmount = 1000.00 ;
    double dollars = 0.0;
    double rate = -0.001; // This is about to change
 
    while ( dollars < 1000000 )
    {
       // change to the next rate
       rate = rate + 0.001;

       // compute the dollars after 40 years at the current rate

       . . . . . .

    }

    System.out.println("After 40 years at " + rate*100 
      + " percent interest you will have " + dollars + " dollars" );
  }

}

This is a result controlled loop, because the loop stops only when the interest rate is found that gives us the result we want. The number of times the loop iterates is not explicit in the program.

QUESTION 9:

The skeleton program does not have code for the part that says "compute the dollars after 40 years at the current rate." How do you expect that this will be done? (Hint: look at the previous version of the program.)