Java

Number of posts: 18235

This Forum Only

Need Help with Assignment Posted by jparks1256 on 30 Aug 2009 at 1:21 PM
Hi everyone, I am in a Java class and need help with my assignment. I am a little lost (well totally lost) ;). I am trying to create a mortgage calculator that will loop through three different loan terms (7 year at 5.35%, 15 year at 5.5%, and 30 year at 5.75%). I need to display an amortization table that will show principle, interest, monthly payment, and balance for each month of the loan term. My code is below - ant assistance would be much appreciated.

<code>
import java.text.DecimalFormat;
import java.io.*;

public class Mortgage3 {

public static void main(String[] args)throws IOException{

double[][] payment={{200000, 84, .0535, 0.0},{200000, 180, .055, 0.0}, {200000, 360, .0575, 0.0}}; //starts the array

DecimalFormat decimal=new DecimalFormat ("#,##0.00"); //allows for the outcome to have 2 decimal points - barrowed from tony

for (int i=0; i <payment.length; i++){ //sets the loop

double loanAmount = payment[i][0]; //priniple amount
double interestAmount = payment[i][2]; //interest amount on loan
double loanTerms=payment[i][1]; //define the number of months for the loan
double interestMonthly=payment[i][2]/12; //calcualtes the interest amount monthly
double interestPower=Math.pow(interestMonthly +1, loanTerms); //calcualtes compounded interest
double interestCompound=interestMonthly * (interestPower)/(interestPower - 1); //determine increment of interest
double monthlyPayment=loanAmount*interestCompound; //this is the Prinicple[(interestMonthly)^360]/[(interestMonthly-1)]

//displays monthly payment for each term loan and interest
System.out.println();
System.out.println("************\tMcBride Financial Mortgage Calculator\t******************");

//buffer below allows for payments/interest to be stored allowing user input to show the info in increments (ie. hitting any key to continue)

//prints payment/interest/balance
System.out.println("Months----Payment---------Principal---------Interest----------Balance");

double principleAmount = 0.00; //amount of remaining principle
double interestAmountMonthly = 0.00; //monthly interest amount
double loanBalance = loanAmount; //sets loan balance equal to loan amount
double paymentMonthly = 0;//sets monthly payment to zero
int monthNumber = 0; //sets the loop to zero
int counter = 0; //sets the loop to zero

// calculates monthly paymeny / interest / principle - calculation taken from programmersheaven.com
interestAmountMonthly = (loanBalance * interestMonthly);
principleAmount = (loanBalance - interestAmountMonthly);
paymentMonthly = (principleAmount + interestAmountMonthly);

//creates the loop to display monthly principle/interest/loan balance 20 lines at a time
monthNumber++;

for ( monthNumber = 1; monthNumber <= 361; monthNumber++ ) {

//calculates the amortization table
interestAmountMonthly = (loanBalance * interestMonthly);
principleAmount = (monthlyPayment - interestAmountMonthly);
paymentMonthly = (principleAmount + interestAmountMonthly);
loanBalance = (loanBalance - principleAmount);

// displays the prinicple/interest/remaining balance in columns using tabs
System.out.println( monthNumber + "\t\$" + decimal.format(monthlyPayment) +"\t \$"+ decimal.format(payment[i][1]) +"\t \$"+ decimal.format(interestAmountMonthly) +"\t \$"+ decimal.format(loanBalance));

counter++; //sets loop for numnber of lines displayed

if (counter==20){ //only displays 20 lines at a time
System.out.print("\nPress ENTER to continue:");
byte[] buffer = new byte[20]; //line taken from programmersheaven.com
counter = 0;
System.out.println("Months----Payment---------Principal---------Interest----------Balance");

}//closes the if statement
}//closes the for loop
}//close of array loop
}//Close of Main
}//close of class Mortgage2

</code>
Re: Need Help with Assignment Posted by osachdev on 1 Sept 2009 at 2:20 AM
Hi
If you need help with Java visit: http://www.java-tuition.co.uk

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic