Source of InterestTable.java


  1: //InterestTable.java
  2: 
  3: /**
  4:  * Displays a two-dimensional table showing how 
  5:  * interest rates affect bank balances.
  6:  */
  7: public class InterestTable
  8: {
  9:     public static void main(String[] args)
 10:     {
 11:         int[][] table = new int[10][6];
 12:         for (int row = 0; row < 10; row++)
 13:             for (int column = 0; column < 6; column++)
 14:                 table[row][column] =
 15:                     getBalance(1000.00, row + 1, (5 + 0.5 * column));
 16: 
 17:         System.out.println("Balances for Various Interest Rates "
 18:             + "Compounded Annually");
 19:         System.out.println("(Rounded to Whole Dollar Amounts)");
 20:         System.out.println();
 21:         System.out.println("Years  5.00%  5.50%  6.00%  6.50%  7.00%  7.50%");
 22:         for (int row = 0; row < 10; row++)
 23:         {
 24:             System.out.print((row + 1) + "      ");
 25:             for (int column = 0; column < 6; column++)
 26:                 System.out.print("$" + table[row][column] + "  ");
 27:             System.out.println();
 28:         }
 29:     }
 30: 
 31:     /**
 32:      * Returns the balance in an account after a given number of years 
 33:      * and interest rate with an initial balance of startBalance.
 34:      * Interest is compounded annually. The balance is rounded
 35:      * to a whole number.
 36:      */
 37:     public static int getBalance
 38:     (
 39:         double startBalance,
 40:         int years,
 41:         double rate
 42:     )
 43:     {
 44:         double runningBalance = startBalance;
 45:         for (int count = 1; count <= years; count++)
 46:             runningBalance = runningBalance * (1 + rate / 100);
 47:         return (int)(Math.round(runningBalance));
 48:     }
 49: }