Source of InterestTable3.java


  1: //InterestTable3.java
  2: 
  3: /**
  4:  * Displays a two-dimensional table showing how interest
  5:  * rates affect bank balances.
  6:  */
  7: public class InterestTable3
  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:         System.out.println("Balances for Various Interest Rates");
 20:         System.out.println("Compounded Annually");
 21:         System.out.println("(Rounded to Whole Dollar Amounts)");
 22:         System.out.println();
 23:         System.out.println("Years  5.00%  5.50%  6.00%  6.50%  7.00%  7.50%");
 24:         showTable(table);
 25:     }
 26: 
 27:     /**
 28:          * The array anArray can have any values for its two dimensions.
 29:          * Postcondition: The array contents are displayed with dollar signs.
 30:          */
 31:     public static void showTable
 32:     (
 33:         int[][] anArray
 34:     )
 35:     {
 36:         for (int row = 0; row < anArray.length; row++)
 37:         {
 38:             System.out.print((row + 1) + "      ");
 39:             for (int column = 0; column < anArray[row].length; column++)
 40:                 System.out.print("$" + anArray[row][column] + "  ");
 41:             System.out.println();
 42:         }
 43:     }
 44: 
 45:     public static int getBalance
 46:     (
 47:         double startBalance,
 48:         int years,
 49:         double rate
 50:     )
 51:     {
 52:         double runningBalance = startBalance;
 53:         for (int count = 1; count <= years; count++)
 54:             runningBalance = runningBalance * (1 + rate / 100);
 55:         return (int)(Math.round(runningBalance));
 56:     }
 57: }