# Mortgage Calculator Variable

Hey there!

I was doing the Mortgage Calculator as in the java courses with Mosh are explained. However, I didn’t quite get one part. Where’s the numberOfPaymentsMade variable specified? I’ve been trying to figure that out but I actually can’t. Any ideas to specify a value to that variable?

Edit: I just checked by debugging… the numerOfPaymentsMade actually gives the correct value. Now, I wanna know why is that? And just to be sure, does it have any issues or flaws?

package me.oriol;

import java.text.NumberFormat;
import java.util.Scanner;

public class Main {
final static byte MONTHS_IN_YEAR = 12;
final static byte PERCENT = 100;

public static void main(String[] args) {

int principal = (int) readNumber("Principal", 1000, 1_000_000);
float annualInterest = (float) readNumber("Annual Interest Rate", 0.1F, 30);
byte years = (byte) readNumber("Period (Years)", 1, 30);

printMortgage(principal, annualInterest, years);
printPaymentSchedule(principal, annualInterest, years);
}

public static void printMortgage(int principal, float annualInterest, byte years) {
double mortgage = calculateMortgage(principal, annualInterest, years);

String mortgageCurrency = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.println("\nMORTGAGE" + "\n--------" + "\nMonthly Payments: " + mortgageCurrency + "\n");
}

public static void printPaymentSchedule(int principal, float annualInterest, byte years) {
System.out.println("PAYMENT SCHEDULE" + "\n----------------");
for (short month = 1; month <= years * MONTHS_IN_YEAR; month++) {
double balance = calculateBalance(principal, annualInterest, years, month);
System.out.println(NumberFormat.getCurrencyInstance().format(balance));
}
}

public static double readNumber(String prompt, float min, int max) {
Scanner scanner = new Scanner(System.in);
float value;
while (true) {
System.out.print(prompt + ": ");
value = scanner.nextFloat();
if (value >= min && value <= max)
break;
System.out.println("Enter a number between " + (short) min + " and " + max + ".");
}
return value;
}

public static double calculateBalance(int principal, float annualInterest, byte years, short numberOfPaymentsMade) {
float monthlyInterest = annualInterest / MONTHS_IN_YEAR / PERCENT;
short numberOfPayments = (short)(years * MONTHS_IN_YEAR);

return principal
* (Math.pow(1 + monthlyInterest, numberOfPayments) - Math.pow(1 + monthlyInterest, numberOfPaymentsMade))
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1);
}

public static double calculateMortgage (int principal, float annualInterest, byte years) {
float monthlyInterest = annualInterest / MONTHS_IN_YEAR / PERCENT;
short numberOfPayments = (short)(years * MONTHS_IN_YEAR);

return principal
* (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments)
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
}

}

Assuming your code matches Mosh’s, it looks like he made it the fourth parameter of the calculateBalance method. Then he passes that parameter inside printPaymentSchedule while he loops through all of the months until the balance is paid off. Another option would have been to name that variable elapsedMonths or currentMonth something else like that to improve the clarity.

1 Like

Okay, but where does the code pass the numberOfPaymentsMade paramenter to the printPaymantSchedule method? By just adding there the calculateBalance method? And also, I still dont understand where u assign the numberOfPaymentsMade parameter… its assigned to 1, but how does it increase at the same time as the month variable?

Let’s take a look at this printPaymentSchedule method since it seems to be the crux of your issue.

First it prints a header message. Then iterate from 1 to years * 12 (as in, the number of months in total) where month is the iteration variable that is assigned to that value. Then call our calculateBalance method passing principal for the principal parameter, annualInterest for the annualInterest parameter, years for the years parameter, and month for the numberOfPaymentsMade parameter. Lastly, it prints out the formatted result from the call to calculateBalance.

The body of the loop is called once for every iteration (so once for every month). On the first iteration month is 1, on the second iteration month is 2, on the third iteration month is 3, etc.

Is that making sense? If not, are you having trouble understanding how method calls work or how loops work?