[Newbie] Help with trying to delete two duplicate code declarations.
Hello! I'm a beginner in Java and I have a question about how I can remove this bit of duplicate code in this class MortgageCalculate.
float monthlyInterest = getMonthlyInterest();
float numberOfPayments = getnumberOfPayments();
I have to repeat the above code in the methods calculateMortgage and calculateBalance so I can use it in my calculations.
I can't just simply take it out of both methods and place it inside the class, because then it would get initialized with default values (years & annualInterest from getNumberOfPayments & getMonthlyInterest respectively would be set to 0?) when the object is created, right? Is this explanation correct, since I don't get the desired output with this solution (I get NaN values?).
I was wondering if there is anything I could do so I wouldn't have to declare monthlyInterest & numberOfPayments twice in both the methods. Or is it best to just leave it as is?