Welcome to Dream.In.Code
Getting Java Help is Easy!

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




Mortgage Calculator with array and loop

 
Reply to this topicStart new topic

Mortgage Calculator with array and loop

kevin_co
post 12 Oct, 2008 - 05:15 PM
Post #1


New D.I.C Head

*
Joined: 30 Sep, 2008
Posts: 2

This is the last part of a series of increasingly harder homework assignments, this assignment reads as:

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.

This is what I have come up with, and it's obvious my logic is way off here, the if and else if statement are way off I believe.

CODE

/*
  /*
      Week 4: Loan Amortization Calculator
      Programmer: Kevin L. Collums
      Date: September 22, 2008
      Filename: LoanAmort4.java
      Purpose: This project will calculate the monthly loan payment for
           a loan originated at $200,000.00 at 5.75% over 30 years
*/

import java.io.*;

public class LoanAmort4
{
    public static void main(String[] args) throws IOException
    {
        //Declare variable for loan amount
        double LoanAmt = 200000;
        double PrincAmtOne, PrincAmtTwo, PrincAmtThree;
        double PayOne, PayTwo, PayThree;// creates variable for three different payment amounts
        double MonthIntOne, MonthIntTwo, MonthIntThree; //creates variables for three differene interest amounts
        double MonthPrincOne, MonthPrincTwo, MonthPrincThree; //creates variables for three different principal payment amounts
        PrincAmtOne = LoanAmt;
        PrincAmtTwo = LoanAmt;
        PrincAmtThree = LoanAmt;
        MonthPrincOne = 0;
        MonthPrincTwo = 0;
        MonthPrincThree = 0;
        MonthIntOne = 0;
        MonthIntTwo = 0;
        MonthIntThree = 0;
        int LineCount = 1;




        //create loan term array
        double [] Term = {84, 180, 360};

        //create interest rate array
        double [] Rate = {.0535, .055, .0575};

        //calculations for each payment amount
        PayOne = (LoanAmt * Rate[0]) / (1-Math.pow(1+Rate[0],-Term[0]));
        PayTwo = (LoanAmt * Rate[1]) / (1-Math.pow(1+Rate[1],-Term[1]));
        PayThree = (LoanAmt * Rate[2]) / (1-Math.pow(1+Rate[2],-Term[2]));

        //output
        System.out.println("\t\tKevin's Loan Amortization Schedule");
                System.out.println();
                System.out.println("\t\tLoan Amount: $200,000.00");
                System.out.println("\t\tTerm: 7 Years");
                System.out.println("\t\tInterest Rate 5.35%");
                System.out.printf("\t\tThe monthly payment amount is $%.2f", PayOne);
                System.out.println();
                System.out.println("\t\tTerm: 15 Years");
                System.out.println("\t\tInterest Rate 5.5%");
                System.out.printf("\t\tThe monthly payment amount is $%.2f", PayTwo);
                System.out.println();
                System.out.println("\t\tTerm: 30 Years");
                System.out.println("\t\tInterest Rate 5.75%");
                System.out.printf("\t\tThe monthly payment amount is $%.2f", PayThree);
                System.out.println();






                if (PrincAmtOne <= 0)
                {
                        //monthly calculations for first payment amount
                        MonthIntOne =         (PrincAmtOne * Rate[0]) / 365 * 30;
                        MonthPrincOne =     (PayOne - MonthIntOne);
                        PrincAmtOne =        (PrincAmtOne - MonthPrincOne);

                        if (PrincAmtOne <= 0)
                        {
                        //outputs
                        System.out.printf("Interest payment: $%.2f", MonthIntOne);
                        System.out.println("");
                        System.out.printf("Principal payment: $%.2f", MonthPrincOne);
                        System.out.println("");
                        System.out.printf("New Principal Balance: $%.2f", PrincAmtOne);
                        System.out.println("");
                        LineCount++;
                        }
                        else
                        {
                        System.out.printf("The principal balance is $0.00");
                        System.out.printf("");
                        }

                else if (PrincAmtTwo <= 0)
                        //monthly calculations for first payment amount
                        MonthIntTwo =         (PrincAmtTwo * Rate[0]) / 365 * 30;
                        MonthPrincTwo =     (PayTwo - MonthIntTwo);
                        PrincAmtTwo =        (PrincAmtTwo - MonthPrincTwo);
                        if (PrincAmtOne <= 0)
                        {
                        //outputs
                        System.out.printf("Interest payment: $%.2f", MonthIntTwo);
                        System.out.println("");
                        System.out.printf("Principal payment: $%.2f", MonthPrincTwo);
                        System.out.println("");
                        System.out.printf("New Principal Balance: $%.2f", PrincAmtTwo);
                        System.out.println("");
                        LineCount++;
                        }
                        else
                        {
                        System.out.printf("The principal balance is $0.00");
                        System.out.printf("");
                        }

                else if (PrincAmtTwo <= 0)
                        //monthly calculations for first payment amount
                        MonthIntTwo =         (PrincAmtTwo * Rate[0]) / 365 * 30;
                        MonthPrincTwo =     (PayTwo - MonthIntTwo);
                        PrincAmtTwo =        (PrincAmtTwo - MonthPrincTwo);
                        if (PrincAmtOne <= 0)
                        {
                        //outputs
                        System.out.printf("Interest payment: $%.2f", MonthIntTwo);
                        System.out.println("");
                        System.out.printf("Principal payment: $%.2f", MonthPrincTwo);
                        System.out.println("");
                        System.out.printf("New Principal Balance: $%.2f", PrincAmtTwo);
                        System.out.println("");
                        LineCount++;
                        }
                        else
                        {
                        System.out.printf("The principal balance is $0.00");
                        System.out.printf("");

                            System.exit(0);

                    }


            }
        }
User is offlineProfile CardPM

Go to the top of the page

bbq
post 12 Oct, 2008 - 07:12 PM
Post #2


D.I.C Head

Group Icon
Joined: 15 May, 2008
Posts: 185



Thanked 15 times

Dream Kudos: 50
My Contributions


Have a look at your if and else statements and the use of { } as you have done it a rather dodgy way

I fixed some up and it compiles and runs now

java

/*
Week 4: Loan Amortization Calculator
Programmer: Kevin L. Collums
Date: September 22, 2008
Filename: LoanAmort4.java
Purpose: This project will calculate the monthly loan payment for
a loan originated at $200,000.00 at 5.75% over 30 years
*/

import java.io.*;

public class LoanAmort4
{
public static void main(String[] args) throws IOException
{
//Declare variable for loan amount
double LoanAmt = 200000;
double PrincAmtOne, PrincAmtTwo, PrincAmtThree;
double PayOne, PayTwo, PayThree;// creates variable for three different payment amounts
double MonthIntOne, MonthIntTwo, MonthIntThree; //creates variables for three differene interest amounts
double MonthPrincOne, MonthPrincTwo, MonthPrincThree; //creates variables for three different principal payment amounts
PrincAmtOne = LoanAmt;
PrincAmtTwo = LoanAmt;
PrincAmtThree = LoanAmt;
MonthPrincOne = 0;
MonthPrincTwo = 0;
MonthPrincThree = 0;
MonthIntOne = 0;
MonthIntTwo = 0;
MonthIntThree = 0;
int LineCount = 1;




//create loan term array
double [] Term = {84, 180, 360};

//create interest rate array
double [] Rate = {.0535, .055, .0575};

//calculations for each payment amount
PayOne = (LoanAmt * Rate[0]) / (1-Math.pow(1+Rate[0],-Term[0]));
PayTwo = (LoanAmt * Rate[1]) / (1-Math.pow(1+Rate[1],-Term[1]));
PayThree = (LoanAmt * Rate[2]) / (1-Math.pow(1+Rate[2],-Term[2]));

//output
System.out.println("\t\tKevin's Loan Amortization Schedule");
System.out.println();
System.out.println("\t\tLoan Amount: $200,000.00");
System.out.println("\t\tTerm: 7 Years");
System.out.println("\t\tInterest Rate 5.35%");
System.out.printf("\t\tThe monthly payment amount is $%.2f", PayOne);
System.out.println();
System.out.println("\t\tTerm: 15 Years");
System.out.println("\t\tInterest Rate 5.5%");
System.out.printf("\t\tThe monthly payment amount is $%.2f", PayTwo);
System.out.println();
System.out.println("\t\tTerm: 30 Years");
System.out.println("\t\tInterest Rate 5.75%");
System.out.printf("\t\tThe monthly payment amount is $%.2f", PayThree);
System.out.println();






if (PrincAmtOne <= 0)
{
//monthly calculations for first payment amount
MonthIntOne = (PrincAmtOne * Rate[0]) / 365 * 30;
MonthPrincOne = (PayOne - MonthIntOne);
PrincAmtOne = (PrincAmtOne - MonthPrincOne);

if (PrincAmtOne <= 0)
{
//outputs
System.out.printf("Interest payment: $%.2f", MonthIntOne);
System.out.println("");
System.out.printf("Principal payment: $%.2f", MonthPrincOne);
System.out.println("");
System.out.printf("New Principal Balance: $%.2f", PrincAmtOne);
System.out.println("");
LineCount++;
}
else
{
System.out.printf("The principal balance is $0.00");
System.out.printf("");
}
}

else if (PrincAmtTwo <= 0)
{
//monthly calculations for first payment amount
MonthIntTwo = (PrincAmtTwo * Rate[0]) / 365 * 30;
MonthPrincTwo = (PayTwo - MonthIntTwo);
PrincAmtTwo = (PrincAmtTwo - MonthPrincTwo);

if (PrincAmtOne <= 0)
{
//outputs
System.out.printf("Interest payment: $%.2f", MonthIntTwo);
System.out.println("");
System.out.printf("Principal payment: $%.2f", MonthPrincTwo);
System.out.println("");
System.out.printf("New Principal Balance: $%.2f", PrincAmtTwo);
System.out.println("");
LineCount++;
}
else
{
System.out.printf("The principal balance is $0.00");
System.out.printf("");
}
}

else if (PrincAmtTwo <= 0)
{
//monthly calculations for first payment amount
MonthIntTwo = (PrincAmtTwo * Rate[0]) / 365 * 30;
MonthPrincTwo = (PayTwo - MonthIntTwo);
PrincAmtTwo = (PrincAmtTwo - MonthPrincTwo);
if (PrincAmtOne <= 0)
{
//outputs
System.out.printf("Interest payment: $%.2f", MonthIntTwo);
System.out.println("");
System.out.printf("Principal payment: $%.2f", MonthPrincTwo);
System.out.println("");
System.out.printf("New Principal Balance: $%.2f", PrincAmtTwo);
System.out.println("");
LineCount++;
}
else
{
System.out.printf("The principal balance is $0.00");
System.out.printf("");
}

}

System.exit(0);
}
}


The output
QUOTE

Desktop]$ java LoanAmort4
Kevin's Loan Amortization Schedule

Loan Amount: $200,000.00
Term: 7 Years
Interest Rate 5.35%
The monthly payment amount is $10836.01
Term: 15 Years
Interest Rate 5.5%
The monthly payment amount is $11000.72
Term: 30 Years
Interest Rate 5.75%
The monthly payment amount is $11500.00


Hope this helps smile.gif

This post has been edited by bbq: 12 Oct, 2008 - 07:14 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 02:59AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month