Activity 6.4.1.
Try running the car loan program below. Change the amount of the loan and the interest rates in main and try it again.
8% 9% 10% 11% Year 2 23,469.81 23,943.82 24,427.39 24,920.71 Year 3 25,424.31 26,198.42 26,996.07 27,817.98 Year 4 27,541.59 28,665.32 29,834.86 31,052.09 Year 5 29,835.19 31,364.50 32,972.17 34,662.19 Year 6 32,319.79 34,317.85 36,439.38 38,692.00 Year 7 35,011.30 37,549.30 40,271.19 43,190.31 Year 8 37,926.96 41,085.02 44,505.94 48,211.60
for
loop that generates the table. Because the table contains seven rows, the outer loop can iterate seven times, through the values 2, 3, \dots 8 :for (int years = 2; years <= 8; years++)
for (int years = 2; years <= 8; years++) {
for (int rate = 8; rate <= 11; rate++) {
} // for
} // for years
carPriceWithLoan
, and the variable we use for the actual price of the car is carPrice
. Then our inner loop body iscarPriceWithLoan = carPrice *
Math.pow(1 + rate/100.0/365.0, years * 365.0);
System.out.print(dollars.format(carPriceWithLoan) +"\t");
Math.pow()
method which returns the first argument to the power of the second argument. It is important here to use 100.0 and not 100 so that the resulting value is a double
and not the int
0.java.text.NumberFormat
class that was described in Chapter 5. The complete program is shown in Figure 6.4.1. Try it in the activecode below.