public class InterestTable
1:
2: /**
3: Displays a two-dimensional table showing how interest
4: rates affect bank balances.
5: */
6: public class InterestTable
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: for (row = 0; row < 10; row++)
22: {
23: System.out.print((row + 1) + " ");
24: for (column = 0; column < 6; column++)
25: System.out.print("$" + table[row][column] + " ");
26: System.out.println( );
27: }
28: }
29:
30: /**
31: Returns the balance in an account that starts with startBalance
32: and is left for the indicated number of years with rate as the
33: interest rate. Interest is compounded annually. The balance is
34: rounded to a whole number.
35: */
36: public static int balance(double startBalance, int years, double rate)
37: {
38: double runningBalance = startBalance;
39: int count;
40: for (count = 1; count <= years; count++)
41: runningBalance = runningBalance*(1 + rate/100);
42: return (int) (Math.round(runningBalance));
43: }
44: }