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

A library issues books on rental basis at a 2% charge on the cost price of the book per day. As per…

Computer Science20255 marksProgram
A library issues books on rental basis at a 2% charge on the cost price of the book per day. As per the rules of the library, a book can be retained for 7 days without any fine. If the book is returned after 7 days, a fine will also be charged for the excess days as per the chart given below:
Number of excess daysFine per day (Rs.)
1 to 52.00
6 to 103.00
Above 105.00
A super class Library has been defined. Define a sub class Compute to calculate the fine and the total amount. The details of the members of both the classes are given below: Class name : Library Data members/instance variables: name : to store the name of the book author : to store the author of the book p : to store the price of the book (in decimals) Methods / Member functions: Library( ... ) : parameterized constructor to assign values to the data members void show( ) : displays the book details Class name : Compute Data members/instance variables: d : number of days taken in returning the book f : to store the fine (in decimals) Methods / Member functions: Compute( ... ) : parameterized constructor to assign values to the data members of both the classes void fine( ) : calculates the fine for the excess days as given in the table above void show( ) : displays the book details along with the number of days, fine and the total amount to be paid. Total amount is (2% of price of book * total no of days) + fine Assume that the super class Library has been defined. Using the concepts of Inheritance, specify the class Compute giving the details of constructor, void fine ( ) and void show ( ) functions. The super class, main function and algorithm need NOT be written.

Answer

Answer

Official answer key
class Compute extends Library
{
    int d;      // number of days taken in returning the book
    double f;   // fine

    Compute(String name, String author, double p, int d)
    {
        super(name, author, p);
        this.d = d;
        f = 0.0;
    }

    void fine()
    {
        int excess = d - 7;
        if (excess <= 0)
            f = 0.0;
        else if (excess <= 5)
            f = excess * 2.0;
        else if (excess <= 10)
            f = excess * 3.0;
        else
            f = excess * 5.0;
    }

    void show()
    {
        super.show();
        System.out.println("Number of days : " + d);
        System.out.println("Fine           : " + f);
        System.out.println("Total amount   : " + ((0.02 * p * d) + f));
    }
}
Explanation: Compute extends Library, so its constructor calls super(name, author, p) to initialise the inherited data members, then sets d. fine() computes the excess days beyond the free 7-day period and applies the given tiered rate (Rs.2/day for 1-5 excess days, Rs.3/day for 6-10, Rs.5/day beyond 10; 0 if not overdue). show() calls super.show() to print the book details, then prints the days, fine and total amount, calculated as (2% of price x total days) + fine. Tested (with a stub Library class) for price=500.0, d=12: excess=5, so fine=10.0 and total=(0.02x500x12)+10=130.0, matching manual calculation (verified by running the code).
Inheritance, Interfaces and Polymorphism

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