In this lab you'll produce a nicely
formatted table in Java using either the
printf
method of PrintStream
or
the NumberFormat
class.
Download InterestPrint.zip, a ZIP file containing a NetBeans project named
InterestPrint and unZIP this project
into your csci/201 directory.
Try to make your Projects panel look
something like the following picture before continuing.
Build and run the project. All it does is produce a poorly
formatted interest table. Let's take a look at the main
method of InterestPrint
.
public static void main(String[] args) { for (int rate = 0; rate <= 20; ++rate) { System.out.println("Interest rate is " + rate) ; double money = 1000.00 ; for (int year = 0; year <= 10; ++year) { System.out.println("At year " + year + " you have " + money) ; money = money + rate*money*0.01 ; } } }
The program has two nested loops. The outer one iterates through interest rates from 0 to 20, and the inner one iterates through years from 0 to 10.
The output produced by this program is hideous. Your job is to modify the program so that it produces a nicely formatted table like the one shown below:
0 1 2 3 4 5 6 7 8 9 10 0% 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1000.00 1% 1000.00 1010.00 1020.10 1030.30 1040.60 1051.01 1061.52 1072.14 1082.86 1093.69 1104.62 2% 1000.00 1020.00 1040.40 1061.21 1082.43 1104.08 1126.16 1148.69 1171.66 1195.09 1218.99 3% 1000.00 1030.00 1060.90 1092.73 1125.51 1159.27 1194.05 1229.87 1266.77 1304.77 1343.92 4% 1000.00 1040.00 1081.60 1124.86 1169.86 1216.65 1265.32 1315.93 1368.57 1423.31 1480.24 5% 1000.00 1050.00 1102.50 1157.63 1215.51 1276.28 1340.10 1407.10 1477.46 1551.33 1628.89 6% 1000.00 1060.00 1123.60 1191.02 1262.48 1338.23 1418.52 1503.63 1593.85 1689.48 1790.85 7% 1000.00 1070.00 1144.90 1225.04 1310.80 1402.55 1500.73 1605.78 1718.19 1838.46 1967.15 8% 1000.00 1080.00 1166.40 1259.71 1360.49 1469.33 1586.87 1713.82 1850.93 1999.00 2158.92 9% 1000.00 1090.00 1188.10 1295.03 1411.58 1538.62 1677.10 1828.04 1992.56 2171.89 2367.36 10% 1000.00 1100.00 1210.00 1331.00 1464.10 1610.51 1771.56 1948.72 2143.59 2357.95 2593.74 11% 1000.00 1110.00 1232.10 1367.63 1518.07 1685.06 1870.41 2076.16 2304.54 2558.04 2839.42 12% 1000.00 1120.00 1254.40 1404.93 1573.52 1762.34 1973.82 2210.68 2475.96 2773.08 3105.85 13% 1000.00 1130.00 1276.90 1442.90 1630.47 1842.44 2081.95 2352.61 2658.44 3004.04 3394.57 14% 1000.00 1140.00 1299.60 1481.54 1688.96 1925.41 2194.97 2502.27 2852.59 3251.95 3707.22 15% 1000.00 1150.00 1322.50 1520.88 1749.01 2011.36 2313.06 2660.02 3059.02 3517.88 4045.56 16% 1000.00 1160.00 1345.60 1560.90 1810.64 2100.34 2436.40 2826.22 3278.41 3802.96 4411.44 17% 1000.00 1170.00 1368.90 1601.61 1873.89 2192.45 2565.16 3001.24 3511.45 4108.40 4806.83 18% 1000.00 1180.00 1392.40 1643.03 1938.78 2287.76 2699.55 3185.47 3758.86 4435.45 5233.84 19% 1000.00 1190.00 1416.10 1685.16 2005.34 2386.35 2839.76 3379.32 4021.39 4785.45 5694.68 20% 1000.00 1200.00 1440.00 1728.00 2073.60 2488.32 2985.98 3583.18 4299.82 5159.78 6191.74
The easiest way to do this is to use the
printf
method.
Look in your textbook or in the on-line Java documention for more information.
If you want to use more traditional (pre-1.5) Java, you can use the
the NumberFormat
class.
Show the lab instructor your nice output.