I have a class like this:
public class Test {
public static void main(String[] args) {
CarPurchaseV2 car = new CarPurchaseV2(23, "Model", 25000, 24, 3.9);
System.out.println(car.computeFiveYearCost(car.computeMonthlyPayment()));
}
}
class CarPurchaseV2 {
private int carMileage;
private String carMakeModel;
private double purchasePrice;
private int loanMonths;
private double interestRate;
public double computeMonthlyPayment() {
double monthlyRate = (interestRate/100)/12;
double factor = Math.exp(loanMonths * Math.log(1 + monthlyRate));
return (factor * monthlyRate * purchasePrice) / (factor - 1);
}
public double computeFiveYearCost(double monthlyPayment) {
int MILES_PER_YEAR = 12000;
double COST_PER_GALLON = 2.75;
double totalLoanCost = monthlyPayment * loanMonths;
double totalGasCost = (MILES_PER_YEAR / carMileage) * COST_PER_GALLON * 5;
return totalLoanCost + totalGasCost;
}
public CarPurchaseV2(int carMileage, String carMakeModel,
double purchasePrice, int loanMonths, double interestRate) {
this.carMileage = carMileage;
this.carMakeModel = carMakeModel;
this.purchasePrice = purchasePrice;
this.loanMonths = loanMonths;
this.interestRate = interestRate;
}
}
When I run it for carMileage = 23
, purchasePrice = 25000
, loanMonths = 24
and interestRate = 3.9%
, I get $33192.01
, while I need to get (textbook answer) $33202.17
. I don't understand what's wrong with this code. When I run debugger, monthlyPayment = 1084.5106749708948
, totalLoanCost = 26028.256199301475
.
EDIT: Edit for code to be MRE.