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

A super class Product has been defined to store the details of a product sold by a wholesaler to a…

Computer Science20175 marksProgram
A super class Product has been defined to store the details of a product sold by a wholesaler to a retailer. Define a sub class Sales to compute the total amount paid by the retailer with or without fine along with service tax. Some of the members of both the classes are given below: Class name : Product Data member/instance variable: name : stores the name of the product code : integer to store the product code amount : stores the total sale amount of the product (in decimals) Member functions/methods: Product(String n, int c, double p) : parameterized constructor to assign data members name=n, code=c and amount = p void show( ) : displays the details of the data members Class name : Sales Data member/instance variable: day : stores number of days taken to pay the sale amount tax : to store the service tax (in decimals) totamt : to store the total amount (in decimals) Member functions/methods: Sales(…) : parameterized constructor to assign values to data members of both the classes void compute( ) : calculates the service tax @ 12·4% of the actual sale amount calculates the fine @ 2·5% of the actual sale amount only if the amount paid by the retailer to the wholesaler exceeds 30 days calculates the total amount paid by the retailer as (actual sale amount + service tax + fine) void show( ) : displays the data members of super class and the total amount Assume that the super class Product has been defined. Using the concept of inheritance, specify the class Sales giving the details of the constructor(…), void compute( ) and void show( ). The super class, main function and algorithm need NOT be written.

Answer

Answer

AI
class Sales extends Product
{
    int day;
    double tax, totamt;

    Sales(String n, int c, double p, int d)
    {
        super(n, c, p);   // assigns name, code and amount of the super class
        day = d;
        tax = 0.0;
        totamt = 0.0;
    }

    void compute()
    {
        double fine = 0.0;
        tax = amount * 12.4 / 100;          // service tax @ 12.4%
        if (day > 30)
            fine = amount * 2.5 / 100;      // fine @ 2.5% only if paid after 30 days
        totamt = amount + tax + fine;
    }

    void show()
    {
        super.show();
        System.out.println("Total amount: " + totamt);
    }
}
Explanation: the constructor uses super(n, c, p) to set the data members of Product and then sets day. compute() calculates the service tax, the fine only when day > 30, and the total amount as amount + tax + fine. show() calls the super class show() and then displays totamt. Tested by running with amount 1000: 25 days gives total 1124.0 and 40 days gives total 1149.0.
Inheritance, Interfaces and Polymorphism

From ISC 2017 Computer Science Paper 1, question 10.