Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 1 Lab6_Methods.java contains instructions for completing the lab. Specifica

ID: 3674605 • Letter: P

Question

Part 1 Lab6_Methods.java contains instructions for completing the lab. Specifically, you will create a program that can be used by a Bank to calculate the monthly and total payment on a house mortgage loan, given the interest rate, length, and amount of the loan. Look over the main method. You will see that it basically is an outline of methods calls 1) 3 input methods are used to read in values for interest rate, loan amount, and term (years) of the loan. 2) A method is called to calculate the monthly payment for the loan. 3) Calculate the total paid and the total interest paid on the loan. 4) A method is called to display the monthly payment, the total amount paid, and the total amount of interest paid . Notice which methods are passing values INTO a method (calcMonthlyPayment, displayOutput) while others pass nothing to the method but the return value is used to assigned into a variable that is LOCAL to main (getlnterest, getLoanAmount, getYears) You will be completing the following methods 1) Complete the getlnterest method 2) Complete the getLoanAmount method 3) Complete the getYears method 4) Complete the calcMonthlyPayment method (formula given on the next page) 5) Complete the displayOutput method

Explanation / Answer

import java.util.Scanner;

/*
* Lab6_Methods.java
* Original Author: Dr. D. Lang
* Modified by: (Your name here)
* Course: CIS 200
* Lab Section: (Insert your lab section day/time here)

PURPOSE- This application will calculate the monthly and total payment on a loan,
given the interest rate, length, and amount of the loan.

Divide and Conquer using submodules!
...Divided into Input, Calculation, and Output submodules
*/

public class Lab6_Methods {
   static Scanner scanner = new Scanner(System.in);

   public static void main(String[] args) {

       double interestRate; // holds MONTHLY interest rate
       double loanAmount; // Amount of the Loan
       int years; // Length of the Loan
       double monthlyPayment; // Monthly Payment required

       // -------------------- Get User-entered Input ------------------
       // call submodule defined below main
       interestRate = getInterest();
       loanAmount = getLoanAmount();
       years = getYears();

       // ----------------------- Calculate the Payments ------------------
       // call method to calculate monthly payment, passing the loan amount,
       // interest rate, and how years of the loan as ARGUMENTS
       // Must pass in the correct order!
       monthlyPayment = calcMonthlyPayment(years, loanAmount, interestRate);

       // calculate the total amount to be paid on the loan
       double totalPaid = monthlyPayment * years * 12;

       // calculate the total interest to be paid on the loan
       double interestPaid = totalPaid - loanAmount;

       // ----------------------- Display Results
       // ------------------------------
       // call submodule defined below main ... pass needed info as ARGUMENTS
       displayOutput(monthlyPayment, totalPaid, interestPaid);

   } // end main method

   // -------------------- Input Methods -----------------------------------

   // getInterest:Gets a YEARLY interest rate from the user,
   // converts and returns a monthly interest (double)
   public static double getInterest() {
       double interest;
       do {
           System.out.print("Enter Yearly Interest rate(Ex 8.25):");
           interest = scanner.nextDouble();
           if (interest >= 3 && interest <= 12)
               break;
       } while (true);
       return interest;
       // Your code goes here ... use cut and paste as much as possible!

   } // end getInterest()

   // getYears: Gets from the user and returns the term of the loan
   // (number of years) as an int
   public static int getYears() {
       // Your code goes here ... use cut and paste as much as possible!
       int years;
       do {
           System.out.print("Enter number of years(as an integer):");
           years = scanner.nextInt();
           if (years >= 1 && years <= 50)
               break;
       } while (true);
       return years;

   } // end getYears()

   // getLoanAmount: Gets from the user and returns the loan amount (double)
   public static double getLoanAmount() {
       // Your code goes here ... use cut and paste as much as possible!
       double loanAmount;
       do {
           System.out.print("Enter Loan amount(120000.95):");
           loanAmount = scanner.nextDouble();
           if (loanAmount >= 50000 && loanAmount <= 1000000)
               break;
       } while (true);
       return loanAmount;
   } // end getLoanAmount

   // ----------------------- Calculate Monthly Payments ------------------
   // calcMonthlyPayment: Calculates and returns the monthly payment (double)
   // Requires passing in the loan amount, interest rate, and term of the loan
   // Reminder: parameter names do NOT need to match the argument names
   public static double calcMonthlyPayment(int term, double amount, double rate) {
       // Your code goes here ... use cut and paste as much as possible!

       // Monthly intertest rate
       rate = rate / 100 / 12;

       // Term in months
       term = term * 12;

       double payment = (amount * rate) / (1 - Math.pow(1 + rate, -term));
       // round to two decimals
       payment = (double) Math.round(payment * 100) / 100;
       return payment;
   } // end calcMonthlyPayment

   // ----------------------- Display Output ------------------------------
   // displayOutput: Displays the monthly payment, total interest paid and
   // the total amound paid on the loan. (Nothing is returned)
   // Requires passing in monthlyPayment, totalPaid, interestPaid
   public static void displayOutput(double monthlyPayment, double totalPaid,
           double interestPaid) {
       // Your code goes here ... use cut and paste as much as possible!
       System.out.println("The monthly payment is $" + monthlyPayment);
       System.out.println("The total paid on loan is $" + totalPaid);
       System.out.println("The total interest paid on loan is $"
               + interestPaid);

   } // end displayOutput

} // end class

OUTPUT:

Test1:
Enter Yearly Interest rate(Ex 8.25):5
Enter Loan amount(120000.95):100000
Enter number of years(as an integer):25
The monthly payment is $584.59
The total paid on loan is $175377.0
The total interest paid on loan is $75377.0


Test2:
Enter Yearly Interest rate(Ex 8.25):2
Enter Yearly Interest rate(Ex 8.25):4
Enter Loan amount(120000.95):15000
Enter Loan amount(120000.95):100000
Enter number of years(as an integer):54
Enter number of years(as an integer):25
The monthly payment is $527.84
The total paid on loan is $158352.0
The total interest paid on loan is $58352.0