Source of InterestTable.java


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