‹ Back to the paper
Question 10
5 marks
Computer ScienceISC 2023 Specimen Computer Science Paper 1ProgramInheritance, Interfaces and Polymorphism
Write the program described below.
A super class Bank has been defined to store the details of the customer in a bank. Define a…
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:
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.
| 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 |
Answer
Answer
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.