0% found this document useful (0 votes)
10 views

Inheritance 2

The document defines two classes - Worker and Wages, with Wages inheriting from Worker. It specifies the constructors, member variables and methods of both classes to calculate wages of a worker using concepts of inheritance.

Uploaded by

suman1986
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Inheritance 2

The document defines two classes - Worker and Wages, with Wages inheriting from Worker. It specifies the constructors, member variables and methods of both classes to calculate wages of a worker using concepts of inheritance.

Uploaded by

suman1986
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

A superclass Worker has been defined to store the details of a worker.

Define a subclass Wages to compute the monthly wages for the worker. The
details/specifications of both the classes are given below:
Class name: Worker
Data Members/instance variables:
Name: to store the name of the worker
Basic: to store the basic pay in decimals
Member functions:
Worker (…): Parameterised constructor to assign values to the instance
variables
void display (): display the worker’s details
Class name: Wages
Data Members/instance variables:
hrs: stores the hours worked
rate: stores rate per hour
wage: stores the overall wage of the worker
Member functions:
Wages (…): Parameterised constructor to assign values to the instance
variables of both the classes
double overtime (): Calculates and returns the overtime amount as
(hours*rate)
void display (): Calculates the wage using the formula wage = overtime
amount + Basic pay and displays it along with the other details

Specify the class Worker giving details of the constructor () and void display
( ). Using the concept of inheritance, specify the class Wages giving details
of constructor ( ), double-overtime () and void display (). The main ()
function need not be written.
Answer:

class wages extends worker


{
int hrs, rate;
double wage
public wage (string n, double b, int h, int r, double w)
{
super (n, b);
hrs = h;
rate = r;
wage = w;
}
public double overtime ()
{
return (hours*rate);
}
public void display ( )
{
super.display ();
wage = overtime () + Basic;
System.out.prinln(wages);
}
}
A superclass Detail has been defined to store the details of a customer.
Define a subclass Bill to compute the monthly telephone charge of the
customer as per the chart is given below:
Number of calls: Rate
1 – 100: Only rental charge
101 – 200: 60 paise per call + rental charge
201 – 300: 80 paise per call + rental charge
Above 300: 1 rupee per call + rental charge
The details of both the classes are given below:
Class name: Detail
Data members/instance variables:
name: to store the name of the customer
address: to store the address of the customer
telno: to store the phone number of the customer
rent: to store the monthly rental charge
Member functions:
Detail (…): parameterized constructor to assign values to data members
void show (): to display the details of the customer
Class name: Bill
Data members/instance variables:
n: to store the number of calls
amt: to store the amount to be paid by the customer
Member functions:
Bill (…): parameterized constructor to assign values to data members of
both classes and to initialize amt = 0.0
void cal(): calculate the monthly telephone charge as per the chart is given
above
void show(): displays the details of the customer and amount to be paid.
Specify the class Detail giving details of the constructor, and void show().

Using the concept of inheritance, specify the class Bill giving details of the
constructor(), void cal() and void show().Assume class Detail has been
defined.
THE MAIN ( ) FUNCTION AND ALGORITHM NEED NOT BE WRITTEN.
Answer:

class Bill extends Detail


{
int n;
double amt;
public Bill (String nm, string ad, long tn, double rn, int, n1)
{
super (nm, ad, tn, rn):
n = n1;
amt = 0.0;
}
void cal ()
{
if (n > = 1 && n < = 100)
{
amt = rent;
}
else if (n > 100 && n < = 200)
{ amt = rent + (n - 100) * 0.60;
}
else if (n > 200 && n< = 300)
{
amt = rent + 100 * 0.60 + (n - 200) * 0.80:
}
else if (n > 300)
{
amt = rent + (0.60 * 100) + (0.80 * 100) + (n - 300) * 100);
}
void show()
{
super.show();
System.out.println("Amount to be paid = "+amt/100 + "Rs");
}
}
A superclass Perimeter has been defined to calculate the perimeter of a
parallelogram. Define a subclass Area to compute the area of the
parallelogram by using the required data members of the superclass. The
details are given below:
Class name: Perimeter
Data members/instance variables:
a: to store the length in decimal
b: to store the breadth in decimal
Member functions:
Perimeter (…): parameterized constructor to assign values to data members
double Calculate(): calculate and return the perimeter of a parallelogram is
2* (length + breadth)
void show(): to display the data members along with the perimeter of the
parallelogram
Class name: Area
Data members/instance variables:
h: to store the height in decimal
area: to store the area of the parallelogram
Member functions:
Area(…): parameterized constructor to assign values to data members of
both the classes
void doarea(): compute the area as (breadth * height)
void show(): display the data members of both classes along with the area
and perimeter of the parallelogram.

Specify the class Perimeter giving details of the constructor (…), double
Calculate and void show (). Using the concept of inheritance, specify the
class Area giving details of the constructor (…), void doarea () and void show
().Assume class Perimeter has been defined. The main function and
algorithm need not be written.
Answer:

class Area extends Perimeter


{
double h;
double area;
Area(double aa, double bb, double cc)
{ super(aa, bb);
h=cc; }
void doarea()
{
area=super.b*h;
}
void show()
{ super, show();
System, out.print("\n Height = " + h);
System.out.print("\n Area = " + area);
}
}

A superclass Stock has been defined to store the details of the stock of a
retail store. Define a subclass Purchase to store the details of the items
purchased with the new rate and updates the stock. Some of the members
of the classes are given below:
Class name: Stock
Data members/instance variables:
item: to store the name of the item
qt: 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:
Stock (…): parameterized constructor to assign values to the data members
void display(): to display the stock details

Class name: Purchase


Data members/instance variables:
pqty: to store the purchased quantity
prate: to store the unit price of the purchased item
Member functions/ methods:
Purchase(…): parameterized constructor to assign values to the data
members of both classes
void update (): to update 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 updation.

Specify the class Stock, giving details of the constructor() and void display().
Using the concept of inheritance, specify the class Purchase, giving details of
the constructor(), void update() and void display().Assume class Stock has
been defined.
The main function and algorithm need not be written.
Answer:

class Purchase extends Stock


{
int pqty;
double prate;
Purchase(String a, double b, double c, int d, double e)
{
super(a, b, c);
pqty=d;
prate=e;
}
void update()
{
qty += pqty;
if(prate!=rate)
rate=prate;
amt = qty * rate;
}
void display()
{
super.display();
update();
super.display();
}
}

You might also like