Payday Loans | Personal Loans | Home Loans
In: Loans Calculator
9 Mar 2010I am doing a mortgage calculator, it is supposed to give a list of declining list of payment but it does not and I can not figure out why I did wrong. It runs gives the amount of the payment and it has headers but no list.
/**
*Week 2 Assignment- Diane Beck
* A proram written in Java (without graphical user interface) that will
* calculate and display the monthly payment amount to fully amortize
* a $200,000.00 loan over a 30 year term at 5.75% interest.
*
*
* Student: Diane Beck
*/
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;
import java.io.IOException;//imports code for handling of Enter key input
import java.io.BufferedReader;
import java.io.InputStreamReader;
class McBrideMortgageCalculator {
public static void main (String[] args) //start main() function
{
//declare variables
double monthlyPayment; //monthly payment
double principal; //loan principal
double interestRateYears; //annual interest rate (precentage)
double interestRateMonths; //monthly interest rate
int termYears; //length of loan in years
int termMonths; //length of loan in months
int linecount; //number of lines to be displayed
double balance; //displays current balance
double interestPaid; //displays the principle paid
double monthlyInterestPayment; //holds current interest payment
double monthlyPrincipalPayment; //holds current principle payment
//assign values
principal = 200000; //total loan amount
interestRateYears = 5.75; //APA (annual percentage rate)
interestRateMonths=(interestRateYears / 12) / 100; //monthly interest rate
termYears = 30; //length of loan in years
termMonths = (termYears*12); //length of loan in months
monthlyInterestPayment = 0; //set current interest payment = 0
monthlyPrincipalPayment = 0; //set current principal payment = 0
balance = principal; //set current balance – principal
linecount = 15; //display 15 lines of results
//formats number to display only two decimal places
java.text.DecimalFormat dec = new
java.text.DecimalFormat (”,###.00″);
//hard coded information to display basic loan information
System.out.println(”nnt***Mortgage Calculator***nn” +
“nLoan Amount: t$” + dec.format(principal) +
“nInterest Rate: t” + interestRateYears +”%”+
“nTerm (Years): t” + termYears);
//calculate monthly mortgage payment
monthlyPayment = (principal * interestRateMonths)/(1 – Math.pow(1 + interestRateMonths, – termMonths));
/*
* J
* M = P *—————– <--------Formula to calculate
amortization
*
*
* M = Monthly Payment (monthlyPayment)
* P = Loan Principal (principal)
* I = Interest Rate (interestRateYears)
* L = Length (term) of loan, in years (termYears)
* J = Monthly Interest Rate (interestRateMonths)
* J = I/(12*100)
* N = Number of months of loan (termMonths)
* N = L*12
*/
{
//displays monthly mortgage payment resulting from above calculation
System.out.println("nnBased on the above criteria," +
"your monthly payment will be:" +
"$" + dec.format(monthlyPayment));
//formula (s) to calculate monthly interest and principal payments
monthlyInterestPayment = (balance * interestRateMonths);
monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
//format column headers for results to be displayed
System.out.println("nnnMonthsttPrincipaltInteresttBalance");
System.out.println("RemainingtPaymenttInteresttBalance");
System.out.println("-------t-------t-------t-------");
//start while loop
while (termMonths >0);
//information to display
System.out.println(termMonths + “tt$” + dec.format (monthlyPrincipalPayment) +
dec.format (monthlyInterestPayment) + “tt$”+ dec.format (balance – monthlyPrincipalPayment));
//decrement months
termMonths =144;
//calculate interest and principal payments
monthlyInterestPayment = (balance + interestRateMonths);
monthlyPrincipalPayment = (monthlyPayment – monthlyInterestPayment);
balance = (balance – monthlyPrincipalPayment);
}
//these conditional statements cause the results to pause in (lineconut == 20)
{
linecount = 0;
}
try
{
Thread.sleep (3000); //pause to last three seconds
I now have my program working but it is giving me what looks like hundreds of rows. I give me data in the neg field, instead of stopping at the o balance mark. What is wrong with it now?
This is site about loans reference and information question..
1 Response to I am doing a program and It runs but it does not give me a tablet, what am I doing wrong?
WaMSie
March 9th, 2010 at 1:33 am
The code looks a bit damaged in its copy-and-paste journey. However I suspect the lines:
//start while loop
while (termMonths >0);
The semicolon is an empty statement, so it says “If termMonths is zero or less, stop here forever”, which is probably not what you want. I would expect you want to swap the ; for a {, you program probably still won’t work but you’ll be a step nearer.
Changing:
//decrement months
termMonths =144;
to something like:
//decrement months
termMonths–;
might also be helpful!