0

さて、私はこのクラスを持っています:

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;
}

}

そして、次の値を持つオブジェクトを作成しています: new LoanCalculator (100.0,20.0,6);

calculateMonthlyPayment() を実行すると、結果は 17.65 になるはずですが、30.07 になり続けます。私は何を間違っていますか?

4

1 に答える 1

3

コードと式はどちらも正しいです。

あなたは金利を 20% として渡していますが、おそらく年率 20% です。あなたの間隔は 6 だと思います。おそらく 6 か月です。利率は、常に間隔の数と同じ条件で渡す必要があります。ここでは、間隔の数は月単位ですが、金利は年単位 (年) です。したがって、金利を月利として渡すだけで、正しい答えが得られるはずです。

月利は 20%/12 = (0.2/12) です。入力でそれを置き換えると、正しい答えが得られます。したがって、次のことを行う必要があります。new LoanCalculator (100.0,20.0/12,6)

于 2013-09-21T18:35:24.747 に答える