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

A super class Demand has been defined to store the details of the demands for a product. Define a…

Computer Science20235 marksProgram
A super class Demand has been defined to store the details of the demands for a product. Define a subclass Supply which contains the production and supply details of the products. The details of the members of both the classes are given below:
Class name:Demand
**Data members/instance variables:**
pid:string to store the product ID
pname:string to store the product name
pdemand:integer to store the quantity demanded for the product
**Methods / Member functions:**
Demand(...):parameterised constructor to assign values to the data members
void display( ):to display the details of the product
<br>
Class name:Supply
**Data members/instance variables:**
pproduced:integer to store the quantity of the product produced
prate:to store the cost per unit of the product in decimal form
**Methods / Member functions:**
Supply(...):parameterised constructor to assign values to the data members of both the classes
double calculation( ):returns the difference between the amount of demand (rate $\times$ demand) and the amount produced (rate $\times$ produced)
void display( ):to display the details of the product and the difference in amount of demand and amount of supply by invoking the method calculation()
Assume that the super class Demand has been defined. Using the concept of inheritance, specify the class Supply giving the details of the constructor(...), double calculation( ) and void display( ). The super class, main function and algorithm need NOT be written.

Answer

Answer

AI
class Supply extends Demand
{
    int pproduced;   // quantity produced
    double prate;    // cost per unit

    // parameterised constructor for both classes
    Supply(String id, String name, int d, int p, double r)
    {
        super(id, name, d);
        pproduced = p;
        prate = r;
    }

    // difference between amount of demand and amount produced
    double calculation()
    {
        return (prate * pdemand) - (prate * pproduced);
    }

    // displays all details and the difference
    void display()
    {
        super.display();
        System.out.println("Quantity produced: " + pproduced);
        System.out.println("Rate per unit: " + prate);
        System.out.println("Difference in amount of demand and supply: " + calculation());
    }
}
Supply inherits pid, pname and pdemand from Demand. Its constructor passes the first three values to the super class constructor with super(...). display() overrides the super class method: it first calls super.display() for the product details, then prints its own data and the value returned by calculation(). Tested with a small Demand class: Supply("P101", "Pen", 500, 420, 12.5) printed the product details and "Difference in amount of demand and supply: 1000.0" ($12.5 \times 500 - 12.5 \times 420 = 1000$).
Inheritance, Interfaces and Polymorphism

From ISC 2023 Computer Science Paper 1, question 10.