import java.util.Scanner; /* Car Loan Estimator (version A) ** ** Java application that prints a table showing the monthly payments for ** car loan with annual interest rates ranging from 1% to 10% and terms ** ranging from 12 months to 72 months. ** This version improves upon the previous one (lacking the 'A' in its ** name) in the following ways: ** ** --The principal of the loan is read from the keyboard rather than ** being "hard-coded". ** --the methods are parameterized (to some degree) so that the only ** global variable used is that holding the principal of the loan. ** (Global constants are still used for indicating the range of ** terms and range of interest rates to be considered.) ** ** CMPS 134 Fall 2025 Prog. Assg. #2 ** Authors: P.M.J. and R.W.M. ** */ public class CarLoanEstimatorA { // GLOBAL CONSTANTS static final int TERM_START = 12; //Shortest term in months static final int TERM_LIMIT = 72; //Longest term in months static final int TERM_INCREMENT = 12; //difference between terms static final double ANNUAL_INTEREST_RATE_START = 1.0; //Lowest Annual Interest Rate static final double ANNUAL_INTEREST_RATE_LIMIT = 10.0; //Hightest Annual Interest Rate // GLOBAL VARIABLE static double principal; //Principal to be borrowed public static void main( String [] args ) { System.out.println("Welcome to the Car Loan Estimator Program..."); Scanner keyboard = new Scanner(System.in); System.out.print("Enter the principal of the loan: "); principal = keyboard.nextDouble(); System.out.printf("Monthly payments for a $%8.2f loan%n", principal); printHeading(); printTable(); System.out.println("Done!!"); } public static void printHeading() { String underline = "---||"; System.out.print(" || "); for (double annualInterestRate = ANNUAL_INTEREST_RATE_START; annualInterestRate <= ANNUAL_INTEREST_RATE_LIMIT; annualInterestRate = annualInterestRate + 1.0) { System.out.printf("%6.2f %% | ", annualInterestRate); underline = underline + "-----------|"; } System.out.println(); System.out.println(underline); } /* Prints the body of the table. */ public static void printTable() { for(int term = TERM_START; term <= TERM_LIMIT; term = term + TERM_INCREMENT) { printRow(term); } } /* Prints one row of the table, specifically the one showing the ** monthly payment due for a loan having the specified term. */ public static void printRow(int term) { System.out.printf("%2d || ",term); for(double annualInterestRate = ANNUAL_INTEREST_RATE_START; annualInterestRate <= ANNUAL_INTEREST_RATE_LIMIT; annualInterestRate = annualInterestRate + 1.0) { double payment = calculatePayment(principal, term, annualInterestRate); System.out.printf("%8.2f | ",payment); } System.out.println(); } /* Given the principal, term (in months), and annual interest rate of a loan ** (in percent, as in 5.6%), returns the monthly payment that would need to be ** made in order to complete repayment by the end of the term. ** An explanation of the formula used can be found at: ** https://www.youtube.com/watch?v=p8f8XP2tmvE */ public static double calculatePayment(double principal, int term, double intRate) { double monthlyInterestRate = (intRate / 100) / 12; double numerator = principal * monthlyInterestRate; double denominator = 1 - Math.pow(1 + monthlyInterestRate, -term); return numerator / denominator; } }