Question: Implement class Loan provided in listing 10.2, page 368 (name it Loan.java). public class Loan { private double annualInterestRate; private int numberOfYears; private double loanAmount;

Implement class Loan provided in listing 10.2, page 368 (name it Loan.java).

public class Loan {

private double annualInterestRate;

private int numberOfYears;

private double loanAmount;

private java.util.Date loanDate;

/** Default constructor */

public Loan() {

this(2.5, 1, 1000);

}

public Loan(double annualInterestRate, int numberOfYears,

double loanAmount) {

this.annualInterestRate = annualInterestRate;

this.numberOfYears = numberOfYears;

this.loanAmount = loanAmount;

loanDate = new java.util.Date();

}

public double getAnnualInterestRate() {

return annualInterestRate;

}

public void setAnnualInterestRate(double annualInterestRate) {

this.annualInterestRate = annualInterestRate;

}

public int getNumberOfYears() {

return numberOfYears;

}

public void setNumberOfYears(int numberOfYears) {

this.numberOfYears = numberOfYears;

}

public double getLoanAmount() {

return loanAmount;

}

public void setLoanAmount(double loanAmount) {

this.loanAmount = loanAmount;

}

public double getMonthlyPayment() {

double monthlyInterestRate = annualInterestRate / 1200;

double monthlyPayment = loanAmount * monthlyInterestRate / (1 - (1 / Math.pow(1 + monthlyInterestRate, numberOfYears)));

return monthlyPayment;

}

public double getTotalPayment() {

double totalPayment = getMonthlyPayment() * numberOfYears * 12;

return totalPayment;

}

public java.util.Date getLoanDate() {

return loanDate;

}

}

Next, implement the test program in listing 10.1, page 367 (name it TestLoan.java).

import java.util.Scanner;

public class TestLoanClass {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("Enter annual interest rate, for example, 8.25: ");

double annualInterestRate = input.nextDouble();

System.out.print("Enter number of years as an integer: ");

int numberOfYears = input.nextInt();

System.out.print("Enter loan amount, for example, 120000.95: ");

double loanAmount = input.nextDouble();

Loan loan =

new Loan(annualInterestRate, numberOfYears, loanAmount);

System.out.printf("The loan was created on %s " +"The monthly payment is %.2f The total payment is %.2f ",loan.getLoanDate().toString(), loan.getMonthlyPayment(),loan.getTotalPayment());

}

}

Now, modify the test program to create a new object, called it carLoan, using these characteristics: interest rate is 8.50%, for 5 years, and borrowed amount is $15,000. Use this new object to test all class methods. Use proper labels in your outputs. Document your code and organize the output using appropriate formatting techniques.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!