Mortgage calculator

mortgage calculator with 3 mortgage options to calculate

Page 1 of 1

1 Replies - 2295 Views - Last Post: 07 December 2009 - 09:02 PM Rate Topic: -----

#1 sernitydragonfly  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-December 09

Mortgage calculator

Posted 07 December 2009 - 06:11 PM

I am really stuck on the equation to calculate the different mortgage payments. any help would be greatly appreciated.

this section in the code below:
double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);
Payment1=(Payment1/100);
double Payment2= Math.round(loanAmount*(interestMonthly[2]/(1-Math.pow((1 + interestMonthly[2])-loanTerm[2])))*100);
Payment2=(Payment2/100);
double Payment3= Math.round(loanAmount*(interestMonthly[3]/(1-Math.pow((1 + interestMonthly[3])-loanTerm[3])))*100);
Payment3=(Payment3/100);

//packages imported for class and method use
import java.io.*;
import java.util.Date;
import java.text.DecimalFormat;

//class header
public class MortgageCalculatorV3
{

	//method header
	public static void main(String[] args)
	{

			DecimalFormat decimalPlace=new DecimalFormat("$000,000.00");

		//Declares and builds the variables
		double[] loanTerm= new double[3];
			 loanTerm[1]=7*12;
			 loanTerm[2]=15*12;
			 loanTerm[3]=30*12;

		double[] interestMonthly=new double [3];
			interestMonthly[1]=(.0535/12);
			interestMonthly[2]=(.055/12);
			interestMonthly[3]=(.0575/12);

		double loanAmount = 200000;
		double monthlyPayment = 0;


			//Displays the mortgage calculator
			System.out.println("Mortgage Calculator");
			System.out.println("The loan amount is $200,000");
			System.out.println();


				//Declares and builds three new variables
				double loanBalance = 0;
				double interestPaid = 0;
				int lineCount = 20;
				loanBalance = loanAmount - monthlyPayment;

				//Starts loop statement,and declares formula for loan balance and interest paid
				while (loanBalance > 0) {

				//Declares formula
				double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);
					Payment1=(Payment1/100);
				double Payment2= Math.round(loanAmount*(interestMonthly[2]/(1-Math.pow((1 + interestMonthly[2])-loanTerm[2])))*100);
					Payment2=(Payment2/100);
				double Payment3= Math.round(loanAmount*(interestMonthly[3]/(1-Math.pow((1 + interestMonthly[3])-loanTerm[3])))*100);
					Payment3=(Payment3/100);

				//Displays the monthly payment for each option.
				System.out.println("Mortgage payment for 7 years is " + decimalPlace.format(Payment1));
						System.out.println("Mortgage payment for 15 years is " + decimalPlace.format(Payment2));
						System.out.println("Mortgage payment for 30 years is " + decimalPlace.format(Payment3));

				loanBalance = loanBalance - monthlyPayment;
				interestPaid = (monthlyPayment * interestMonthly);

				//Pauses screen
				if (lineCount > 0) {
				lineCount--;
					try {
				Thread.sleep(1500);}
				catch (InterruptedException e) {
				}
				}
				}
			//Stops loop statement
			if (loanBalance <= 0) {
			System.out.println("The loan balance is: $0.00");
}
		}

	}//close method header
//close class header



Is This A Good Question/Topic? 0
  • +

Replies To: Mortgage calculator

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Mortgage calculator

Posted 07 December 2009 - 09:02 PM

You are really looking for problems if you use such statements

double Payment1= Math.round(loanAmount*(interestMonthly[1]/(1-Math.pow((1 + interestMonthly[1])-loanTerm[1])))*100);

and they are impossible to debug..... and think at the poor programmer that will have to maintain your code
split your different steps... something like that: (I am not sure your formula is impossible to read)

double intCalc = 1 + interestMonthly[1]-loanTerm[1];
double pow = 1 - Math.pow(intCalc);
double tmpLoan= loanAmount * pow;
.... you can alternatively print intCalc and pow and tmpLoan
double Payment1 = Math.round(tmpLoan * 100);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1