[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do i fix errors in my Mortgage Calculator in my Java Class

Posted on 2009-04-28
3
Medium Priority
?
941 Views
Last Modified: 2012-05-07
Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:

- 7 year at 5.35%
- 15 year at 5.5%
- 30 year at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.

import java.text.DecimalFormat;        // class needed to format decimals
 
public class Mort_Cal            // program name: Mortgage Calculator
{
 
    public static void main(String[] arguments)        // start main() function
    {
    // define variables
    double monthlyPayment;    // monthly payment
    double principal;        // loan principal
    double interestRateYears;    // annual interest rate (percentage)
    double interestRateMonths;     // monthly interst rate
    int []termYears={7,15,30};        // length of loan in years
    int termMonths;        // length of loan in months
    int linecount;        // number of lines to be displayed
 
    double []balance={0.,0.,0.};        // displays current balance
    double []interestPaid={5.35,5.5,5.75};        // displays the interest paid
    double principalPaid=200000;        // displays the principal paid
    double []monthlyInterestPayment;    // holds current interest payment
    double monthlyPrincipalPayment;    // holds current principal payment
 
 
 
 
 
    // formats numbers to display only two decimal places
    java.text.DecimalFormat dec = new java.text.DecimalFormat(",###.00");
 
    //for loop//
	   for (int i = 0; i < interestPaid; i++)
	   {
 
	//time  and interest rate//
	          System.out.print("\n\t over " + termYears[i] + " years");
	     System.out.print(" with an interest rate of " + interestPaid[i]);
     System.out.print("%");
 
 
    // hard coded information to display basic loan information
    System.out.println("\n\n\t*** Mortgage Calculator ***\n\n" +
           "\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));
 
 
 
 
    // displays monthly mortgage payment resulting from above calculation
        System.out.println("\n\nBased 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("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
        System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
        System.out.println("---------\t---------\t---------\t---------");
 
    // start while loop
    while (termMonths > 0)
    {
    // information to display
    System.out.println(termMonths + "\t\t$" +      dec.format(monthlyPrincipalPayment) +
        "\t\t$" + dec.format(monthlyInterestPayment) +
        "\t\t$" + dec.format(balance));
 
    // decrement months
        termMonths--;
 
    // calculate interest and principal payments
    monthlyInterestPayment = (balance * interestRateMonths);
    monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
    balance = (balance - monthlyPrincipalPayment);
 
for(int monthNumber=1;monthNumber<termYears[i]*12;monthNumber++)
{
 
balance[i] = (principal-month);
principalPaid= balance[i];
interestPaid[i]=principal * (interest[i]/(12*100));
 
System.out.println((monthNumber)+ "\t\t\t" + decimalplace.format(balance[i]) + "\t\t\t" + decimalplace.format(interestpaid[i]));
monthNumber ++;
 
 
 
 
 
    if(linecount == 20)
    {
    linecount = 0;
    try
    {
    Thread.sleep(3000);        // pause to last three seconds
    }
    catch (InterruptedException e)
    {
      }
    }    // end if
    else
    {
        linecount++;
    }    // end else
 
               }           // end while
}
}

Open in new window

0
Comment
Question by:imagekrazy
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
Gibu George earned 1000 total points
ID: 24250194
Whats is the issue you are facing?
0
 

Author Comment

by:imagekrazy
ID: 24250616
I am getting a  one error message at the end of the file.
0
 
LVL 12

Expert Comment

by:Gibu George
ID: 24482507
Whats the error message?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question