Write the definition of a class, Loan, to implement the properties of a loan. -
ID: 3547215 • Letter: W
Question
Write the definition of a class, Loan, to implement the properties of a loan.
- Your class should have the instance variables to store the principal, interest rate, and term of the loan in days.
- Add two overloaded constructors.
- Add the get and set methods.
- Add the toString method.
- Add a method that can calculate and return the interest charge.
Write a client program that instantiates a Loan object to calculate the interest charge.
Explanation / Answer
package mypro;
import java.util.Scanner;
import javax.security.sasl.SaslException;
public class Loan {
float principal;
float rate;
int days;
public Loan(float principal, float rate, int days) {
this.principal = principal;
this.rate = rate;
this.days = days;
}
Loan(){
}
public float getPrincipal() {
return principal;
}
public void setPrincipal(float principal) {
this.principal = principal;
}
public float getRate() {
return rate;
}
public void setRate(float rate) {
this.rate = rate;
}
public int getDays() {
return days;
}
public void setDays(int days) {
this.days = days;
}
@Override
public String toString() {
return "Loan [principal=" + principal + ", rate=" + rate + ", days="
+ days + "]";
}
public float cal(){
float charge;
charge = principal *rate * days /365;
return charge;
}
public static void main(String[] args) {
Loan loan = new Loan();
Scanner in = new Scanner(System.in);
do{
System.out.println("Enter loan principal(-1 to end):");
loan.principal = in.nextFloat();
if(loan.principal == -1)
break;
System.out.print("Enter interest rate:");
loan.rate = in.nextFloat();
System.out.print("Enter term of the loan in days:");
loan.days = in.nextInt();
System.out.println("the interest charge is $:"+loan.cal());
}while(loan.principal!=-1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.