1

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.

parsecer
  • 4,758
  • 13
  • 71
  • 140
  • 2
    `MILES_PER_YEAR / carMileage` is integer division. Maybe you are losing a bit of precision there. – 001 Nov 19 '19 at 04:00
  • @JohnnyMopp Yes, it worked, thank you. I was looking at code after `/` thinking it should make integer division a non-issue since it's double. I changed it to `double totalGasCost = ((double) MILES_PER_YEAR / carMileage)* COST_PER_GALLON * 5;`. If you post an answer, I'll accept it. – parsecer Nov 19 '19 at 04:10
  • 1
    Since the division is in parentheses, it gets evaluated first. Even without the parens, it would be evaluated first since mult and div have the same precedence and left to right associativity. – 001 Nov 19 '19 at 04:13
  • tip: Use `BigDecimal` for monetary calculations. It is resource heavy but more precise :) – bananas Nov 19 '19 at 04:34

1 Answers1

2

Variable carMileage should be double. Because when you perform division you lose floating part.

Change variable to double type

private double carMileage;

Or cast the division result to double

double totalGasCost = ((double) MILES_PER_YEAR / carMileage)* COST_PER_GALLON * 5;

Maxim Kasyanov
  • 938
  • 5
  • 14
  • nah man. Sometimes homeworks require for some variables to be a certain type, nothing to be done about that ; ( – parsecer Nov 19 '19 at 04:11
  • @parsecer you perform integer division. After division, the result will autocast to int type – Maxim Kasyanov Nov 19 '19 at 04:14
  • Yeah, I get it. It's just a solution I posted ^ in the comments suits my needs more. If Johnny doesn't post his answer and you update yours, I'll accept yours. I wonder how many manhours the integer division problem wasted since Java was created; ( – parsecer Nov 19 '19 at 04:18