In: Computer Science
N/B: THE PROGRAMME SHOULD BE WRITTEN IN JAVA LANGUAGE
CS 202
Part 1. Create a class called Loan. An instance of the class can be used to calculate information about a bank loan. The class should have the following methods: a. A constructor that is passed the amount being borrowed, the annual interest rate and the number of years for repayment. b. A default constructor that initializes the amount being borrowed, the annual interest rate and the number of years for repayment to 0. c. "get" methods (accessors) for the loan amount, the annual interest rate and the number of years for repayment. d. "set" methods (mutators) for the loan amount, the annual interest rate and the number of years for repayment. e. A method that calculates and returns the amount of the monthly payment for the loan. The monthly payment on a loan can be calculated using the following formula: loanAmount * R monthly payment = -------------------- -N 1 - (1 + R ) R = monthly rate of interest for the loan N = number of months to repay the loan f. A method that returns the sum of all payments needed to repay the loan. g. A method that returns the sum of all interest paid on the loan. Part 2. Write an application that tests the Loan class. The program should allow the user to create a loan, enter information about the loan and make changes to the amount of the loan, the interest rate, and the number of years to repay the loan.
Code to copy along with screenshots of code and
output are provided.
Please refer to screenshots to understand the indentation of
code.
If you have any doubts or issues. Feel free to ask in
comments
Please give this answer a like, or upvote. This will be very
helpful for me.
================================================================
Screenshots of "Loan.java" :
Screenshots of "LoanTest.java"
Screenshots of Output :
Code to copy("Loan.java"):
public class Loan {
// member variables
double amount;
double annualInterestRate;
double years;
// default constructor
Loan()
{
// setting all value to 0
this.amount = 0;
this.annualInterestRate = 0;
this.years = 0;
}
// constructor that takes 3 arguments
Loan(double amount, double annualInterestRate,double
years)
{
// setting values accordingly
this.amount = amount;
this.annualInterestRate =
annualInterestRate;
this.years = years;
}
// get method to return amount
double getAmount()
{
return this.amount;
}
// get method to return Annual Interest Rate
double getInterestRate()
{
return
this.annualInterestRate;
}
// get method to return number of years
double getYears()
{
return this.years;
}
// set method for setting the amount
void setAmount(double loanAmount)
{
this.amount = loanAmount;
}
//set method for setting Annual Interest Rate
void setInterestRate(double interestRate)
{
this.annualInterestRate = interestRate;
}
//set method to set number of years
void setYears(double years)
{
this.years = years;
}
// method to return monthly payment
double monthlyPayment()
{
// calculating monthly interest rate
double r = (this.annualInterestRate)/12;
// calculating number of months
double n = 12*this.years;
// amount
double a = this.amount;
// formula for calculating emi
double emi = (a * r/100 * Math.pow((1+(r/100)),n)) /
(Math.pow((1+(r/100)),n)-1);
// returning emi amount
return emi;
}
// function to return sum of all payments needed to repay the
loan
double totalPayment()
{
// calculating total payment
double totalPay = monthlyPayment()*12*10;
return totalPay;
}
// function to return amount to total Interest to be paid
double totalInterestPayment()
{
// calculating total Interest
double totalInterest = totalPayment() -
this.amount;
return totalInterest;
}
}
Code to copy("LoanTest.java"):
public class LoanTest {
public static void main(String args[])
{
// creating instance of loan
Loan loan = new Loan();
// declaring variables
double loanAmount = 700000;
double years = 10;
double interestRate = 8.5;
// setting value of loan Amount
loan.setAmount(loanAmount);
// setting value of number of years
loan.setYears(years);
//setting value of annual Interest rate
loan.setInterestRate(interestRate);
// displaying loan Amount
System.out.println("Loan Amount: "+loanAmount);
// displaying no of years
System.out.println("Years: "+years);
// displaying Annual Interest Rate
System.out.println("Annual Interest Rate:
"+interestRate);
// displaying EMI
System.out.println("\nEvery Month Payment =
"+String.format(" %.2f",loan.monthlyPayment()));
// displaying total amount to be paid
System.out.println("Total Amount to be paid overall =
"+String.format(" %.2f",loan.totalPayment()));
// displaying total Interest to be paid
System.out.println("Total Interest to be paid =
"+String.format(" %.2f",loan.totalInterestPayment()));
}
}
=============================XXX================