0

I have setter and getter for parent class Employee and I have to use void for calculateSalary() method for child class PermEmployee. I need to calculate PermEmployee salary based on the formula Salary = variable component + Basic pay + HRA (variable component is a percentage of basic pay depending on years of experience) and also to use it to set the salary attribute of the parent class as the child class does not have the salary attribute.

Parent class Employee

public void setSalary(double salary) { 
        this.salary = salary; 
    }

    public double getSalary() { 
        return salary;
    }

Child class PermEmployee

//constructor 
    PermEmployee(int empId, String name, double basicPay, double hra, int experience){ 
        super(empId,name); 
        this.basicPay = basicPay; 
        this.hra = hra; 
        this.experience = experience;
    }

public void calculateSalary() { 
        if (experience<3) {
            setSalary(0/100*getBasicPay() + getBasicPay() + getHra());
        }
        else if (experience >=3 && experience <5) { 
            setSalary(5/100*getBasicPay() + getBasicPay() + getHra());

        }
        else if (experience >=5 && experience <10) { 
            setSalary(7/100*getBasicPay() + getBasicPay() + getHra());
        }
        else { 
            setSalary(12/100*getBasicPay() + getBasicPay() + getHra());
        }
 System.out.println("Permanent Employee: Your salary is " + getSalary()); 

    }

The formula does not work well when I try to pass the values to calculate the salary because the variable component does not get calculated. I'm expecting the output of 12000.0 but I got 11500.0 instead.

Can anyone pls enlighten me why? Is it wrong to use the setter and getter in calculation in the child class?

PermEmployee p1 = new PermEmployee(101, "Apple", 10000, 1500, 4); 
p1.calculateSalary();

0 Answers0