Implement class Loan provided in listing 10.2, page 368 (name it Loan.java). pub
ID: 3767835 • Letter: I
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;
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. modifiy the tst program to creat a new object, called carLoan, using these characteristics: interest rate is 8.50, for 5 years, and borrowed amount is $15,000. Use this new object to to test all class methods.Use proper labels in your outputs. Document your code, and organize and space the outputs properly. use escaoe characters and formatting objects when applicable.
Explanation / Answer
The modified test class is herewith. If you have any further queries, just get back to me.
//(Next, implement the test program in listing 10.1, page 367(name it TestLoan.java).
import java.util.Scanner;
public class TestLoan {
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());
*/
Loan carLoan = new Loan(); //Creating a car loan with default constructor.
//8.5% interest p.a., for 5 years, with a loan amount of $15000.
//System.out.printf("The loan was created on %s " +"The monthly payment is %.2f The total payment is %.2f ",carLoan.getLoanDate().toString(), carLoan.getMonthlyPayment(),carLoan.getTotalPayment()); //This shows the default values.
carLoan.setAnnualInterestRate(8.5);//Sets the annual interest rate to 8.5%.
carLoan.setNumberOfYears(5); //Sets the number of years to 5.
carLoan.setLoanAmount(15000); //Sets the loan amount to $15,000.
System.out.println("Car Loan Details: ");
System.out.printf("Initial loan amount : %.2f ", carLoan.getLoanAmount());
System.out.println("Loan Tenure(In Years) : "+carLoan.getNumberOfYears());
System.out.println("Interest Rate : "+carLoan.getAnnualInterestRate()+"%");
System.out.println("Loan Creation Date : "+carLoan.getLoanDate().toString());
System.out.printf("Monthly Instalment : %.2f ", carLoan.getMonthlyPayment());
System.out.printf("Total Amount Paid : %.2f ", carLoan.getTotalPayment());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.