KnowledgeBoat Logo

Computer Applications

Define a class with the following specifications:

Class name: Bank

Member variables:
double p — stores the principal amount
double n — stores the time period in years
double r — stores the rate of interest
double a — stores the amount

Member methods:
void accept () — input values for p and n using Scanner class methods only.
void calculate () — calculate the amount based on the following conditions:

Time in (Years)Rate %
Upto 1⁄29
> 1⁄2 to 1 year10
> 1 to 3 years11
> 3 years12

a=p(1+r100)na = p\Big(1+\dfrac{r}{100}\Big)^n

void display () — display the details in the given format.

Principal    Time    Rate    Amount

XXX            XXX     XXX     XXX

Write the main method to create an object and call the above methods.

Java

Java Classes

ICSE Sp 2025

12 Likes

Answer

import java.util.Scanner;

public class Bank
{
    private double p;
    private double n;
    private double r;
    private double a;

    void accept() {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter principal amount: ");
        p = in.nextDouble();

        System.out.print("Enter time period in years: ");
        n = in.nextDouble();
    }
    
    void calculate() {
        if (n <= 0.5) {
            r = 9;
        } else if (n <= 1) {
            r = 10;
        } else if (n <= 3) {
            r = 11;
        } else {
            r = 12;
        }

        a = p * Math.pow(1 + (r / 100), n);
    }
    
    void display() {
        System.out.println("Principal\tTime\tRate\tAmount");
        System.out.println(p + "\t" + n + "\t" + r + "\t" + a);
    }
    
    public static void main(String args[]) {
        Bank b = new Bank();
        b.accept();
        b.calculate();
        b.display();
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class with the following specifications: Class name: Bank Member variables: double p — stores the principal amount double n — stores the time period in years double r — stores the rate of interest double a — stores the amount Member methods: void accept () — input values for p and n using Scanner class methods only. void calculate () — calculate the amount based on the following conditions: void display () — display the details in the given format. Principal Time Rate Amount XXX XXX XXX XXX Write the main method to create an object and call the above methods.

Answered By

8 Likes


Related Questions