Solved

Posted on 2006-06-01
Medium Priority
386 Views
I finish my code, it run with no error.  Can some one take a look and help me to make it more presentable, with out using loops.

Here is the code

import java.text.DecimalFormat;//This funtion allow to format the numeric output
class MortgageCalculator
{
public static void main(String arguments[])
{

int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };

for (int i=0; i<3; i++)
{

/*Program Variables*/
double I = rates[i];//*Interest rate
double P = 200000;//*Loan amount
double J = I/12; //*Monthy interest
int    N = terms[i];

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation

DecimalFormat twoDigits = new DecimalFormat("\$#,000.00");//Defines pattern for formatting numeric output

/*Output*/
System.out.println("\t Mortgage Calculator");
System.out.println("For a \$200,000.00 Mortgage");
System.out.println("With a interest rate of 5.35% for 7 Years");
System.out.println("With a interest rate of 5.5% for 15 Years");
System.out.println("With a interest rate of 5.75% for 30 Years");
System.out.println ("Your monthly payment is " + twoDigits.format(payment));
}
}
}

0
Question by:martinez1112

LVL 14

Expert Comment

ID: 16814090
> with out using loops

so do each of the 3 calculations separately out of loop
0

Author Comment

ID: 16814118
yes, but I still need to use Array
0

LVL 14

Expert Comment

ID: 16814156
is this OK ?

import java.text.DecimalFormat;

class MortgageCalculator {
public static void main(String arguments[]) {
String msg = "\t Mortgage Calculator\n" +
"For a \$200,000.00 Mortgage\n" +
"With a interest rate of 5.35% for 7 Years\n" +
"With a interest rate of 5.5% for 15 Years\n" +
"With a interest rate of 5.75% for 30 Years\n" +

int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };
DecimalFormat twoDigits = new DecimalFormat("\$#,000.00");

for (int i=0; i<3; i++)
System.out.println(msg + twoDigits.format(calculatePayment(rates[i], terms[i])));
}

static double calculatePayment(double I, int N) {
double P = 200000; //*Loan amount
double J = I/12; //*Monthy interest

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation
return payment;
}
}
0

Author Comment

ID: 16814326
I'm getting the output in the same way as before.  the problem that I have in in the output that is showing

Mortgage Calculator
For a \$200,000.00 Mortgage
With a interest rate of 5.35% for 7 Years
With a interest rate of 5.5% for 15 Years
With a interest rate of 5.75% for 30 Years
payment........

but I'm trying to have it some thing like this

Mortgage Calculator
For a \$200,000.00 Mortgage
With a interest rate of 5.35% for 7 Years
payment......

Mortgage Calculator
For a \$200,000.00 Mortgage
With a interest rate of 5.5% for 15 Years
payment......

Mortgage Calculator
For a \$200,000.00 Mortgage
With a interest rate of 5.75% for 30 Years
payment......
0

LVL 10

Accepted Solution

mukundha_expert earned 664 total points
ID: 16814457
Try this code..

import java.text.DecimalFormat;//This funtion allow to format the numeric output
public class MortgageCalculator
{
public static void main(String arguments[])
{

int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };

for (int i=0; i<3; i++)
{

/*Program Variables*/
double I = rates[i];//*Interest rate
double P = 200000;//*Loan amount
double J = I/12; //*Monthy interest
int    N = terms[i];

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation

DecimalFormat twoDigits = new DecimalFormat("\$#,000.00");//Defines pattern for formatting numeric output

/*Output*/
System.out.println("\t Mortgage Calculator");
System.out.println("For a \$200,000.00 Mortgage");
System.out.println("With a interest rate of "+ rates[i]*100 +"% for "+ terms[i]/12 +"Years");
System.out.println ("Your monthly payment is " + twoDigits.format(payment));
}
}
}
0

LVL 14

Assisted Solution

hoomanv earned 672 total points
ID: 16814489
import java.text.DecimalFormat;

class MortgageCalculator {
public static void main(String arguments[]) {
int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };
DecimalFormat twoDigits = new DecimalFormat("\$#,000.00");

for (int i=0; i<3; i++) {
System.out.println("\t Mortgage Calculator\n" +
"For a \$200,000.00 Mortgage\n" +
"With a interest rate of " + (rates[i] * 100) + " for " + (terms[i] / 12) + "% Years\n" +
"Your monthly payment is " +
twoDigits.format(calculatePayment(rates[i], terms[i])));
}
}

static double calculatePayment(double I, int N) {
double P = 200000; //*Loan amount
double J = I/12; //*Monthy interest

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation
return payment;
}
}
0

LVL 23

Assisted Solution

basicinstinct earned 664 total points
ID: 16814496
Like this...?

import java.text.DecimalFormat;//This funtion allow to format the numeric output
class MortgageCalculator
{
public static void main(String arguments[])
{

int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };

for (int i=0; i<terms.length; i++)
{
/*Program Variables*/
double I = rates[i];//*Interest rate
double P = 200000;//*Loan amount
double J = I/12; //*Monthy interest
int    N = terms[i];

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation

DecimalFormat twoDigits = new DecimalFormat("\$#,000.00");//Defines pattern for formatting numeric output

/*Output*/
System.out.println("Mortgage Calculator");
System.out.println("For a \$200,000.00 Mortgage");
System.out.println("With a interest rate of " + (rates[i] * 100) + "% for " + (terms[i] / 12) + " Years");
System.out.println ("Your monthly payment is " + twoDigits.format(payment));
System.out.println();
}
}
}
0

LVL 14

Expert Comment

ID: 16814652
apparently all the answers are the same :)

-- for java 1.5, without DecimalFormat by using System.out.printf()

class MortgageCalculator {
public static void main(String arguments[]) {
String msg = "\t Mortgage Calculator\n" +
"For a \$200,000.00 Mortgage\n" +
"With a interest rate of %.2f%% for %d Years\n" +
"Your monthly payment is %,.2f \n\n";

int[] terms = { 7*12, 15*12, 30*12 };
double[] rates = { 0.0535, 0.055, 0.0575 };

for (int i=0; i<3; i++)
System.out.printf(msg, rates[i] * 100, terms[i] / 12, calculatePayment(rates[i], terms[i]));
}

static double calculatePayment(double I, int N) {
double P = 200000; //*Loan amount
double J = I/12; //*Monthy interest

//calculate monthly payment
double discount = (Math.pow((1 + J), N) -1) / (J * Math.pow((1 + J), N));
double payment = P / discount; //*Rate Calculation
return payment;
}
}
0

Author Comment

ID: 16814661
Thanks to all. I really apresiate your help

0

## Featured Post

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
###### Suggested Courses
Course of the Month17 days, left to enroll