# Arrays

• 01-22-2012, 05:29 AM
kprofgold
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]);

}

}
• 01-22-2012, 06:52 AM
diamonddragon
Re: Arrays
Check this:
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
• 01-22-2012, 06:57 AM
Fubarable
Re: Arrays
Quote:

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.
• 01-22-2012, 07:08 AM
diamonddragon
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.
• 01-22-2012, 07:19 AM
kprofgold
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?
• 01-22-2012, 06:01 PM
diamonddragon
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};