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(…): parameterized 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(…): parameterized 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.
import java.util.Scanner;
class EmpSal{
protected String empnum;
protected int empcode;
protected double salary;
public EmpSal(String n, int c, double s){
empnum = n;
empcode = c;
salary = s;
}
public void show(){
System.out.println("Employee name: " + empnum);
System.out.println("Employee code: " + empcode);
System.out.println("Salary: " + salary);
}
}
class Overtime extends EmpSal{
int hours;
double totsal;
public Overtime(String n, int c, double s, int h){
super(n, c, s);
hours = h;
totsal = 0.0;
}
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("Hours: " + hours);
System.out.println("Total salary: " + totsal);
}
}
class Inheritance{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("Employee name: ");
String n = in.nextLine();
System.out.print("Employee code: ");
int c = Integer.parseInt(in.nextLine());
System.out.print("Salary: ");
double s = Double.parseDouble(in.nextLine());
System.out.print("Hours: ");
int h = Integer.parseInt(in.nextLine());
Overtime obj = new Overtime(n, c, s, h);
obj.calSal();
obj.show();
}
}