Source of InterestTable3.java


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