DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: simple beginners mort calc- mine wont execute!

1. Registered User
Join Date
Jun 2008
Posts
3

## 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

Assignment:
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
140000 … … … … … … … … …
160000 … … … … … … … … …
180000 … … … … … … … … …
200000 … … … … … … … … …
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);
}
System.out.println("");
}
}
}

2. Registered User
Join Date
Jun 2008
Posts
5

## RE: simple begginers mort calc...

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

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 true...so will never run?
(correction...)
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. Registered User
Join Date
Jun 2008
Posts
3
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. Senior Member
Join Date
Dec 2004
Location
San Bernardino County, California
Posts
1,468
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. Registered User
Join Date
Jun 2008
Posts
3

## 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.security.AccessController.doPrivileged(Native Method)
Process completed.

6. Registered User
Join Date
Mar 2007
Location
Bangalore, India
Posts
247
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
•

 FAQ Latest Articles Java .NET XML Database Enterprise