Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Incorrect results

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Incorrect results

    Hi I was hoping to get some help with my code. I think the formulas may be wrong and I just cant figure it out. My results are incorrect. Here is the assignment with my code. 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




    public class MortgageCalculator3 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    double loanAmount = 200000; //This is the loan amount
    double monthlyPayment[] = new double[3];//creates the new variable
    monthlyPayment[0] = (loanAmount * 5.35 * (Math.pow((1 + 5.35 / 1200), 84))) / (1200 * (Math.pow((1 + 5.35 / 1200), 84) - 1));//creates the formula for 7 year loan
    monthlyPayment[1] = (loanAmount * 5.5 * (Math.pow((1 + 5.5 / 1200), 180))) / (1200 * (Math.pow((1 + 5.5 / 1200), 180) - 1));//creates the formula for 15 year loan
    monthlyPayment[2] = (loanAmount * 5.75 * (Math.pow((1 + 5.75 / 1200), 360))) / (1200 * (Math.pow((1 + 5.75 / 1200), 360) - 1));//creates the formula for 30 year loa

    double interestRate[] = new double[3];
    interestRate[0] = 5.75;
    interestRate[1] = 5.5;
    interestRate[2] = 5.35;

    double loan[] = new double[1];
    loan[0] = 200000.00;

    System.out.println("MortgageCalculator");
    System.out.println("Loan A");
    System.out.println("Loan Amount: $200,000");
    System.out.println("Annual Interest: 5.75%");
    System.out.println("Loan Length: 30 years");
    System.out.println("Monthly Payment: " + monthlyPayment[2]);

    double currentLoanAmount;
    currentLoanAmount = loanAmount;
    for (int i = 1; i <= 360; i++) { //begins the loop process for 360 payments
    if (loanAmount > currentLoanAmount) {
    currentLoanAmount = 0;
    }

    double interestPayment = loanAmount * currentLoanAmount;
    currentLoanAmount -= (loanAmount - interestPayment);
    System.out.println("For payment #" + i + ", The Interest Payment is $" + (interestPayment));//displays the results of the interest payment
    System.out.println("For payment #" + i + ", The Remaining Balance is $" + (currentLoanAmount));//displays the results of the remaining balance
    currentLoanAmount = 0;
    }
    System.out.println("Loan B");
    System.out.println("Loan Amount: $200,000");
    System.out.println("Annual Interest: 5.55%");
    System.out.println("Loan Length: 15 years");
    System.out.println("Monthly Payment: " + monthlyPayment[1]);


    currentLoanAmount = loanAmount;
    for (int i = 1; i <= 360; i++) { //begins the loop process for 360 payments
    if (loanAmount > currentLoanAmount) {
    currentLoanAmount = 0;
    }

    double interestPayment = loanAmount * currentLoanAmount;
    currentLoanAmount -= (loanAmount - interestPayment);
    System.out.println("For payment #" + i + ", The Interest Payment is $" + (interestPayment));//displays the results of the interest payment
    System.out.println("For payment #" + i + ", The Remaining Balance is $" + (currentLoanAmount));//displays the results of the remaining balance
    currentLoanAmount = 0;
    }
    System.out.println("Loan C");
    System.out.println("Loan Amount: $200,000");
    System.out.println("Annual Interest: 5.35%");
    System.out.println("Loan Length: 7 years");
    System.out.println("Monthly Payment: " + monthlyPayment[0]);


    currentLoanAmount = loanAmount;
    for (int i = 1; i <= 360; i++) { //begins the loop process for 360 payments
    if (loanAmount > currentLoanAmount) {
    currentLoanAmount = 0;
    }
    double interestPayment = loanAmount * currentLoanAmount;
    currentLoanAmount -= (loanAmount - interestPayment);
    System.out.println("For payment #" + i + ", The Interest Payment is $" + (interestPayment));//displays the results of the interest payment
    System.out.println("For payment #" + i + ", The Remaining Balance is $" + (currentLoanAmount));//displays the results of the remaining balance
    currentLoanAmount = 0;
    }

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Incorrect results

    Cross posted at: Incorrect formulas

Similar Threads

  1. Java Calculation Incorrect
    By cow23 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 14th, 2011, 08:09 AM
  2. Incorrect Key Input
    By risen375 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 01:17 PM
  3. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  4. [SOLVED] logic error: cpu assigning incorrect values in for loop
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 25th, 2010, 08:13 PM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM