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

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.


The simple interest on a loan is calculated by the formula interest = principal * rate * days / 365; The preceding formula assumes that rate is the annual interest rate, and therefore includes the division by 365 (days). Develop a program that will input principal, rate and days for several loans, and will calculate and display the simple interest for each loan, using the preceding formula. Here is a sample input/output dialog:

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);

   }

}