Pages

Sunday, October 27, 2013

How to Make an Amortization Table in Java

Have you ever wondered how banks, credit card companies and other lenders calculate your monthly payments? Perhaps you thought it was by some mysterious and suspiciously secret process? Actually, lenders calculate payments based on a technique known as an amortization table. The math for filling in an amortization table can be a bit tedious, however, and that makes it a good candidate for completing it in Java.

Instructions

    1

    Open a text editor and immediately save the file as "AmortizationTable.java." This will save you some trouble if your computer crashes in the middle of your work.

    2

    Paste the following Java code into the text editor:

    import java.io.Console;

    import java.text.NumberFormat;

    public class AmortizationTable

    public static void main(String[] args)

    Console console = System.console();

    String principalS = console.readLine("Please enter your principal> $");

    String lengthS = console.readLine("Please enter the length of your loan in months> ");

    String interestS = console.readLine("Please enter your annual interest rate. Do not include the percent sign> ");

    double principal = Double.parseDouble(principalS);

    int length = Integer.parseInt(lengthS);

    double interest = Double.parseDouble(interestS);

    double monthlyInterest = interest / (12 * 100);

    double monthlyPayment = principal * ( monthlyInterest / ( 1 - Math.pow((1 + monthlyInterest), (length*-1))));

    final int PAYMENT_WIDTH = 15;

    final int AMOUNT_WIDTH = 15;

    final int PRINCIPAL_WIDTH = 15;

    final int INTEREST_WIDTH = 15;

    final int BALANCE_WIDTH = 15;

    String pattern = "%" + PAYMENT_WIDTH + "s%" + AMOUNT_WIDTH + "s%" + PRINCIPAL_WIDTH + "s%" + INTEREST_WIDTH + "s%" + BALANCE_WIDTH + "s";

    System.out.printf(pattern, "PAYMENT", "AMOUNT", "PRINCIPAL", "INTEREST", "BALANCE");

    System.out.println();

    NumberFormat nf = NumberFormat.getCurrencyInstance();

    for (int x = 1; x <= length; x++)

    double amountInterest = principal * monthlyInterest;

    double amountPrincipal = monthlyPayment - amountInterest;

    principal = principal - amountPrincipal;

    System.out.printf(pattern, x, nf.format(monthlyPayment), nf.format(amountPrincipal), nf.format(amountInterest), nf.format(principal));

    System.out.println();

    The program is fairly simple. First, it asks you for the three pieces of information it needs to calculate the amortization table: the amount you borrowed, the length of the loan in months and the annual interest rate. It interprets that information into numbers and does the math according to some standard formulas used in the finance industry. When it's finished with the math, it prints out the table, showing your not just your monthly payment, but how much of it, each month, goes to interest and how much goes to paying down your original loan.

    3

    Save your work and open a terminal/command prompt. In Windows, you can do this by clicking "Start," "Run" and typing "cmd." Type the following in the terminal to compile your program:

    javac AmortizationTable.java

    Alternatively, if you chose a programmer's text editor such as Netbeans, you can probably just click the "Build" button and skip this step.

    4

    Type the following into your terminal to run the program:

    java AmortizationTable

    Answer the prompts and analyze the amortization table to see if the loan is the best deal for you.

0 comments:

Post a Comment