Source of Interest.java


  1: // Fig. 5.6: Interest.java
  2: // Compound-interest calculations with for.
  3: 
  4: public class Interest 
  5: {
  6:    public static void main( String args[] )
  7:    {
  8:       double amount; // amount on deposit at end of each year
  9:       double principal = 1000.0; // initial amount before interest
 10:       double rate = 0.05; // interest rate
 11: 
 12:       // display headers
 13:       System.out.printf( "%s%20s\n", "Year", "Amount on deposit" );
 14: 
 15:       // calculate amount on deposit for each of ten years
 16:       for ( int year = 1; year <= 10; year++ ) 
 17:       {
 18:          // calculate new amount for specified year
 19:          amount = principal * Math.pow( 1.0 + rate, year );
 20: 
 21:          // display the year and the amount
 22:          System.out.printf( "%4d%,20.2f\n", year, amount );
 23:       } // end for
 24:    } // end main
 25: } // end class Interest
 26: 
 27: 
 28: /**************************************************************************
 29:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 30:  * Pearson Education, Inc. All Rights Reserved.                           *
 31:  *                                                                        *
 32:  * DISCLAIMER: The authors and publisher of this book have used their     *
 33:  * best efforts in preparing the book. These efforts include the          *
 34:  * development, research, and testing of the theories and programs        *
 35:  * to determine their effectiveness. The authors and publisher make       *
 36:  * no warranty of any kind, expressed or implied, with regard to these    *
 37:  * programs or to the documentation contained in these books. The authors *
 38:  * and publisher shall not be liable in any event for incidental or       *
 39:  * consequential damages in connection with, or arising out of, the       *
 40:  * furnishing, performance, or use of these programs.                     *
 41:  *************************************************************************/