home *** CD-ROM | disk | FTP | other *** search
/ BURKS 2 / BURKS_AUG97.ISO / BURKS / LANGUAGE / JAVA / NOTES / SOURCE / intrest3.jav < prev    next >
Text File  |  1996-12-20  |  2KB  |  56 lines

  1.  
  2. /* Simulation of console-I/O program Interest3,
  3.    using ConsoleApplet as a basis.  See the file
  4.    ConsoleApplet.java for more information.
  5.    
  6.    David Eck
  7.    eck@hws.edu
  8.    
  9.    August 2, 1996
  10. */
  11.  
  12. public class Interest3Console extends ConsoleApplet {
  13.  
  14.    public void init() {
  15.       title = "Sample program \"Interest3\"";
  16.       super.init();
  17.    }
  18.  
  19.    protected void program() {
  20.       /*
  21.           Program computes and prints the value of investment
  22.           for each of the next five years.  Initial value
  23.           and interest rate are read as input from the 
  24.           user.
  25.       */
  26.  
  27.       double principal;  // the value of the investment
  28.       double rate;       // the annual interest rate
  29.       
  30.       console.put("Enter the initial investment: ");
  31.       principal = console.getlnDouble();
  32.       
  33.       console.put("Enter the annual interest rate: ");
  34.       rate = console.getlnDouble();
  35.       
  36.       int years = 0;  // counts the number of years tha have passed
  37.       
  38.       while (years < 5) {
  39.          double interest = principal * rate;   // compute this year's interest
  40.          principal = principal + interest;     // add it to principal
  41.          years = years + 1;    // count the current year.
  42.          if (years > 1) {
  43.             console.put("The value of the investment after ");
  44.             console.put(years);
  45.             console.put(" years is $");
  46.          }
  47.          else {
  48.             console.put("The value of the investment after 1 year is $");
  49.          }
  50.          console.putln(principal);
  51.       } // end of while loop
  52.    
  53.    }
  54.  
  55. }
  56.