Debugging Practice

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

Answer:

Seems to work, but seems a little askward.

Debugging Practice

The way the variable rate is initialized and then immediately incremented is awkward. Here is a possibly buggy program that does this in a different way:

class  MillionDollarBuggy
{

  public static void main( String[] args ) 
  {
    double initialAmount = 1000.00 ;
    double dollars = 0.0;
    double rate;
    int    year;

    rate = 0.0;   // Start interest rate at zero
 
    while ( dollars < 1000000 )
    {

       // compute the dollars after 40 years at the current rate
       year =  1 ;     
       dollars = initialAmount;     
       while (  year <= 40 )
       {     
         dollars = dollars + dollars*rate  ; // add another year's interest     
         dollars = dollars + 1000 ;          // add in this year's contribution
         year    =  year + 1 ;
       }

       // change to the next rate
       rate = rate + 0.001;

    }

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

}

QUESTION 13:

Is there a bug in this program?