DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: simple beginners mort calc- mine wont execute!

  1. #1
    Join Date
    Jun 2008

    Question simple beginners mort calc- mine wont execute!

    Hello Java gurus,

    I'm learning java and one of my class assignments is the old "mortgage calculator"... I made code up, compiled okay, but it won't execute. Gives me all sorts of errors. I THINK it's to do with the double vs int vs float... i don't quite understand when to use them. Please help!!! I've been playing with this and can't figure whats erroring. I know its prob something simple so please explain when you write back so i can learn... Thank you! -nika

    The monthly payment on mortgage loan of L dollars, at a rate of interest r is given by Monthly payment = []
    Where N is the number of years of mortgage. Write a program to find the monthly payment for 30 years, for principals from $100,000 through $200,000(with increments of $20,000) and interest rates of 6% through 10% (with increments of 0.5%). Print monthly payment as a whole number (rounded off).
    The output must be a neat table, similar to the following (your answers may be slightly different because of rounding off).
    Principal 6% 6.5% 7% 7.5% 8% 8.5% 9% 9.5% 10%
    100000 600 632 665 699 733 768 804 840 877
    120000 719 758 798 839 880 922 965 1009 1053
    Hint: Use the following idea to compute (1 + r/12)12N
    double temp = 1+r/12;
    double temp1 = 1;
    for (int i = 1; i<=12*N;i++)
    temp1 = temp1*temp;
    The final formula now becomes:
    double mp = (L*(r/12)*temp1)/(temp1-1);

    MY CODE:

    import java.util.*;
    class MortgagePmt {

    public static void main(String args[]) {
    int n = 30;
    System.out.println("Mortgage Monthly Payment");
    System.out.println("Principal 6.0% 6.5% 7.0% 7.5% 8.0% 8.5% 9.0% 9.5% 10.0% ");

    for (int l = 100000; l<=200000; l=l+20000){
    System.out.printf("%9d ",l);
    for(double r = 6.0; r<=10.0; r=r+0.5){
    double temp = 1+r%12;
    double temp1 = 1;
    for (int i = 1; i<=12*n;i++)
    temp1 = temp1*temp;
    double mp = (l*(r%12)*(temp1))/(temp1 - 1);
    System.out.printf("%5.0f", mp);

  2. #2
    Join Date
    Jun 2008

    RE: simple begginers mort calc...

    Per the codes thats listed...
    Do you need to add another } at the end to close the class?

    also your ...
    for (int l = 100000; l<=200000; l=l+20000)

    "L" starts at 100K
    but only will execute until "L" is lessthan or equal to 200K
    Which starts off will never run?
    should be: execute until "L" is equalto or greaterthan 200K ?
    with increments of 20K

    Last edited by LitlGreyCells; 06-13-2008 at 07:07 PM.

  3. #3
    Join Date
    Jun 2008
    I thought the for expression meant that while it was true, it would perform the listed code. So that it would run as long as L was less than or equal to 200,000- and that when L became greater than 200,00 (when condition was FALSE) then it stopped...

    Isn't this correct?

  4. #4
    Join Date
    Dec 2004
    San Bernardino County, California
    nikajoy has that loop control written as he intended: the control expression stops the loop when it evaluates to FALSE not TRUE, Litl (the loop executes while the expression is "true"). So this loop will run until "l" evaluates larger than 200000 [as long as it is less than or equal to 200000].

  5. #5
    Join Date
    Jun 2008

    Question Still not right

    Thank you for your input. the extra } was not the issue. Here is the error message I get if anyone can help me out:

    --------------------Configuration: <Default>--------------------
    java.lang.ClassFormatError: Extra bytes at the end of class file MortgagePmt
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.ClassLoader.loadClassInternal(
    Exception in thread "main"
    Process completed.

  6. #6
    Join Date
    Mar 2007
    Bangalore, India
    Do you get this exception during compile time or runtime? What is the exact command you gave to compile and run it? It seems you are trying to run the wrong file.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center