001_MortgageCalculator
If you’re new to Java programming, you may want to start with this project.
It’s simple and makes use of the basic functions
you’re required to know to code in Java. In this page, I am going to share a step-by-step process on how to build a Mortgage Calculator in IntelliJ (You can replace it with Eclipse/other IDEs but make sure they are Java specific).
The gist of a Mortgage Calculator is to find the amount of loan you should pay in each instalment. The formula for a monthly payment is as follows:
Now our job is to rewrite the formula in a coding language that the computer understands. In order to calculate the monthly payment, we should first ask the user to key in these parameters:
- How much the annual rate is
- How much principal to begin with
- Specify number of months in the instalment plan
To obtain these data from the user, we use the Scanner
reference (line 6). A reference is a type of variable where an address of an object is stored in memory so you can reuse (or in coding language: import) it multiple times; this is already embedded inside the IDE. This reference allows the user to type in the data into our screen, which we will process using the formula. Note that IntelliJ will automatically show import java.util.Scanner;
(line 2) each time you call in a reference (in this case, Scanner
). Make sure you write in this format each time you use a reference: Reference name = new Reference()
.
I named my Scanner
“screen” but you can rename it based on your liking. Also, be very mindful of the upper and lower cases as IntelliJ is very case-sensitive (ie. Scanner
should start with a capital letter), otherwise the program won’t understand and the reference would turn red. This indicates that an error has been found.
System.out.print("");
, or usually abbreviated with sout
as you type is a function used to print a string in the terminal. In this case, the parameters Principal, Annual Interest Rate, Period, and Mortgage are the ones we write inside the quotation marks. Be sure to write them in the order you want to show on the user’s screen.
After sout
, define these parameters in the following line by making use of suitable primitive variables int
, float
, byte
, etc. Remember float
and double
are for decimals and int
and byte
only works for whole numbers. Look up for primitive variables to see which one is suitable for your case.
In line 11 and 15, notice that I redefine annualInterest
and period
. This is because our formula above counts for a monthly interest, hence annualInterest
that the user inputs (assuming it is in percentage) should be divided by 100 and 12 as seen in line 11. As period
is inputted in years, we also have to multiply it by 12 in order to translate it into months, hence line 15.
Towards the end of the code, I renamed mortgage
to a new String mortgageFinal
(line 19) where I call in NumberFormat reference. This reference allows you to format your data in a specific way. There are a lot of functions to choose from! In this case I used getCurrencyInstance()
in order to format the mortgage monthly payment in its basic annotation (a $ sign and rounded off to the nearest hundredth). Here I have one problem though, I don’t know how to change the default into $ so my mortgage payment shows up in IDR (Indonesian currency). Will come back to this later!
To see what the code will turn up as, look at the screenshot of the terminal down below. The numbers in green are the data the user inputs.
This pretty much wraps up the gist of the whole project! However, there is one more problem: The program will crash if the user inputs a negative number, or an amount bigger than what the primitive variable is able to store. To prevent this error, we can continually ask the user to input until he/she keys in a positive number or implementing a range (ie. Principal=$0–100 000). This is where looping comes into play.
One of the functions
used to loop is while(true)
, which basically means that it will continually repeat itself until the user inputs what is asked for. As seen above, if
the principal is between 0 and 1 000 000, the code will end (or break) and move on to the next line. If not, then the else
function will print “Enter a value between 0 and 1 000 000” in the user’s screen and continually repeat itself until the condition is fulfilled (see terminal below).
So that’s it with the Mortgage Calculator! You can play around with the conditions of the Annual Rate and Period to your liking as well.
Thanks for reading our first post & we hope to see you in the next one!
Takeaways: Rewriting a Mathematical formula into code is probably one of the easiest ways to learn! You can explore other formulas and perform a similar task to this one to get more used to Java programming :)