KnowledgeBoat Logo

Computer Applications

Bank charges interest for the vehicle loan as given below:

Number of yearsRate of interest
Up to 5 years15%
More than 5 and up to 10 years12%
Above 10 years10%

Write a program to model a class with the specifications given below:

Class name: Loan

Data MembersPurpose
int timeTime for which loan is sanctioned
double principalAmount sanctioned
double rateRate of interest
double interestTo store the interest
double amtAmount to pay after given time
Member MethodsPurpose
void getdata()To accept principal and time
void calculate()To find interest and amount.
Interest = (Principal*Rate*Time)/100
Amount = Principal + Interest
void display()To display interest and amount

Java

Java Classes

36 Likes

Answer

import java.util.Scanner;

public class Loan
{
    private int time;
    private double principal;
    private double rate;
    private double interest;
    private double amt;
    
    public void getdata() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter principal: ");
        principal = in.nextInt();
        System.out.print("Enter time: ");
        time = in.nextInt();
    }
    
    public void calculate() {
        if (time <= 5)
            rate = 15.0;
        else if (time <= 10)
            rate = 12.0;
        else
            rate = 10.0;
            
        interest = (principal * rate * time) / 100.0;
        amt = principal + interest;
    }
    
    public void display() {
        System.out.println("Interest = " + interest);
        System.out.println("Amount Payable = " + amt);
    }
    
    public static void main(String args[]) {
        Loan obj = new Loan();
        obj.getdata();
        obj.calculate();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Bank charges interest for the vehicle loan as given below: Write a program to model a class with the specifications given below: Class name: Loan

Answered By

15 Likes


Related Questions