Advertisements
Advertisements
Question
A super class EmpSal has been defined to store the details of an employee. Defie a subclass (overtime to compute the total salary of the employee, after adding the overtime amount based on the following criteria.
- If hours are more than 40, then 5000 are added to salary as an overtime amount
- If hours are between 30 and 40 (both inclusive), then 3000 are added to salary as an overtime amount
- If hours are less than 30, then the salary remains unchanged The details of the members of both the classes are given below:
Class name | EmpSal |
Data members/instance variables: | |
empnum | to store the name of the employee |
empcode | integer to store the employee code |
salary | to store the salary of the employee in decimal |
Methods/Member functions: | |
EmpSal(...) | parameterised constructor to assign values to data members |
void show() | to display the details of the employee |
Class name | Overtime |
Data members/instance variables: | |
hours | integer to store overtime in hours |
totsal | to store the total salary in decimal |
Methods/Member functions: | |
Overtime(....) | parameterised constructor to assign values to data members of both the classes |
void calSal() | calculates the total salary by adding the overtime amount to salary as per the criteria given above |
void show() | to display the employee details along with the total salary (salary +overtime amount) |
Assume that the super class EmpSal has been defined. Using the concept of inheritance, specify the class Overtime giving the details of the constructer (...), void calSal() and void show().
The super class, main function and algorithm need NOT be written.
Answer in Brief
Solution
class Overtime extends EmpSal
{
int hours;
double totsal;
public Overtime(String n, int e, double s, int h)
{
super(n,e,s);
hours=h;
}
public void calSal( )
{
if(hours>40)
totsal = salary+5000;
else if(hours>=30)
totsal = salary+3000;
else
totsal = salary;
}
public void show( )
{
super.show();
System.out.println("Total salary:"+totsal);
}
}
shaalaa.com
Base and Derived Classes
Is there an error in this question or solution?