Results 1 to 14 of 14
  1. #1
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Need help please

    Hi.... I am in a java course and have no experience whatsoever with programming and am having trouble creating code for a mortgage calculator. It is supposed to be simple but I cannot figure it out for the life of me. I know I am missing something and probably quite a bit of code. Here is what is required and what I have: Calculate and display the monthly payment amount to fully amortize a $200,000.00 loan over a 30 year term at 5.75% interest.


    Java Code:
    public class MortgageCalculator {
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    
        public static void main(String[] args) {
            
        }
        private double numOfPayments;
        
           double loanAmount = 200000;     //This is the loan amount
           double loanLengthInYears = 30;  //This is the length of the loan in years
           double annualInterest = 5.75;   //This is the annual interest rate
           
           //Formuals bieng used are:
           double monthlyInterest = annualInterest/12;
           double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -numOfPayments));
    
           System.out.println("MortgageCalculator");
           System.out.println("Loan Amount: $200,000");
           System.out.println("Loan Length: 30 Years");
           System.out.println("Interest Rate: 5.75%");
    
    }
    Last edited by Eranga; 12-19-2011 at 04:06 AM. Reason: code tags added

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Need help please

    I get cannot find symbol, identifier expected, and illegal start of type errors

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help please

    Could you please post the complete error message?

    Did you read it? There is some details about the line of the source code that error occur and so on.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help please

    Didn't you notice that the print statements and declarations, you have define outside of a method (outside of main method) which is illegal?

  6. #6
    Imbur is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: Need help please

    double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -numOfPayments));

    The last bit -numOfPayments seems that you are attempting to subtract your method from the equation?

    That last subtraction should be the total term in months. You could get this by creating a new variable such as:

    double termTotal = loanLengthInYears * 12;

    This calculates the number of months of the loan- in this case it is 30 years times 12 months per year.

    double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -termTotal));

    Another note is that monthlyInterest needs to be calculated and not put into that equation as a flat 5.75. You need to change this into the correct decimal format.

    This can be done by dividing this 5.75 by 100 which gives us 0.0575 and then by 12 (month). You started to get this with annualInterest.

    double rateCalc = monthlyInterest/100/12;

    Take my advice with a grain of salt, I am new to Java and also in school myself.

  7. #7
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Need help please

    Here is what I have now but still not getting it

    public class MortgageCalculator {
    public static void main(String[] args) {

    }

    double annualInterest = 5.75; //This is the annual interest rate
    double loanAmount = 200000; //This is the loan amount
    double loanLengthInYears = 30; //This is the length of the loan in years


    //Formuals bieng used are:
    double monthlyInterest = annualInterest / 100 /12;
    double loanLengthInMonths = loanLengthInYears * 12;

    double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), loanLengthInYears));


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

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help please

    As I said earlier, you cannot place print statements outside of a method in Java.

  9. #9
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Need help please

    Can you explain that a bit more please... I dont know how to fix that. Ive only had 4 hours of class time so far and am trying to find what you are referring to in my book.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help please

    You have one method in your class.

    Java Code:
    public static void main(String[] args) {
    
    }
    In simple terms method holds the logic in a program. So all what you have to define inside a method. In your case, inside the main method.

    Java Code:
    public class MortgageCalculator {
        NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
     
        public static void main(String[] args) {
             
           double numOfPayments = 0;
         
           double loanAmount = 200000;     //This is the loan amount
           double loanLengthInYears = 30;  //This is the length of the loan in years
           double annualInterest = 5.75;   //This is the annual interest rate
            
           //Formuals bieng used are:
           double monthlyInterest = annualInterest/12;
           double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -numOfPayments));
     
           System.out.println("MortgageCalculator");
           System.out.println("Loan Amount: $200,000");
           System.out.println("Loan Length: 30 Years");
           System.out.println("Interest Rate: 5.75%");
        }
    }

    My suggestion is you to have a look at the basis in Java again. Seems you are messup with all.

  11. #11
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Need help please

    Ok thank you! I think I have most of it figured out but I cant figure out the math portion of it. I am not getting the correct monthly payment. Here is my updated code.

    public class MortgageCalculator {


    public static void main(String[] args) {
    double annualInterest = 5.75; //This is the annual interest rate
    double loanAmount = 200000; //This is the loan amount
    double loanLengthInYears = 30; //This is the length of the loan in years


    //Formuals bieng used are:
    double monthlyInterest = annualInterest /100/12;
    double loanLengthInMonths = loanLengthInYears * 12;

    double monthlyPayment = (loanAmount * monthlyInterest) / (1 * Math.pow((1 + monthlyInterest), loanLengthInYears));


    System.out.println("MortgageCalculator");
    System.out.println("Annual Interest: 5.75%");
    System.out.println("Loan Amount: $200,000");
    System.out.println("Loan Length: 30 years");
    System.out.println(monthlyPayment);
    }
    }

  12. #12
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default Re: Need help please

    It's all about the logic you have implemented. Have a look at it again and check that the correct logic is implemented or not.

  13. #13
    kprofgold is offline Member
    Join Date
    Dec 2011
    Posts
    15
    Rep Power
    0

    Default Re: Need help please

    Ok I got it and the correct monthly payment is being displayed.... One last thing Im trying to do is run a team mates project but it only runs my project which is the main project... How can I run another project. I am using NetBeans

  14. #14
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •