Wap a pgm in Java
Please help me
Answers
Loan Program - Java
Question
Trust Bank charges interest for the vehicle loan as given below:
Write a program to model a class with the specifications as given below:
Class name: Loan
Data members / Instant variables:
- int time : Time for which the loan is sanctioned
- double principal : Amount sanctioned
- double rate : Rate of interest
- double amt : Amount to pay after given time
Interest = (Principal * Rate * Time) / 100
Amount = Principal + Interest
Member Methods:
- void getdata() : To accept principal and time
- void calculate() : To find interest and amount
- void display() : To display interest and amount
Answer
Loan.java
public class Loan {
int time; //Time for which loan is sanctioned
double principal; //Amount sanctioned
double rate; //Rate of interest
double interest; //To store the interest
double amt; //Amount to pay after given time
void getdata(double p, int t) { //To accept principal ad time
principal = p;
time = t;
}
void calculate() { //To find interest and amount
/* Different case conditions for rate of interest
* Up to 5 years - 15%
* More than 5 and up to 10 years - 12%
* Above 10 years - 10%
*/
if(time <= 5) {
rate = 15;
}
else if(time <= 10) {
rate = 12;
}
else {
rate = 10;
}
//Calculate
interest = (principal * rate * time) / 100;
amt = principal + interest;
}
void display() { //To display interest and amount
System.out.println("Interest = "+interest);
System.out.println("Amount = "+amt);
}
}
TrustBank.java
public class TrustBank {
public static void main(String[] args) {
Loan loan = new Loan(); //Create Loan object
double principal = 10000;
int time = 7;
loan.getdata(principal, time);
loan.calculate();
loan.display();
}
}
Explanation
The Loan.java file contains the code for the Loan class, with the required variables and methods. Code comments describe the purpose of each thing.
The TrustBank.java file contains the main() method which has an example. We first create an object of the Loan class.
Then, we take the Principal as 10,000 and Time as 7 years and pass it on to the getdata() method. Finally, running calculate() and display() gets us our answers.
Screenshots show the code of the two files and a terminal run of the program.