Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java Mortgage Amortization

Posted on 2006-07-17
12
Medium Priority
?
677 Views
Last Modified: 2008-01-09
I have completed this much of it but need the program somehow to only display the payments based upon a 5.75% rate but when I change it to only reflect a 30 year loan at 5.75%, it does not compile.  This actually compiles but the payments are all jacked and I can't figure it out.  I am trying to show the $200,000 balance and the first payment as well as interest paid and continue displaying this until the balance is zero.  Please help !  Here's what I have now:

//import the java classes
import java.text.NumberFormat;
import java.util.Locale;

public class MortgageMulti {
    //MortgageMulti is the existing function
    public static void main(String[] arguments) {

        //hard code the value of the mortgage
        double mortgage = 200000.00;
        //hard code in the yearly interst rate
        double[][] yearsYearlyRate = {
            { 7, 15, 30 },
            { 5.35, 5.5, 5.75 }
        };
        //then code in the value for the number of times a year mulitplied by
        // the amoutn paid each time
        double[][] monthsMonthlyRt = {
            { yearsYearlyRate[0][0] * 12, yearsYearlyRate[0][1] * 12,
              yearsYearlyRate[0][2] * 12
            },
            { yearsYearlyRate[1][0] / 1200, yearsYearlyRate[1][1] / 1200,
              yearsYearlyRate[1][2] / 1200
            }
        };

        for (int i = 0; i < 3; i++) {

            //Call the method
            double payment = calPayment(monthsMonthlyRt[1][i], monthsMonthlyRt[0][i], mortgage);

            // Display the monthly payment
            System.out.println("\nMortgage Amount \tTerm \t\tRate \t\tMonthly Payment");
            System.out.println("$200,000 \t\t" + yearsYearlyRate[0][i] + " years\t"
                    + yearsYearlyRate[1][i] + "%\t\t" + payment);

            System.out.println("\nMonth # \tInterest for the Month \t\tLoan Balance");

            // Call the method
            interestBalanceList(mortgage, monthsMonthlyRt[1][i], monthsMonthlyRt[0][i], payment);
        }
    }

    // Utility method to calculate the monthly mortgage payment
    // (1st error spotted)public static double calPayment(double param1, double
    // param2, double mortgage);
    public static double calPayment(double param1, double param2, double mortgage) {

        // Calculate the monthlyPayment of the mortgage
        double monthlyPayment = (param1 + param2 / (Math.pow((1 + param1), param2) - 1)) * mortgage;

        return monthlyPayment;
    }

    // Utility method to list the interest paid and loan balance
    public static void interestBalanceList(double mortgage, double param1, double param2,
            double payment) {

        // Use the for loop to display the list
        for (int i = 1; i < (param2 + 1); i++) {

            // Pause the list and go to the next month and next amount
            System.out.println("Hit the 'ENTER' key to exit.");
            try {
                System.in.read();
            } catch (Exception e) {
            }
            // Calculate the monthlyInterest
            double monthlyInterest = mortgage * param1;

            // Calculate the loanBlance
            double loanRemaining = mortgage - (payment - monthlyInterest);

            // Set the value of mortgageAmount to the value of loanRemaining
            mortgage = loanRemaining;

            // Formate the monthlyInterest into U.S. currency
            NumberFormat n = NumberFormat.getCurrencyInstance(Locale.US);
            String str = n.format(monthlyInterest);

            // Formate the loanRemaining into U.S. currency
            NumberFormat num = NumberFormat.getCurrencyInstance(Locale.US);
            String string = num.format(loanRemaining);

            // Display the month #, interest paid and loan balance
            System.out.println("\n " + i + "\t\t " + str + "\t\t\t" + string);

        }
    }
}
0
Comment
Question by:waw9667
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 17128398
Hi waw9667

can you be more specific? I am not sure I understand what exactly you would like to do.

Cheers
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17128854
>>  double monthlyPayment = (param1 + param2 / (Math.pow((1 + param1), param2) - 1)) * mortgage;

i think this is not correct,

monthly payment is greater the mortage itself, so you get negative balance and it keeps on increasing in negative

0
 

Author Comment

by:waw9667
ID: 17130298
I'll be more specific with what I am trying to do.  The program should do something like this:

Mortgage Payment     |     Interest Rate     |     Interest Paid     |     New Balance

The program starts with a balance of $200,000 and the four categories above will obviously display new numbers on every line as the loan will be decreasing and I need to do this until the loan reaches zero.  I hope this gives you a better understanding of what I am trying to accomplish and I certainly appreciate your speedy response to my post.  I am staying home from work today with much hope that I can get this completed which means I will be on the computer all day, so I will also be able to respond back just as timely.  Thank you for your help.
0
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!

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17130337
did u check that formula,
0
 

Author Comment

by:waw9667
ID: 17130385
I can definitely see what you're saying about the formula and that would explain why it compiles the way it does.  However, I have not successfully changed the formula yet.
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17130900
check this link for formula on mortage calculator,

http://infoscouts.com/finance/mortgagecalc.htm

try giving the monthly payments as given by the calculator,

your code is working fine
0
 

Author Comment

by:waw9667
ID: 17130962
I forgot to mention that this program is without a GUI.
0
 

Author Comment

by:waw9667
ID: 17130984
However, I am assuming you wanted me to view the source code of that calculator.  Am I correct ?
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17131033
  public static double calPayment(double param1, double param2, double mortgage) {

       
          double tmp = 1 + ( param1 ) ;
          for ( int i = 1 ; i < param2 ; i++)
          tmp = tmp * ( 1 + param1 ) ;
          double monthlyPayment = mortgage * tmp * param1 / ( tmp - 1 ) ;

          // Calculate the monthlyPayment of the mortgage
        //double monthlyPayment = (param1 + param2 / (Math.pow((1 + param1), param2) - 1)) * mortgage;

        return monthlyPayment;
    }
0
 
LVL 10

Accepted Solution

by:
mukundha_expert earned 2000 total points
ID: 17131039
use the above method instead of yours... now the code works
0
 

Author Comment

by:waw9667
ID: 17131269
Again, thank you for your incredibly fast response time.  My only problem here is that I want the payments to calculate at a 5.75% interest rate on a 30 year loan and not 7 but when I manipulate the code to my desired data, it will not compile.  Here is my code for displaying the monthly payment on a $200,000 loan at 5.75% on a 30 year term:

import java.io.*;
import java.util.Date;
import java.text.DecimalFormat;

//More java packages can be added here for the Week Three Assignment

public class Mortgage

{
public static void main(String[] args)


      {

Date currentDate = new Date();
//Date Constructor
DecimalFormat decimalPlaces = new DecimalFormat("$0.00");

//Declaring Variables
double dPrincipal = 200000.00; //Total Loan or Principal Amount
double dInterest = .0575;  //Interest Rate all Payments are Calculated at
double dTerm = 12*30; //Total Term of Loan in Months
double dMonthlyInterest = dInterest/12;


//Declaring Variables

final double dMonthly = dPrincipal * (dMonthlyInterest / (1 - Math.pow(1 + dMonthlyInterest,-dTerm)));
//The above formula calculates and determines the exact monthly payment based upon input

//The following code will display the output of the program
System.out.println("\t\t" + currentDate);
System.out.println("\t\tThe Total Loan Amount is: " + decimalPlaces.format(dPrincipal));
System.out.println("\t\tThe Calculated Interest Rate is: " + dInterest);
System.out.println("\t\tThe Total Number of Monthly Payments is: " + dTerm);
System.out.println("\t\tThe Total Monthly Payment is: " + decimalPlaces.format(dMonthly));

      }

}

I simply need to incorporate what you've helped me with as well as what I've completed, and apply it somehow within this code.  This piece compiles nicely and I would like for you to run it so that you can see what it looks like and I think after doing so, you will see what I meant by the interest rate and term of the loan.
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17131511
do you mean to say that you want to display the monthly status only for 30 year loan..??

if so, you can change the fiirst code to get that

>> for (int i = 0; i < 3; i++) {

>>            //Call the method
>>            double payment = calPayment(monthsMonthlyRt[1][i], monthsMonthlyRt[0][i], mortgage);


set i = 2, it will do for 30 year loan with 5.75%
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

604 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