/* * File: CarLoan.java * Author: Java, Java, Java * Description: This application prints a table showing the total * price paid for a $20,000 car if financing at various interest * rates between 8 and 100 percent is assumed for a period of * 2 through 8 years. * * The algorithm uses a nested for-loop to print the rows and columns * of the table. Note the use of the java.text.NumberFormat class * in the formatting of the numeric output. */ import java.text.NumberFormat; // For formatting $nn.dd or n% public class CarLoan { public static void main(String args[]) { double carPrice = 20000; // Car's actual price double carPriceWithLoan; // Total cost of the car plus financing NumberFormat dollars = NumberFormat.getCurrencyInstance(); // Number formatting NumberFormat percent = NumberFormat.getPercentInstance(); percent.setMaximumFractionDigits(2); // Print the table for (int rate = 8; rate <= 11; rate++) // Print the column heading System.out.print("\t" + percent.format(rate/100.0) + "\t" ); System.out.println(); for (int years = 2; years <= 8; years++) { // For years 2 through 8 System.out.print("Year " + years + "\t"); // Print row heading for (int rate = 8; rate <= 11; rate++) { // Calc and print CD value carPriceWithLoan = carPrice * Math.pow(1 + rate / 100.0 / 365.0, years * 365.0); System.out.print( dollars.format(carPriceWithLoan) + "\t"); } // for rate System.out.println(); // Start a new row } // for years } // main() } // CarLoan