/* * MortgageCalculator.java * */ public class MortgageCalculator { public static int defaultYears = 30; public static float defaultLoanAmount = 200000; public static float defaultInterestRate = (float)0.0575; private int Years; private float LoanAmount; private float InterestRate; private float MonthlyPayment; /** Creates a new instance of MortgageCalculator */ public MortgageCalculator(int years, float LA, float IR){ Years = years; LoanAmount = LA; InterestRate = IR; } public int GetYears(){ return Years; } public void SetYears(int Years){ this.Years = Years; } public float GetLoanAmount(){ return LoanAmount; } public void SetLoanAmount(float LoanAmount){ this.LoanAmount = LoanAmount; } public float GetInterestRate(){ return InterestRate; } public void SetInterestRate(float InterestRate){ this.InterestRate = InterestRate; } public float GetMonthlyPayment(){ return MonthlyPayment; } // using the equation to calculate the monthly payment public void CalcMonthlyPayment() { float temp = (float)Math.pow((InterestRate + 1), (- 12 * Years)); MonthlyPayment = LoanAmount * (InterestRate / (1 - temp)) ; } //To convert a float value into a string, public String ToString (float value){ return Float.toString(value); } //display the monthly payment, since the actually display is related to the UI //design which isn’t required here, so this function only convert the //MonthlyPayment from numeric format to a string. public String DisplayMonthlyPayment(){ return ToString(MonthlyPayment); } }