CODE
import java.text.DecimalFormat;
public class mortgage1
{
public static void main (String[] args)
{
//Declare and construct variables
DecimalFormat decimalPlaces = new DecimalFormat(".00");
double monthlypayments, principle,interest,
interestAmount, payment;
int amount, i, paymentsPerPage, lengthOfPause;
/* Below is the hard coded values used in the formula
for calculating the monthly mortgage payment amount
*/
interest = 0.0575; //Interest rate
principle = 200000; //Amount borrowed
monthlypayments = 360; //Number of monthly payments
//This is the formula used to calculate the monthly payment
payment = principle * ( (interest / 12.0) / (1 - Math.pow( (1 + (interest / 12.0)),-monthlypayments) ));
//Output to screen
System.out.println("Principle = $"+decimalPlaces.format(principle)); //The original amount borrowed
System.out.println("Interest Rate ="+interest*100 +"%"); //The interest rate
System.out.println("Monthly Payments = $"+decimalPlaces.format(monthlypayments)); //The number of payments
System.out.print("Payment per Month = $"); //The monthly payment amount
System.out.println(decimalPlaces.format(payment)); //formats the payment amount to two decimals
System.out.println("**************************");
System.out.println("**************************");
lengthOfPause = 5; //The amount of time (in seconds) to pause the page
paymentsPerPage = 20; //The number of payments to view per page
i = 1;
for(i = 1; i <= 360; i++)
{
System.out.println("Payment " + i + ":");
System.out.println("----------");
System.out.println("Payment Amount: $ " + decimalPlaces.format(payment));
interestAmount = ((interest / 12) * principle);
System.out.println("Interest Amount: $ " + decimalPlaces.format(interestAmount));
// You also have to calculate interest and add that back in.
principle = (principle - payment) + interestAmount;
System.out.println("Remaining Balance: $ " + decimalPlaces.format(principle));
System.out.println("----------");
System.out.println("");
if(((i - 1) % paymentsPerPage) == 0)
{
try{Thread.sleep(lengthOfPause * 1000);} //This is a pause function.
catch(InterruptedException ie){}
}
}
}
}
Edited to add the [ code] tags
This post has been edited by pbl: 23 Aug, 2008 - 06:11 PM