PRASHNIKAप्रश्निका
Back to the paper

A super class Bank has been defined to store the details of the customer in a bank. Define a…

Computer Science20235 marksProgram
A super class Bank has been defined to store the details of the customer in a bank. Define a subclass Interest to calculate the compound interest. The details of the members of both the classes are given below: Class name : Bank Data members/instance variables: name : to store the name of the customer acc_no : integer to store the account number principal : to store the principal amount in decimals Methods / Member functions: Bank( ... ) : parameterized constructor to assign values to the data members void display( ) : to display the customer details Class name : Interest Data members/instance variables: rate : to store the interest rate in decimals time : to store the time period in decimals Methods / Member functions: Interest( ... ) : parameterized constructor to assign values to the data members of both the classes double calculate( ) : to calculate and return the compound interest using the formula $[ \text{CI} = P ( 1 + R/100 )^N - P ]$ where, P is the principal, R is the rate and N is the time void display( ) : to display the customer details along with the compound interest Assume that the super class Bank has been defined. Using the concept of inheritance, specify the class Interest giving the details of the constructor(...), double calculate( ) and void display( ). The super class, main function and algorithm need NOT be written.

Answer

Answer

AI
class Interest extends Bank
{
    double rate, time;

    Interest(String n, int a, double p, double r, double t)
    {
        super(n, a, p);
        rate = r;
        time = t;
    }

    double calculate()
    {
        double ci = principal * Math.pow((1 + rate / 100), time) - principal;
        return ci;
    }

    void display()
    {
        super.display();
        System.out.println("Rate of Interest = " + rate);
        System.out.println("Time Period = " + time);
        System.out.println("Compound Interest = " + calculate());
    }
}
Explanation: Interest extends Bank. Its parameterised constructor passes name, account number and principal up to the superclass via super(n,a,p), then stores rate and time in the subclass. calculate() applies the given compound-interest formula CI = P(1+R/100)^N - P using the inherited principal along with rate and time, and returns the result. display() calls super.display() to reuse the superclass's printing of the customer details, then additionally prints rate, time and the computed compound interest, demonstrating inheritance and method overriding.
Inheritance, Interfaces and Polymorphism

From ISC 2023 Specimen Computer Science Paper 1, question 10.