import java.text.NumberFormat; class Loan{ private static final int MONTHS_IN_YEAR = 12; private double amountBorrowed; private double yearlyRate; private int year; public Loan(double P, double R, int Y){ amountBorrowed = P; yearlyRate = R; year = Y; } public double getAmountBorrowed(){ return amountBorrowed; } public double getYearlyRate(){ return yearlyRate; } public int getYear(){ return year; } public double monthlyPayment(){ double monthlyRate = yearlyRate / MONTHS_IN_YEAR / 100; int period = year * MONTHS_IN_YEAR; double numerator = monthlyRate * Math.pow((1 + monthlyRate), period); double denominator = Math.pow((1 + monthlyRate), period) - 1; return amountBorrowed * numerator / denominator; } public double totalPayment(){ return monthlyPayment() * year * MONTHS_IN_YEAR; } public String toString(){ NumberFormat fmt = NumberFormat.getCurrencyInstance(); return fmt.format(amountBorrowed) + " at " + yearlyRate + "% for " + year + " years will cost you " + fmt.format(monthlyPayment()) + " per month"; } }