KnowledgeBoat Logo

Computer Applications

Define a class Interest having the following description:

Class name : Interest

Data MembersPurpose
int pto store principal (sum)
int rto store rate
int tto store time
double interestto store the interest to be paid
double amtto store the amount to be paid
Member functionsPurpose
void input()Stores the principal, rate, time
void cal()Calculates the interest and amount to be paid
void display()Displays the principal, interest and amount to be paid

Write a program to compute the interest according to the given conditions and display the output.

TimeRate of interest
For 1 year6.5%
For 2 years7.5%
For 3 years8.5%
For 4 years or more9.5%

(Note: Time to be taken only in whole years)

Java

Java Classes

60 Likes

Answer

import java.util.Scanner;

public class Interest
{
    private int p;
    private float r;
    private int t;
    private double interest;
    private double amt;
    
    public void input() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter principal: ");
        p = in.nextInt();
        System.out.print("Enter time: ");
        t = in.nextInt();
    }
    
    public void cal() {
        if (t == 1)
            r = 6.5f;
        else if (t == 2)
            r = 7.5f;
        else if (t == 3)
            r = 8.5f;
        else
            r = 9.5f;
            
        interest = (p * r * t) / 100.0;
        amt = p + interest;
    }
    
    public void display() {
        System.out.println("Principal: " + p);
        System.out.println("Interest: " + interest);
        System.out.println("Amount Payable: " + amt);
    }
    
    public static void main(String args[]) {
        Interest obj = new Interest();
        obj.input();
        obj.cal();
        obj.display();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class Interest having the following description: Write a program to compute the interest according to the given conditions and display the output. (Note: Time to be taken only in whole years)

Answered By

25 Likes


Related Questions