Source of InterestTable2.java


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