In: Computer Science
Modify the mortgage program to display 3 mortgage loans: 7 years at 5.35%, 15 year at 5.5%, and 30 years at 5.75%. Use an array for the different loans. Display the mortgage payment amount for each loan. Do no use graphical user interface. Insert comments in the program to document the program.
***********Please look at my code so far. Still having trouble with arrays. Want to know if I'm going the right path********
import java.util.Scanner;
public class MortPay2
{
public static void main(String args[])
{
//declaration section
double principal, monthlyPayment;
int choice;
Scanner input = new Scanner(System.in);
int term [] = {84, 180, 360}; //term in
months
double rate [] = {.0044, .0046, .0048};//rate in
decimal format
//display section
System.out.println("Please select from 3 loan
options.");
System.out.println("1. Loan 1: 7 year term at 5.35%
interest.");
System.out.println("2. Loan 2: 15 year term at 5.5%
interest.");
System.out.println("3. Loan 3: 30 year term at 5.75%
interest.");
choice = input.nextInt();
if (choice ==1)
{
System.out.println("You have chosen
Loan 1.");
System.out.println("Please enter
the loan amount: $");
principal = input.nextDouble();
for ( int i = 0; i<3;
i++)
{
monthlyPayment =
principal * rate[0] / (1.0 - Math.pow (rate[0] + 1),
-term[0]));
System.out.println("Total monthly mortgae payment is $%.2f%n",
monthlyPayment);
}
}
}
}
JAVA PROGRAM
/////////////////////////////MortPay2.java////////////////////////////
import java.util.Scanner;
public class MortPay2{
public static void main(String args[]){
//declaration section
double principal, monthlyPayment;
int choice;
Scanner input = new Scanner(System.in);
int term [] = {84, 180, 360}; //term in months
double rate [] = {.0044, .0046, .0048};//rate in
decimal format; stored as (yearlyRate/12)
//display section
System.out.println("Please select from 3 loan
options.");
System.out.println("1. Loan 1: 7 year term at 5.35%
interest.");
System.out.println("2. Loan 2: 15 year term at 5.5%
interest.");
System.out.println("3. Loan 3: 30 year term at 5.75%
interest.");
System.out.println("4. Display all Loans");//added
this option to show all options at a time
choice = input.nextInt();
int index = 0,termInMonth;
double mortgageRate;
if (choice ==1){
System.out.println("You have chosen Loan 1.");
index =0; //as we need to consider 0th index element
from both term and rate array
}else if(choice ==2){
System.out.println("You have chosen
Loan 2.");
index =1; //as we need to consider 1st index element
from both term and rate array
}else if(choice ==3){
System.out.println("You have chosen
Loan 3.");
index =2; //as we need to consider 2nd index element
from both term and rate array
}else {
System.out.println("You have chosen
to display all loans.");
index = -1; //this is set in this
way to show all loans
}
System.out.println("Please enter the loan amount: $");
principal = input.nextDouble();
if(index==-1){//if index = -1 show all loans
for(int i = 0; i < 3 ; i++){//loop from i = 0 to
2
//calculate monthly payment for ith
index loan
monthlyPayment =
principal * rate[i]/ (1.0 - Math.pow (rate[i] + 1,
-term[i]));
//print the
monthly payment
System.out.printf("\nLoan %d : Total monthly mortgae payment is
$%.2f%n", i+1,monthlyPayment);
}
}else{//otherwise display for specific loan
mortgageRate = rate[index];//get
rate for the index
termInMonth = term[index];//get
term for the index
//calculate monthly payment
monthlyPayment = principal *
mortgageRate/ (1.0 - Math.pow (mortgageRate+ 1,
-termInMonth));
//print it
System.out.printf("\nLoan %d :
Total monthly mortgae payment is $%.2f%n",
index+1,monthlyPayment);
}
input.close();//close scanner
System.out.println("\nThank you!");
}
}
=====================================
OUTPUT
====================================
Run1
Please select from 3 loan options.
1. Loan 1: 7 year term at 5.35% interest.
2. Loan 2: 15 year term at 5.5% interest.
3. Loan 3: 30 year term at 5.75% interest.
4. Display all Loans
1
You have chosen Loan 1.
Please enter the loan amount: $
15000
Loan 1 : Total monthly mortgae payment is $213.99
Thank you!
Run2
Please select from 3 loan options.
1. Loan 1: 7 year term at 5.35% interest.
2. Loan 2: 15 year term at 5.5% interest.
3. Loan 3: 30 year term at 5.75% interest.
4. Display all Loans
2
You have chosen Loan 2.
Please enter the loan amount: $
16000
Loan 2 : Total monthly mortgae payment is $130.90
Thank you!
Run3
Please select from 3 loan options.
1. Loan 1: 7 year term at 5.35% interest.
2. Loan 2: 15 year term at 5.5% interest.
3. Loan 3: 30 year term at 5.75% interest.
4. Display all Loans
3
You have chosen Loan 3.
Please enter the loan amount: $
17000
Loan 3 : Total monthly mortgae payment is $99.32
Thank you!
Run4
Please select from 3 loan options.
1. Loan 1: 7 year term at 5.35% interest.
2. Loan 2: 15 year term at 5.5% interest.
3. Loan 3: 30 year term at 5.75% interest.
4. Display all Loans
4
You have chosen to display all loans.
Please enter the loan amount: $
20000
Loan 1 : Total monthly mortgae payment is $285.32
Loan 2 : Total monthly mortgae payment is $163.63
Loan 3 : Total monthly mortgae payment is $116.84
Thank you!