1. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Arrays

Hi can somebody please help me or give me some direction on how I am supposed to do this: Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans:
* - 7 year at 5.35%
* - 15 year at 5.5%
* - 30 year at 5.75%
Use an array for the different loans. Display the mortgage payment amount for each loan.
I dont even know if I am even close with the arrays but I think I need to add formulas to this to. I just am completely lost. Here is what I have. (I know I am not even close) Please help!!

package mortgagecalculator2;

/**
*
* @author
*/
public class MortgageCalculator2 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double loanAmount[]=new double[1];
loanAmount[0]=200000.00;

double loanLengthInMonths[]=new double[3];
loanLengthInMonths[0]=84;
loanLengthInMonths[1]=180;
loanLengthInMonths[2]=360;

double monthlyInterest[]=new double[3];
monthlyInterest[0]=5.35;
monthlyInterest[1]=5.5;
monthlyInterest[2]=5.75;

System.out.println(loanAmount[0]);
System.out.println(loanLengthInMonths[0]);
System.out.println(monthlyInterest[0]);
System.out.println(loanAmount[0]);
System.out.println(loanLengthInMonths[1]);
System.out.println(monthlyInterest[1]);
System.out.println(loanAmount[0]);
System.out.println(loanLengthInMonths[2]);
System.out.println(monthlyInterest[2]);

}

}

2. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## Re: Arrays

Check this:
Java Code:
```import java.util.Scanner;

public class LoanAmortizationShedule {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);;

System.out.println("Enter loan amount (\$):");
double loanAmount = stdin.nextDouble();

System.out.println("Enter number of years:");
int numberOfYears = stdin.nextInt();

System.out.println("Enter interest rate (%):");
double interestRate = stdin.nextDouble();

double monthlyInterestRate = interestRate / 1200;

// Compute mortgage (monthly payment)
double monthlyPayment = loanAmount * monthlyInterestRate /
(1 - (Math.pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));

//Compute total payment
double totalPayment = monthlyPayment * numberOfYears * 12;

//printing table
System.out.printf("Loan Amount: %1.2f\n", loanAmount);
System.out.printf("Number of years: %d\n", numberOfYears);
System.out.printf("Annual Interest Rate: %1.4f%%\n\n", interestRate);

System.out.printf("Monthly payment: %1.2f\n", monthlyPayment);
System.out.printf("Total Payment: %1.2f\n\n", totalPayment);

double balance = loanAmount; //balance initialization
System.out.printf("Payment#     Interest     Principal    Balance\n");

for (int i = 1; i <= numberOfYears * 12; i++) {
double interest = monthlyInterestRate * balance;
double principal = monthlyPayment - interest;
balance = balance - principal;

System.out.printf("%-13d%-13.2f%-13.2f%-1.2f", i, interest, principal, balance);
System.out.println();
}
}
}```
Tips:
* Total payment = monthly payment * 12
* balance(initial) = loan amount
* interest = interest rate / 12 * balance
* principal = monthly payment - interest.
* balance = balance - principal

3. ## Re: Arrays

Originally Posted by diamonddragon
Check this:....
diamonddragon, sure it's a lot easier for any of us to solve his problem in its entirety and post it, but in doing this, what does the original poster gain? Learn how to beg for a solution and then copy it? Not much benefit in that and it will only reinforce the wrong behavior. It's a lot harder and takes a lot more time and involvement to give hints and suggestions so that you gently nudge him in the direction of a solution and then go through his code iterations towards the solution, but in doing the latter, the OP would gain much more.

What I'm getting at is please don't post full solution answers. Again it may be a lot easier to do, but it's just plain wrong.

Edit: Oh, and the OP will often come back and protest, stating that he learns much more from full solutions, but don't believe it as we've seen time and again that it doesn't help as much as doing it the hard way. I think that we have to sweat a little to truly learn.
Last edited by Fubarable; 01-22-2012 at 07:02 AM.

4. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## Re: Arrays

Well that wasn't full post solution, just a hint with same matter, with usefull formulas, and it needs lots of work in order to get final solution.
Anyway, I respect Your suggestions and will have it in mind in the future.
Last edited by diamonddragon; 01-22-2012 at 07:11 AM.

5. Member
Join Date
Dec 2011
Posts
15
Rep Power
0

## Re: Arrays

Although he did post this and I thank you for that i cannot turn that in because it is way to good of code in that my professor would obviously know. I do need to be pointed in the right direction though because i have no idea what or how this code was written. Is any of my code usable?

6. Senior Member
Join Date
Jan 2012
Posts
210
Rep Power
5

## Re: Arrays

This:
double monthlyInterest[]=new double[3];
monthlyInterest[0]=5.35;
monthlyInterest[1]=5.5;
monthlyInterest[2]=5.75;

can be writen as :
double monthlyInterest[] = {5.35, 5.5, 5.75};

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•