Extending our interest calculator
No. 109
Q: |
Our current Extend the current project by adding a new class
variable CautionDo not forget to change the Javadoc comments accordingly! |
A: |
We introduce a new variable private static double
assetInterestRate = 1.5, // applied to positive balances / assets
debtInterestRate = 15.; // applied to negative balances / debts We need the appropriate getter and setter methods in
/** * @return The debt interest rate value. */ public static double getDebtInterestRate() { return debtInterestRate; } /** * This interest rate will be applied to negative balances. In contrast * {{@link #setInterestRate(double)} will handle positive balance values. * * @param defaultInterestRate * the desired default interest rate value. */ public static void setDebtInterestRate(double debtInterestRate) { Account.debtInterestRate = debtInterestRate; } The computed interest depends on positive or negative balance values:
Using Math.pow() for calculating e.g. rather than using a loop is actually a bad idea: Math.pow() internally using a power series expansion is expensive with respect to execution performance. Due to the integer exponential value a simple loop involving only multiplications offers a better choice. |