The simple interest on a loan is calculated by the formula interest = principal
ID: 3547662 • Letter: T
Question
The simple interest on a loan is calculated by the formulainterest = principal * rate * day / 365.
The proceeding formula assumes that rate is the annual interest rate, and therefore includes the divison 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 proceeding formula.
Write a definition of a class, Loan, to implement the properties of a loan.
The simple interest on a loan is calculated by the formula
interest = principal * rate * day / 365.
The proceeding formula assumes that rate is the annual interest rate, and therefore includes the divison 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 proceeding formula.
Write a definition of a class, Loan, to implement the properties of a loan.
Explanation / Answer
/*************************** interestcal.java **************************/
package interestcalc;
import java.util.Scanner;
public class Interestcalc {
static Scanner console = new Scanner(System.in);
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
double principal,rate,interest;
int days;
for(int x = 0;x < 1000;x++){
System.out.printf("%nEnter loan principal (-1 to end):");
principal = console.nextDouble();
if(principal != -1){
System.out.printf("Enter interest rate:");
rate = console.nextDouble();
System.out.printf("Enter term of the loan in days:");
days = console.nextInt();
Loan loan = new Loan(principal, rate, days);
System.out.printf("The interest charge is $%.2f%n", loan.getInterest());
}
else{
return;
}
}
}
}
/************* Loan.java *****************/
package interestcalc;
public class Loan {
double principal;
double rate;
int term;
public Loan() {
this.principal = 0;
this.rate = 0;
this.term = 0;
}
public Loan(double principal, double rate, int term) {
this.principal = principal;
this.rate = rate;
this.term = term;
}
public double getPrincipal() {
return principal;
}
public void setPrincipal(double principal) {
this.principal = principal;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public int getTerm() {
return term;
}
public void setTerm(int term) {
this.term = term;
}
@Override
public String toString() {
return "Loan{" + "principal=" + principal + ", rate=" + rate + ", term=" + term + '}';
}
public double getInterest() {
double interest = 0;
interest = principal * rate * term;
return interest/365;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.