this is a java amortization table and i need some help in figuring about with the for loops and calculations.?

import java.util.Scanner;
import java.text.DecimalFormat;

public class LoanPayment
{

//main method beings with exevution of Java application
public static void main ( String args[])
{

//create scanner to obtain input from command window
Scanner input = new Scanner(System.in);

DecimalFormat decimalPlaces=new DecimalFormat("0.00"); //Create Format

System.out.print("Welcome to the Loan Payment Calculator by Jeffery Smith");
System.out.print("\nFor Exercise 1 in CMIS 212");

//declare variables
double I;
double LB;
double CC;
double DP;
double Y;
double PpY;
double MonthlyPayment;

//declare variable for exercise 2
double NB;
double PA;
double IA;
double IB;

//declare variables for totals
double PT = 0;
double IT = 0;
double BT = 0;

//instruction line
System.out.println("\nFor each of the prompts below, enter a single value and press Enter:");

//prompt
System.out.print("Yearly Interest Rate:");
I = input.nextDouble();//read first number from user

System.out.print("Loan Amount:$");
LB = input.nextDouble();//read first number from user

System.out.print("Closing Costs:$");
CC = input.nextDouble();//read first number from user

System.out.print("Down Payment:$");
DP = input.nextDouble();//read first number from user

System.out.print("Time(in years) for loan:");
Y = input.nextDouble();//read first number from user

System.out.print("Number of payments per year:");
PpY = input.nextDouble();//read first number from user

//formula to the the MonthlyPayment
MonthlyPayment = (I * (LB + CC – DP)) / (PpY * ( 1 – Math.pow(1 + I/PpY, -Y * PpY) ));
MonthlyPayment = Math.ceil(MonthlyPayment *100)/100;//round to the nearest cent
System.out.printf("The total is $%.2f\n", MonthlyPayment );

System.out.println("\nYear\tPayment\tPrinciple\tInterest\tBalance");
System.out.println("————————————————————");

IB = LB + CC – DP;
IA = IB * (I / PpY);
PA = MonthlyPayment * Math.pow(1+IA,PpY);
NB = IB – PA;

for (int year = 1; year <= Y; year++)
{

for (int payment = 1; payment <= PpY; payment++)
{

NB = IB* Math.pow( 1.0 + I, -payment );

PA = MonthlyPayment * Math.pow(1+ I, -payment);

IA = PA * Math.pow(1+ I, -payment);

for (int number = 1; number <= PpY*Y; number += 1)
{
PT = PpY * PA;
}

for (int number = 1; number <= PpY; number += 1)
{
IT = PpY * IA;
}

System.out.printf("%s \t%3d% 11.2f\t %12.2f\t %15.2f\n",year, payment, PA, IA, NB);

}
System.out.println("::::::::::::::::::::::::::::::::::::::::::::::::::::::::");
System.out.printf("Year %s Totals: Principle: %.2f Interest: %.2f Balance: %.2f\n", year,PT,IT, NB);
System.out.println("::::::::::::::::::::::::::::::::::::::::::::::::::::::::");

}// end for loop
}//end method main
}//end class LoanPayment

It would appear that you were being too complicated in printing the amortization table and that led you down a bad path. Consider this: Once you know the periodic (monthly?) payment amount, all you have to to is

1) set the running balance (NB) to the original loan amount

2) calculate the interest accrued during the payment period
3) subtract that from the payment to get the principal payment
4) subtract the principal payment from the running balance
5) print your numbers
6) add to your running yearly totals

7) repeat steps 2-6 remembering to display yearly totals at the end of each year and zero out those running totals.

Try the following as a replacement for your double-loop. Notice that the IB and BT variables are no longer needed.

//////////// code begins here ///////////

System.out.printf("The periodic payment is $%.2f\n", MonthlyPayment);

NB = LB + CC – DP; // Calculate the new balance

for (int year = 1; year <= Y; year++) {

System.out.println("\nYear \tPayment \tPrincipal \tInterest \tBalance");
System.out.println("—— ——– ——");

for (int payment = 1; payment <= PpY; payment++) {

IA = NB * (I / PpY); // Interest paid this period
PA = MonthlyPayment – IA; // Principal paid this period
if (PA > NB) {
// The (last) payment is greater than the balance so adjust it.
PA = NB;
}
NB -= PA; // Subtract this principal payment from the balance
System.out.printf("%s \t%3d% 11.2f\t %12.2f\t %15.2f\n", year, payment, PA, IA, NB);

PT += PA; // Total up the Payments this year
IT += IA; // Total up the Interest thei year
}
System.out.println(":::: :::::::: :::");

System.out.printf("Year %s Totals: Principle: %.2f Interest: %.2f Balance: %.2f\n", year, PT, IT, NB);
System.out.println(":::::: :::: :::::");

// Reset the totals for the year
PT = 0;
IT = 0;

}// end for loop

///// End of Code /////

Update 1: Moved the line of code for updating the NewBalance BEFORE printing out the new balance.

Leave a comment

1 Comments.

  1. It would appear that you were being too complicated in printing the amortization table and that led you down a bad path. Consider this: Once you know the periodic (monthly?) payment amount, all you have to to is

    1) set the running balance (NB) to the original loan amount

    2) calculate the interest accrued during the payment period
    3) subtract that from the payment to get the principal payment
    4) subtract the principal payment from the running balance
    5) print your numbers
    6) add to your running yearly totals

    7) repeat steps 2-6 remembering to display yearly totals at the end of each year and zero out those running totals.

    Try the following as a replacement for your double-loop. Notice that the IB and BT variables are no longer needed.

    //////////// code begins here ///////////

    System.out.printf("The periodic payment is $%.2f\n", MonthlyPayment);

    NB = LB + CC – DP; // Calculate the new balance

    for (int year = 1; year <= Y; year++) {

    System.out.println("\nYear \tPayment \tPrincipal \tInterest \tBalance");
    System.out.println("—— ——– ——");

    for (int payment = 1; payment <= PpY; payment++) {

    IA = NB * (I / PpY); // Interest paid this period
    PA = MonthlyPayment – IA; // Principal paid this period
    if (PA > NB) {
    // The (last) payment is greater than the balance so adjust it.
    PA = NB;
    }
    NB -= PA; // Subtract this principal payment from the balance
    System.out.printf("%s \t%3d% 11.2f\t %12.2f\t %15.2f\n", year, payment, PA, IA, NB);

    PT += PA; // Total up the Payments this year
    IT += IA; // Total up the Interest thei year
    }
    System.out.println(":::: :::::::: :::");

    System.out.printf("Year %s Totals: Principle: %.2f Interest: %.2f Balance: %.2f\n", year, PT, IT, NB);
    System.out.println(":::::: :::: :::::");

    // Reset the totals for the year
    PT = 0;
    IT = 0;

    }// end for loop

    ///// End of Code /////

    Update 1: Moved the line of code for updating the NewBalance BEFORE printing out the new balance.
    References :

Leave a Reply

Your email address will not be published. Required fields are marked *

*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>