‹ Back to the paper
Question 7
4 marks
Computer ScienceISC 2022 Computer Science - Specimen paper (Semester 2), Paper 1ProgramInheritance, Interfaces and Polymorphism
Write the program described below.
A super class Godown has been defined to store the details of the stock of a retail store. Define a…
A super class Godown has been defined to store the details of the stock of a retail store. Define a subclass Update to store the details of the items purchased with the new rate and update the stock. Some of the members of both the classes are given below:
Class name: Godown
Data members/instance variables:
item: to store the name of the item
qty: to store the quantity of an item in stock
rate: to store the unit price of an item
amt: to store the net value of the item in stock
Member functions/methods:
Godown(...): parameterized constructor to assign value to the data members
void display(): to display the stock details
Class name: Update
Data members/instance variables:
pur_qty: to store the purchase quantity
pur_rate: to store the unit price of the purchased item
Member functions/methods:
Update(...): parameterized constructor to assign values to the data members of both the classes
void update(): to update the stock by adding the previous quantity by the purchased quantity and replace the rate of the item if there is a difference in the purchase rate. Also update the current stock value as: $(quantity * unit\ price)$
void display(): to display the stock details before and after updating
Assume that the super class Godown has been defined. Using the concept of inheritance, specify the class Update giving details of the constructor, void update() and void display().
The super class, main function and algorithm need NOT be written.
Answer
Answer
class Update extends Godown
{
int pur_qty;
double pur_rate;
Update(String i, int q, double r, int pq, double pr)
{
super(i, q, r);
pur_qty = pq;
pur_rate = pr;
}
void update()
{
qty = qty + pur_qty;
if (rate != pur_rate)
rate = pur_rate;
amt = qty * rate;
}
void display()
{
System.out.println("Stock before updating:");
super.display();
update();
System.out.println("Stock after updating:");
super.display();
}
}The constructor passes the stock details to Godown using super(). update() adds the purchased quantity, replaces the rate if it differs and recomputes the stock value as quantity * rate. display() shows the stock before and after the update. (Assumes rate and amt are double and qty is int in Godown.)