Computer Science, asked by nithin9213, 9 months ago

Wap a pgm in Java
Please help me

Attachments:

Answers

Answered by QGP
2

Loan Program - Java

Question

Trust Bank charges interest for the vehicle loan as given below:

\begin{tabular}{|l|c|}\cline{1-2} \textbf{\textsf{Number of years}} & \textbf{\textsf{Rate of interest}} \\ \cline{1-2} \sf Up to 5 years & \sf 15\% \\ \sf More than 5 and up to 10 years & \sf 12\% \\ \sf Above 10 years & \sf 10\% \\ \cline{1-2}\end{tabular}

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

 \rule{300}{1}

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

   }

}

 \rule{300}{1}

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

   }

}

 \rule{300}{1}

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.

Attachments:
Similar questions