Below i have my Main class and a MortgageCalculation class I am trying to modify my app to do the following:

"Modify the mortgage program to display 3 mortgage loans: 7 year at 4.35%, 15 year at 4.5 %, and 30 year at 4.75%. Use an array for the different loans. Display the mortgage payment amount for each loan. Then, list the loan balance and interest paid for each payment over the term of the loan. Pause the thread to prevent lists from scrolling off the screen. Do not use a graphical user interface. Insert comments in the program to document the program. Create at least one method and move some of your most commonly used code into that method"


My mind is just over complicating this. Please any assistance would be appreciated thanks for your time!

package com.mortgagecalc;
import java.text.DecimalFormat;

public class Main {
	
	public static void main(String[] args) throws InterruptedException {		
		
		//creating a new instance of my Calc class with a custom constructor 
		MortgageCalculation calc = new MortgageCalculation(30, 0.0575, 200000.00);

		//Method call to calc class
		double monthlyPayment = calc.CalculatePayment();
        
		//format currency correctly
		DecimalFormat df = new java.text.DecimalFormat("$,###.00");  		
		
		//out put 
		System.out.println("__________________________________________________________________________\n");
		System.out.println("\nPrincipal Amount: " + df.format (calc.GetPrincipal()));
		System.out.println("Interest rate: " + calc.GetInterestRate());
		System.out.println("Term of loan(number of years): " + calc.GetTerm());
		System.out.println("Monthly payment is: " + df.format(monthlyPayment));
		System.out.println("\n__________________________________________________________________________\n");
	
		System.out.println ("Month \t Payment \0\0\0\0Interest Paid \0\0\0Principal Paid \0\0\0Remaining Balance");
		System.out.println("__________________________________________________________________________");
		
		// two dimensional array for months. 
		double[][] amorization = calc.CalculateAmorization();		
		for (int month = 0; month < amorization.length; month++)
		{		
			System.out.println ("\0\0"+ (int)amorization[month][0] + "\t" + df.format(amorization[month][1])+"\t" + df.format(amorization[month][2])+"\t\t"+ df.format(amorization[month][3])+ "\t\t\0" +df.format(amorization[month][4])); 
			// Display 15 lines at a time 
			 if (month % 15 == 0) {
				 Thread.sleep(600);
				 
		
	     }
		
		}
	}
}
package com.mortgagecalc;

public class MortgageCalculation {
	
	private int term; //how long the loan is in years
	private double interestRate; // loan interest rate
	private double principal; // loan amount
	private int period = 12; // 12 months in a year
		
	// constructor
	public MortgageCalculation(int term, double interestRate, double principal) {
		this.term = term;
		this.interestRate = interestRate;
		this.principal = principal;
	}
	
	public int GetTerm() {
		return this.term;		
	}
	
	public double GetInterestRate() {
		return this.interestRate;
	}
	
	public double GetPrincipal() {
		return this.principal;
	}
	
	//Method to calculate the payment.
	public double CalculatePayment() throws InterruptedException 
	{		
		//Payment calculation 
		return (double) (principal * Math.pow(1 + interestRate / period, term * period) * (interestRate / period) / (Math.pow(1 + interestRate / period, term * period) - 1));			
	}
	
	//Method to calculate Amortization 
	public double[][] CalculateAmorization() throws InterruptedException
	{			
		int totalMonths = this.term * 12;
		double[][] amorization = new double[totalMonths][5];
		double balance = this.principal;
		double monthlyInterest = (1 + this.interestRate / 12);
		double monthlyPayment  = balance * ( monthlyInterest - 1 ) / ( 1 - Math.pow(monthlyInterest,-totalMonths));
		double interest, principalPayment;		
			
		
		for (int month = 0; month < totalMonths; month++)
		{			
			
			interest = balance * ( this.interestRate / 12 );
			principalPayment = monthlyPayment - interest ;
			balance = balance - principalPayment;	

			amorization[month][0] = month + 1;
			amorization[month][1] = monthlyPayment;
			amorization[month][2] = interest;
			amorization[month][3] = principalPayment;
			amorization[month][4] = balance;
		}
		
		
		return amorization;
		
	}
	
}

Yes i need guidance on how to incorporate an array for the interest rate and mortgage term in my above code, I am drawing a blank and i am not sure where to begin.

Thank you for your time.

How do you want to use the elements in these arrays?
Will you be looping through the array or how do you plan to use them?

The way i think i could implement this is doing somthehing like

public MortgageCalc(){
        loanTerm[0]=7;
        loanTerm[1]=15;
        loanTerm[2]=30;
        interest[0] = .0435;
        interest[1] = .045;
        interest[2] = .0475;
    }

I need to re implement this line as well

//creating a new instance of my Calc class with a custom constructor 
		MortgageCalculation calc = new MortgageCalculation(30, 0.0575, 200000.00);

because i will need to hard code this into the array inside the MortgageCalculation Class

But this is where it gets fuzzy i am not sure how to implement this or what direction i need take.

Yes i will need to loop through I believe i can use my current code to do this I just need to fig out how to implement the array.

You have not said how you are going to use the contents of the arrays.
For example if you want to compute the amounts for each of the interest rates, then you would use a loop and index to select the value of the interest rate to be used this time through the loop. Each time around the loop the code would use a different interest rate.

I got figured this out. FYI if someone else has the issue with Arrays. I just added an array for both interestRate and term and then for loop then call the constructor inside the loop :]

package com.mortgagecalc;

import java.text.DecimalFormat;

public class Main {
	
	public static void main(String[] args) throws InterruptedException {		
		
		//creating a new instance of my Calc class with a custom constructor 
		//MortgageCalculation calc = new MortgageCalculation(30, 0.0575, 200000.00);
		  
		//Array to pass in the interest rate
		 double[] interestRate = new double[3];
					   interestRate[0] = .0435;
				       interestRate[1] = .045;
				       interestRate[2] = .0475;
			          
		 
		 
		  int[] term = new int[3];
					   term[0] = 7;
				       term[1] = 15;
				       term[2] = 30;	
			     
		// for loop to pass the interestRate and term data to MortgageCalculation class 
		 for (int i = 0; i < term.length; i++) {
			//creating a new instance of my Calc class with a custom constructor 
			 MortgageCalculation calc = new MortgageCalculation(term[i], interestRate[i], 200000.00);
			 
			
		

		//Method call to calc class
		double monthlyPayment = calc.CalculatePayment();
		
		//format currency correctly
		DecimalFormat df = new java.text.DecimalFormat("$,###.00");  		
			
		//out put 
		System.out.println("\n______________________________________________________________________");
		System.out.println("\nPrincipal Amount: " + df.format (calc.GetPrincipal()));
		System.out.println("Interest rate: " + calc.GetInterestRate());
		System.out.println("Term of loan(number of years): " + calc.GetTerm());
		System.out.println("Monthly payment is: " + df.format(monthlyPayment));
		System.out.println("______________________________________________________________________\n");
		
		 Thread.sleep(1500);
		 
		System.out.println (" Month\t  Payment     Interest Paid  Principal Paid  Remaining Balance");
		System.out.println("______________________________________________________________________\n");
		
		// two dimensional array for months. 
		double[][] amorization = calc.CalculateAmorization();		
		for (int month = 0; month < amorization.length; month++)
		{		
			System.out.println ("   "+ (int)amorization[month][0] + "\t " + df.format(amorization[month][1])+"\t" + df.format(amorization[month][2])+"\t\t"+ df.format(amorization[month][3])+ "\t\t" +df.format(amorization[month][4])); 
			// Display 15 lines at a time 
			 if (month % 15 == 0) {
				 Thread.sleep(600);
	     }
		
		}
	}
}
}

You can initialize an array in one statement:
int[] term = new int[]{7, 15, 30};


One thing your code does that might not make sense: It changes the interest rate and the term at the same time
How many combinations of computations do you want to do?
All terms for each interest rate or only one term / interest rate?

A nested loop: one for interest rate and one for term would do all possible combinations.

Edited 4 Years Ago by NormR1: n/a

You can initialize an array in one statement:
int[] term = new int[]{7, 15, 30};

or shorter:

int[] term = {7,15,30};

...One thing your code does that might not make sense: It changes the interest rate and the term at the same time
How many combinations of computations do you want to do?.

The spec says: "Modify the mortgage program to display 3 mortgage loans: 7 year at 4.35%, 15 year at 4.5 %, and 30 year at 4.75%."

You can initialize an array in one statement:
int[] term = new int[]{7, 15, 30};


One thing your code does that might not make sense: It changes the interest rate and the term at the same time
How many combinations of computations do you want to do?
All terms for each interest rate or only one term / interest rate?

A nested loop: one for interest rate and one for term would do all possible combinations.

Could you please show me with my code what you are talking about ?

stultuske gave an example of a short way to initialize an array in one statement.
Replace this:

loanTerm[0]=7;
    loanTerm[1]=15;
    loanTerm[2]=30;

with what he posted.

int[] loanTerm = {7,15,30};

Edited 4 Years Ago by NormR1: n/a

stultuske gave an example of a short way to initialize an array in one statement.
Replace this:

loanTerm[0]=7;
    loanTerm[1]=15;
    loanTerm[2]=30;

with what he posted.

int[] loanTerm = {7,15,30};

Right im talking about the nested loop.

The outer loop would do the terms, the inner loop would do all the interest rates for the current term the outer loop has chosen.

This question has already been answered. Start a new discussion instead.