Source of InterestTable2.java


  1: 
  2: /**
  3:  Displays a two-dimensional table showing how 
  4:  interest rates affect bank balances.
  5: */
  6: public class InterestTable2
  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:                            
 19:                 System.out.println("Balances for Various Interest Rates " +
 20:                            "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: 
 25:                 showTable(table);
 26:         }
 27: 
 28:     /**
 29:      Precondition: The array anArray has ROWS rows and COLUMNS columns.
 30:      Postcondition: The array contents are displayed with dollar signs.
 31:     */
 32:     public static void showTable(int[][] anArray)
 33:     {
 34:       for (int row = 0; row < ROWS; row++)
 35:       {
 36:          System.out.print((row + 1) + "      ");
 37:          for (int column = 0; column < COLUMNS; column++)
 38:              System.out.print("$" + anArray[row][column] + "  ");
 39:          System.out.println( );
 40:       }
 41:     }
 42: 
 43:         public static int getBalance(double startBalance, int years, double rate)
 44:         {
 45:                 double runningBalance = startBalance;
 46:                 for (int count = 1; count <= years; count++)
 47:                         runningBalance = runningBalance * (1 + rate / 100);
 48:                  
 49:                 return (int)(Math.round(runningBalance));
 50:         }
 51: }