/* Car Loan Estimator ** ** 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. ** ** CMPS 134 Fall 2025 Prog. Assg. #2 ** Authors: P.M.J. and R.W.M. */ public class CarLoanEstimator { // CONSTANTS static final int TERM_START = 12; //Shortest term in months static final int TERM_LIMIT = 72; //Longest term in months 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 VARIABLES static double principal = 25000.00; //Principal to be borrowed static double annualInterestRate; //Annual Interest Rate static int term; //term (in months) static double payment; //Calculated monthly payment public static void main( String [] args ) { 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 (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); } public static void printTable() { for(term = TERM_START; term <= TERM_LIMIT; term = term + 12) { printRow(); } } public static void printRow() { System.out.printf("%2d || ",term); for(annualInterestRate = ANNUAL_INTEREST_RATE_START; annualInterestRate <= ANNUAL_INTEREST_RATE_LIMIT; annualInterestRate = annualInterestRate + 1.0) { calculatePayment(); System.out.printf("%8.2f | ", payment); } System.out.println(); } /* Uses the global variables 'principal', 'annualInterestRate', and 'term' ** to determine a value for payment. An explanation of the formula used ** may be viewed at: https://www.youtube.com/watch?v=p8f8XP2tmvE */ public static void calculatePayment() { double monthlyInterestRate = (annualInterestRate / 100) / 12; double numerator = principal * monthlyInterestRate; double denominator = 1 - Math.pow((1 + monthlyInterestRate),(0.0-term)); payment = numerator / denominator; } }