Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 10

5 marks

Computer ScienceISC 2024 Computer Science Paper 1ProgramInheritance, Interfaces and Polymorphism

Write the program described below.

A super class EmpSal has been defined to store the details of an employee. Define a subclass…

A super class EmpSal has been defined to store the details of an employee. Define 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 constructor (...), void calSal( ) and void show( ). The super class, main function and algorithm need NOT be written.

Answer

Answer

class Overtime extends EmpSal
{
    int hours;
    double totsal;

    Overtime(String n, int c, double s, int h)
    {
        super(n, c, s);
        hours = h;
        totsal = 0.0;
    }

    void calSal()
    {
        if (hours > 40)
            totsal = salary + 5000;
        else if (hours >= 30 && hours <= 40)
            totsal = salary + 3000;
        else
            totsal = salary;
    }

    void show()
    {
        super.show();
        calSal();
        System.out.println("Overtime Hours: " + hours);
        System.out.println("Total Salary: " + totsal);
    }
}
Explanation: Overtime extends EmpSal. Its constructor calls super(n, c, s) to initialise the inherited empnum, empcode and salary, then initialises hours and totsal. calSal() applies the three overtime criteria (hours>40 adds 5000, 30<=hours<=40 adds 3000, hours<30 leaves salary unchanged) to compute totsal. show() calls super.show() to display the inherited employee details, invokes calSal(), then prints the overtime hours and total salary. Tested with salary=40000: hours=45 gives Total Salary=45000.0, hours=35 gives 43000.0, and hours=20 gives 40000.0 (unchanged) - all matching the given criteria.