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 11 of 11

Thread: Mortgage calulator HW problem

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Mortgage calulator HW problem

    Hello,

    I am very new to programming and I have hit a stuck point in my code. This is for a mortgage calculator and we have to use the math.pow method. I am getting results, but the results are wrong. The monthly payment is coming out to big.

    Here is what it should be:

    Enter loan amount: 10000
    Enter rate: 4.5
    Enter number years: 3

    The monthly payment is: $297.47


    Here is what I'm getting.

    Enter loan amount: 10000
    Enter rate: 4.5
    Enter number years: 3

    The monthly payment is: $566.06

    Here is my code. I believe my issue is in the math.pow formula. Can someone help my out?



    Scanner in = new Scanner(System.in); // input object

    double loanAmount; //user input
    double interestRate; // user input
    double yearNumber; // user input
    double monthlyPayment; // answer to math
    double finalPayment; // output
    double monthNumber; // year number converted of months
    double monthlyRate; // converted intersted rate


    System.out.print("Enter loan amount: "); // Shows Line to input loan amount
    loanAmount = Double.parseDouble (in.nextLine()); // gets input number


    System.out.print("Enter rate: "); // Shows line to input rate amount
    interestRate = Double.parseDouble (in.nextLine()); // gets input number

    monthlyRate = interestRate * .01; //convert rate into decimal


    System.out.print("Enter number years: "); //Shows line to input number of years
    yearNumber = Double.parseDouble (in.nextLine()); // gets input number

    monthNumber = yearNumber * 12; // total amount of months



    monthlyPayment = Math.pow(1+ interestRate,monthNumber) / (Math.pow(1+interestRate, monthNumber)-1);


    finalPayment = (loanAmount*monthlyRate*monthlyPayment); // gives the payment


    System.out.printf("\nThe monthly payment is: $%.2f", monthlyPayment); // Shows monthly payment


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Mortgage calulator HW problem

    Please use highlight tags when posting code to preserve formatting. Also, please post your code as an MCVE so that we can copy and paste it to run it. I currently don't see a main method or a class definition.

    What happened when you stepped through this with a debugger, or at least added some print statements, to figure out when the program's execution differed from what you expected? What exactly do you mean when you say the output is wrong? Wrong in what way?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    I'm sorry. I don't understand how to use highlight tags. But the answer should be $297.47 in this example and I am getting $566.06. My answer is incorrect meaning the math must be wrong. I can't figure out where it is wrong.

    --- Update ---

    Here is the entire code.

    package assignment2;

    import java.util.Scanner;

    /**
    *
    *
    */
    public class Assignment2 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here

    Scanner in = new Scanner(System.in); // input object

    double loanAmount; //user input
    double interestRate; // user input
    double yearNumber; // user input
    double monthlyPayment; // answer to math
    double finalPayment; // output
    double monthNumber; // year number converted of months
    double monthlyRate; // converted intersted rate


    System.out.print("Enter loan amount: "); // Shows Line to input loan amount
    loanAmount = Double.parseDouble (in.nextLine()); // gets input number


    System.out.print("Enter rate: "); // Shows line to input rate amount
    interestRate = Double.parseDouble (in.nextLine()); // gets input number

    monthlyRate = interestRate * .01; //convert rate into decimal


    System.out.print("Enter number years: "); //Shows line to input number of years
    yearNumber = Double.parseDouble (in.nextLine()); // gets input number

    monthNumber = yearNumber * 12; // total amount of months



    monthlyPayment = Math.pow(1+ interestRate,monthNumber) / (Math.pow(1+interestRate, monthNumber)-1);


    finalPayment = (loanAmount*monthlyRate*monthlyPayment); // gives the payment


    System.out.printf("\nThe monthly payment is: $%.2f", monthlyPayment); // Shows monthly payment
    System.out.println();

    }

    }

  4. #4
    Junior Member Rods's Avatar
    Join Date
    Sep 2014
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    convert your Annual interest rate into monthly interest rate. Your annual interest rate here is 4.5 which will become 4.5/100=0.045, 0.045/12=0.00375.

  5. #5
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    I convert the annual interest rate with
    monthlyRate = interestRate * .01; //convert rate into decimal

    Will that not do the same as /100?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Mortgage calulator HW problem

    Add some println statements to print out the parts of the expression that the code uses to see which one is not doing what you expect.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member Rods's Avatar
    Join Date
    Sep 2014
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem


  8. #8
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    Do you have an example?

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Mortgage calulator HW problem

    There are 12 months in a year.

    If you eat 120 apples per year, that means you eat 10 apples per month.

    What is your interest per year? Then what is your interest per month?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Sep 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    I read this before and it helped. I missed the part about monthly rate. so I just added this
    yearRate = interestRate * .01; //convert rate into decimals
    ** monthlyRate = yearRate / 12; // convert rate to month rate

    And now it works correctly. Thanks for your help everyone.

  11. #11
    Junior Member Rods's Avatar
    Join Date
    Sep 2014
    Location
    New York
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Mortgage calulator HW problem

    Excellent. Enjoy.

Similar Threads

  1. Monthly mortgage payment calculator
    By iamgonge in forum What's Wrong With My Code?
    Replies: 38
    Last Post: March 24th, 2013, 11:59 AM
  2. Arrays for mortgage calc
    By kprofgold in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 22nd, 2012, 01:01 AM
  3. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  4. Mortgage Calculator Issue
    By coyboss in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 5th, 2011, 09:12 AM
  5. Problem in implementing mortgage calculator
    By American Raptor in forum AWT / Java Swing
    Replies: 1
    Last Post: April 1st, 2009, 02:09 PM