Amazon Affiliate

Thursday 31 May 2012

PROGRAM USING INHERITANCE IN JAVA....


Q3) Derive sub-classes of  ContractEmployee namely HourlyEmployee & WeeklyEmployee with information number of hours & wages per hour, number of weeks & wages per week respectively  & method calculateWages() to calculate their monthly salary. Also override getDesig () method depending on the type of contract employee.
ANSWER:

class Employee
{
            private String firstName;
            private String lastName;
            public String getFirstName()
            {
                        return firstName;
            }
            public String getLastName()
            {
                        return lastName;
            }
            public void setFirstName(String firstName)
            {
                        this.firstName=firstName;
            }
            public void setLastName(String lastName)
            {
                        this.lastName=lastName;
            }
}
class ContractEmployee extends Employee
{
            String depart;
            String desig;
            double salary;
            public void setDepartment(String depart)
            {
                        this.depart=depart;
            }
            public void setDesignation(String desig)
            {
                        this.desig=desig;
            }
            public void setSalary(double salary)
            {
                        this.salary=salary;
            }
            public String getDepartment()
            {
                        return depart;
            }
            public String getDesignation()
            {
                        return desig;
            }
            public double getSalary()
            {
                        return salary;
            }
            public void dispFullName()
            {
                        System.out.println(getFirstName()+getLastName());
            }
}
class RegularEmployee extends Employee
{
            String depart;
            String desig;
            double salary;
            public void setDepartment(String depart)
            {
                        this.depart=depart;
            }
            public void setDesignation(String desig)
            {
                        this.desig=desig;
            }
            public void setSalary(double salary)
            {
                        this.salary=salary;
            }
            public String getDepartment()
{
                        return depart;
            }
            public String getDesignation()
            {
                        return desig;
            }
            public double getSalary()
            {
                        return salary;
            }
            public void dispFullName()
            {
                        System.out.print(getFirstName() + " " + getLastName());
            }
}
class HourlyEmployee extends ContractEmployee
{
            int noOfHrs;
            double wagesPerHrs;
            public void setHours(int noOfHrs)
            {
                        this.noOfHrs=noOfHrs;
            }
            public void setWages(double wagesPerHrs)
            {
                        this.wagesPerHrs=wagesPerHrs;
            }
            public double calculateSalary()
            {
                        return noOfHrs*wagesPerHrs;
            }
            public String getDesignation()
            {
                        return "HourlyEmployee";
            }
               }
class WeeklyEmployee extends ContractEmployee{
            int noOfWeeks;
            double wagesPerWeek;
            public void setWeeks(int noOfWeeks)
            {
                        this.noOfWeeks=noOfWeeks;
            }
            public void setWages(double wagesPerWeek)
            {
                        this.wagesPerWeek=wagesPerWeek;
            }
            public double calculateSalary()
            {
                        return noOfWeeks*wagesPerWeek;
            }
            public String getDesignation()
            {
                        return "WeeklyEmployee";
            }
}

No comments:

Post a Comment