Solved

# java mortgage code

Posted on 2004-09-22
818 Views
My program below runs great but my teacher does not like the following. He wants me to "use variable name that mean something" instead if
H = P * J;
C = M - H;
Q = P - C;
P = Q;
I asked him what he means and he never answered back. Can any one help me???

//class MortgageCalculator5

import java.math.*; //*loan calculator
import java.text.*; //*formats numbers

class MortgageCalculator5{
public static void main(String arguments[]) throws Exception
{
//Program variables

double Rate[] = {.0575, .0550, .0535};
int N[] = {360, 180, 84};

for (int i=0; i<3; i++) { //step thru array

double P = 200000;//*Loan amount
double J = Rate[i] /12; //*Monthly interest
double discount = (Math.pow((1 + J), N[i]) - 1) / (J * Math.pow((1 + J), N[i]));
double M = P / discount; //* Monthly payment calculation
double H;//*Current monthly interest
double C; //*Principal payment each month minus interest
double Q;//*New balance of loan

//*Output for monthly payment
java.text.DecimalFormat dfm = new java.text.DecimalFormat(",###.00");
System.out.println("Your payment for the " +N[i]+ " month loan is \$" +dfm.format(M));

for (int a=0; a < N[i]; a=a+1) { //*For loop

H = P * J;
C = M - H;
Q = P - C;
P = Q;

if (a % 6 == 0) {
System.in.read(); //causes the program to wait for a keypress
}
System.out.println (" Principal paid this month is \$" + dfm.format(C));
System.out.println (" Interest paid this month is \$" + dfm.format(H));
System.out.println (" New loan balance is \$" + dfm.format(Q));

}
}
}
}

0
Question by:melverc
• 4

LVL 3

Expert Comment

Yup. Instead of using letters, use variable names that actually give some idea of what the variables stores. For instance, for a variable the holds the interest rate, call it interestRate.
0

LVL 3

Expert Comment

P.S. Then you don't need those comments after each variable declaration as the variables are "self documenting" (there's a vocab word... well, phrase ;) ).
0

LVL 3

Accepted Solution

CI-Ia0s earned 500 total points
P.P.S. You can use some of the text in the comments to create the variable names, i.e.:

double P = 200000;//*Loan amount

becomes

double loanAmount = 200000;
0

LVL 3

Expert Comment

:)
0

## Featured Post

### Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.