• Status: Solved
• Priority: Medium
• Security: Public
• Views: 303

# Mortgage Payment Calculator

I am attempting to incorporate a 7 year loan as well as a 15 year loan into the following code and these two loans would have different interest rates than the 30 year loan I have already programmed.  I need some assistance on how to incorporate a 7 year loan @ 5.35% and a 15 year loan @ 5.5% into my current code which compiles very nicely:

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

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));

}

}

Any help would be greatly appreciated.  Thank you
0
waw9667
• 5
• 3
1 Solution

Commented:
do you mean that for the fixed principal ( 200000 ), you have to show thw loan amount , interest rate, monthly payment for 30yrs, 15 yrs, 7 yrs resply.
0

Commented:
if that is the case,

double dInt[] = { 0.0535, 0.055, 0.0575 } ;
double dTerms[] = { 7,15,30 } ;

for ( int i = 0 ; i < 3 ; i ++ )
{
//Declaring Variables
double dPrincipal = 200000.00; //Total Loan or Principal Amount
double dInterest = dInt [ i ];  //Interest Rate all Payments are Calculated at
double dTerm = 12* dTerms [i ]; //Total Term of Loan in Months
double dMonthlyInterest = dInterest/12;
//calculate and print
}
0

Author Commented:
Only a single payment for each loan term though.  My current code shows what a \$200,000 loan monthly payment would be on a 30 year loan at 5.75% interest rate.  If you compile what I already have, I basically need to do the same exact thing for a 7 year loan at 5.35% interest rate and a 15 year loan at 5.5% interest rate.  To answer your question,
yes.  However, I already have the 30 year portion completed.
0

Commented:
double dInt[] = { 0.0535, 0.055, 0.0575 } ;
double dTerms[] = { 7,15,30 } ;

for ( int i = 0 ; i < 3 ; i ++ )
{
//Declaring Variables
double dPrincipal = 200000.00; //Total Loan or Principal Amount
double dInterest = dInt [ i ];  //Interest Rate all Payments are Calculated at
double dTerm = 12* dTerms [i ]; //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));

System.out.println( "" ) ;
}

have you tried this,
0

Author Commented:
Did I add something in the wrong place ?  It contains three errors.  I probably did.

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

public class Mortgage

{
public static void main(String[] args)
double dInt[] = {0.0535,0.055,0.0575};
double dTerms[] = {7,15,30};

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

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 = dInt[i];  //Interest Rate all Payments are Calculated at
double dTerm = 12*dTerms[i]; //Total Term of Loan in Months
double dMonthlyInterest = dInterest/12;
//Calculate and print

//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));

}
}

0

Commented:
>>public static void main(String[] args)

public static void main(String[] args)
{

then one at the last,

System.out.println("\t\tThe Total Monthly Payment is: " + decimalPlaces.format(dMonthly));
}
}
}
0

Author Commented:
Thanks again for sharing your incredible knowledge and wisdom with a beginner like myself.  I work in the IT field but this programming thing is just too new for me right now.  Have a great day !!
0

Commented:
:)
Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Featured Post

• 5
• 3
Tackle projects and never again get stuck behind a technical roadblock.