InterviewSolution
| 1. |
Wap a pgm in Java Please help me |
|
Answer» ogram - JavaQuestionTrust 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: LoanData members / Instant variables:int time : Time for which the loan is sanctioneddouble principal : Amount sanctioneddouble rate : Rate of interestdouble amt : Amount to pay after given timeInterest = (Principal * Rate * Time) / 100Amount = Principal + InterestMember Methods:void getdata() : To accept principal and timevoid CALCULATE() : To find interest and amountvoid DISPLAY() : To display interest and amountAnswerLoan.javapublic 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.javapublic 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(); }}ExplanationThe 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. |
|