Source of InterestTable2.java


  1: //InterestTable2.java
  2: 
  3: /**
  4:  * Displays a two-dimensional table showing how
  5:  * interest rates affect bank balances.
  6:  */
  7: public class InterestTable2
  8: {
  9:     public static final int ROWS = 10;
 10:     public static final int COLUMNS = 6;
 11: 
 12:     public static void main(String[] args)
 13:     {
 14:         int[][] table = new int[ROWS][COLUMNS];
 15:         for (int row = 0; row < ROWS; row++)
 16:             for (int column = 0; column < COLUMNS; column++)
 17:                 table[row][column] =
 18:                     getBalance(1000.00, row + 1, (5 + 0.5 * column));
 19: 
 20:         System.out.println("Balances for Various Interest Rates "
 21:             + "Compounded Annually");
 22:         System.out.println("(Rounded to Whole Dollar Amounts)");
 23:         System.out.println();
 24:         System.out.println("Years  5.00%  5.50%  6.00%  6.50%  7.00%  7.50%");
 25: 
 26:         showTable(table);
 27:     }
 28: 
 29:     /**
 30:      * Precondition: The array anArray has ROWS rows and COLUMNS columns.
 31:      * Postcondition: The array contents are displayed with dollar signs.
 32:      */
 33:     public static void showTable
 34:     (
 35:         int[][] anArray
 36:     )
 37:     {
 38:         for (int row = 0; row < ROWS; row++)
 39:         {
 40:             System.out.print((row + 1) + "      ");
 41:             for (int column = 0; column < COLUMNS; column++)
 42:                 System.out.print("$" + anArray[row][column] + "  ");
 43:             System.out.println();
 44:         }
 45:     }
 46: 
 47:     public static int getBalance
 48:     (
 49:         double startBalance,
 50:         int years,
 51:         double rate
 52:     )
 53:     {
 54:         double runningBalance = startBalance;
 55:         for (int count = 1; count <= years; count++)
 56:             runningBalance = runningBalance * (1 + rate / 100);
 57:         return (int)(Math.round(runningBalance));
 58:     }
 59: }