Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,482 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,766 people online right now. Registration is fast and FREE... Join Now!




Mortgage Calculator add arrays

 

Mortgage Calculator add arrays, Need to add two more interest rates and years to the java program

Stupidography

20 Nov, 2008 - 07:17 AM
Post #1

New D.I.C Head
*

Joined: 4 Nov, 2008
Posts: 2

I do not understand how to add 7 years @ 5.35% , 15 years @ 5.5% to the already
calculated 30 years @5.75%. I could not round this to .00 as well, and need to have
this code looked at so I can tell how to do this without just doubling the same code
with different percentages and years. IS there not an easier way to add the three together
in one section? I do not need the entire printout of the payments, just the total amount
per month in principle and interest....




MY CODE IS AS FOLLOWS:

CODE

import java.text.DecimalFormat;

public class MortgagePayment
{
    public static void main(String args[]) throws Exception
{
     //declare and construct variables
     int loanAmt    = 200000;       // this is the principal loan amount
     int loanTerm = 30;                // this is the loan term in years
     int monthNum = 360;              // indicates the monthly line item number
     int line = 0;
     double intRate    = 5.75;        // this is the initial interest rate
     double monthlyPay    = 0;      // monthly payment
     double monPrinPay;                // monthly principal payment
     double newLoanBal = 200000;    // the loan balance
     double monIntPaid;                // interest paid
     double newIntRate    = 0;      // monthly interest rate



     // displays in the console window
     System.out.println();
     System.out.println("Welcome to The Mortgage Payment Calculator");
     System.out.println();
     System.out.println("This program will calculate and display: (1) Monthly mortgage payments");
     System.out.println("The principal loan amount = $" + loanAmt);
     System.out.println("The interest rate = " + intRate + "%");
     System.out.println("The term of the loan = " + loanTerm + " years");

     // construct the formulas
     loanTerm      =    loanTerm * 12;
     newIntRate  =    (intRate * .01) / 12;
     monthlyPay  =    loanAmt * newIntRate / (1 - Math.pow(1 + newIntRate, - loanTerm));

     // displays the variable information and formula results
     System.out.println();
     System.out.println("The monthly payment for a $" + loanAmt + " over a " + loanTerm + "-month term (30 years) at a ");
     System.out.println(intRate + "% interest rate = $" + monthlyPay);
     System.out.println();


    }
}



***Added code tags***
-jjsaw5

This post has been edited by jjsaw5: 20 Nov, 2008 - 07:40 AM

User is offlineProfile CardPM
+Quote Post


jjsaw5

RE: Mortgage Calculator Add Arrays

20 Nov, 2008 - 07:40 AM
Post #2

I must break you
Group Icon

Joined: 4 Jan, 2008
Posts: 2,656



Thanked: 25 times
Dream Kudos: 125
My Contributions
code.gif
User is offlineProfile CardPM
+Quote Post

bbq

RE: Mortgage Calculator Add Arrays

20 Nov, 2008 - 08:04 AM
Post #3

omgwtfbbq
Group Icon

Joined: 15 May, 2008
Posts: 725



Thanked: 64 times
Dream Kudos: 125
My Contributions
to print out to 2 decimals... use the String.format metod

java

//example
double x = 3;
double y = 1;

double z = y / x;

System.out.println( z ); // will print 0.333333333333333333333 etc
System.out.println( String.format("%.2f", z) ); // will print0.33



Hope that helped

This post has been edited by bbq: 20 Nov, 2008 - 08:05 AM
User is offlineProfile CardPM
+Quote Post

P4L

RE: Mortgage Calculator Add Arrays

20 Nov, 2008 - 08:29 AM
Post #4

Geek 4 Life
Group Icon

Joined: 7 Feb, 2008
Posts: 2,163



Thanked: 16 times
Dream Kudos: 125
My Contributions
Change this
java

int loanTerm = 30;


To

java

int loanTerm = New lTerm [7,15,30];


and
java

double intRate = 5.75;


to
java

double intRate = New inTRate [5.75, 5.5, 5.35];


This will declare the Rate and Term fields as an array. I will try to get a better example of the code needed when I have my machine with Java on it.
User is offlineProfile CardPM
+Quote Post

Stupidography

RE: Mortgage Calculator Add Arrays

22 Nov, 2008 - 09:11 AM
Post #5

New D.I.C Head
*

Joined: 4 Nov, 2008
Posts: 2

IS THIS ANY CLOSER?
I am still getting errors, and canot figure out what is wrong here...




import java.text.DecimalFormat;

//
public class MortgagePayment
{
public void calc(double interest, double principle, int monthlypayments)
{

//Declare and construct variables
DecimalFormat decimalPlaces = new DecimalFormat(".00");
double monthlypayments, principle, interest, interestAmount, payment;

int amount, i, paymentsPerPage, lengthOfPause;


// Variable Declaration
int principal[] = {200000, 200000, 200000}; // Principal for each calculation
double interestRate[] = {5.35, 5.50, 5.75}; // Interest Rate for each interest rate
int totalYears[] = {7, 15, 30}; // Length in years for each loan
double monthlyInterest[] = {0, 0, 0}; // Monthly Interest
int totalMonths[] = {0, 0, 0}; // Number of Months
double monthlyPayment[] = {0, 0, 0}; // Monthly Payment

// Calculations Loop
for (int i = 0; i < 3; i++){
monthlyInterest[i] = interestRate[i] / (12 * 100);
totalMonths[i] = totalYears[i] * 12;
monthlyPayment[i] = principal[i] * (monthlyInterest[i] /
(1 - (Math.pow((1 + monthlyInterest[i]),(-totalMonths[i])))));
}

//Output to screen
System.out.println("Principle = $"+decimalPlaces.format(principle)); //The loan amount
System.out.println("Interest Rate ="+interest*100 +"%"); //The interest rate
System.out.print("Payment per Month = $"); //The monthly payment amount
System.out.println(decimalPlaces.format(payment));
System.out.println("********************");
System.out.println("********************");

for(i = 1; i <= 1; 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("Loan 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){}
}
}
}
}
User is offlineProfile CardPM
+Quote Post

bbq

RE: Mortgage Calculator Add Arrays

26 Nov, 2008 - 08:37 PM
Post #6

omgwtfbbq
Group Icon

Joined: 15 May, 2008
Posts: 725



Thanked: 64 times
Dream Kudos: 125
My Contributions
java

import java.text.DecimalFormat;

//
public class MortgagePayment
{
public void calc(double interest, double principle, int monthlypayments)
{

//Declare and construct variables
DecimalFormat decimalPlaces = new DecimalFormat(".00");
double monthlypayments, principle, interest, interestAmount, payment;

int amount, i, paymentsPerPage, lengthOfPause;


// Variable Declaration
int principal[] = {200000, 200000, 200000}; // Principal for each calculation
double interestRate[] = {5.35, 5.50, 5.75}; // Interest Rate for each interest rate
int totalYears[] = {7, 15, 30}; // Length in years for each loan
double monthlyInterest[] = {0, 0, 0}; // Monthly Interest
int totalMonths[] = {0, 0, 0}; // Number of Months
double monthlyPayment[] = {0, 0, 0}; // Monthly Payment

// Calculations Loop
for (int i = 0; i < 3; i++){
monthlyInterest[i] = interestRate[i] / (12 * 100);
totalMonths[i] = totalYears[i] * 12;
monthlyPayment[i] = principal[i] * (monthlyInterest[i] /
(1 - (Math.pow((1 + monthlyInterest[i]),(-totalMonths[i])))));
}

//Output to screen
System.out.println("Principle = $"+decimalPlaces.format(principle)); //The loan amount
System.out.println("Interest Rate ="+interest*100 +"%"); //The interest rate
System.out.print("Payment per Month = $"); //The monthly payment amount
System.out.println(decimalPlaces.format(payment));
System.out.println("********************");
System.out.println("********************");

for(i = 1; i <= 1; 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("Loan 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){}
}
}
}
}


You tell us, does it work ? if not what is wrong with it ?

Oh and don't forget to use the code tags when posting codez smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 04:03AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month