Reputation: 43
Alright so I have this class:
package com.sandrovictoriaarena.unittesting;
public class LoanCalculator {
private double pricipelValue; //PV
private double interestRate; //rate
private int loanLength; //n
private double result;
public LoanCalculator(double pricipelValue, double interestRate,
int loanLength) {
super();
this.pricipelValue = pricipelValue;
this.interestRate = interestRate / 100;
this.loanLength = loanLength;
}
public double getPricipelValue() {
return pricipelValue;
}
public void setPricipelValue(double pricipelValue) {
this.pricipelValue = pricipelValue;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public int getLoanLength() {
return loanLength;
}
public void setLoanLength(int loanLength) {
this.loanLength = loanLength;
}
@Override
public String toString() {
return "LoanCalculator [pricipelValue=" +
pricipelValue + ", interestRate=" + interestRate +
", loanLength=" + loanLength + "]";
}
public double calculateMonthlyPayment() throws IllegalArgumentException{
result = (1 - (Math.pow((1 + interestRate), -1 * loanLength)));
result = interestRate / result;
result = result * pricipelValue;
return result;
}
}
And i'm making an object with the following values: new LoanCalculator (100.0,20.0,6);
The result when I run calculateMonthlyPayment() should be 17.65 but I keep getting 30.07. What am I doing wrong?
Upvotes: 0
Views: 84
Reputation: 35405
The code and the formula both are correct.
You are passing the interest rate as 20%, but it is probably 20% per annum. I think your interval is 6, which is probably six months. You should always pass the interest rate in the same terms as the number of intervals. Here, your number of intervals is in months, but your interest rate is in years (per annum). So just pass the interest rate as monthly rate, and you should get the right answer!
The monthly interest rate is 20%/12 = (0.2/12). If you substitute that in your input, you will get the right answer. So you should be doing: new LoanCalculator (100.0,20.0/12,6)
Upvotes: 3