?
Solved

java mortgage code

Posted on 2004-09-22
4
Medium Priority
?
826 Views
Last Modified: 2008-01-09
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.out.println("Press return to continue");
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
Comment
Question by:melverc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
4 Comments
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 12129534
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

by:CI-Ia0s
ID: 12129540
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

by:
CI-Ia0s earned 2000 total points
ID: 12129546
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

by:CI-Ia0s
ID: 12129603
:)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question