There is an abstract Employee class with subclasses: HourlyWorker, TemmpWorker,
ID: 669643 • Letter: T
Question
There is an abstract Employee class with subclasses: HourlyWorker, TemmpWorker, and Manager. The annual bonus for each employee type is calculated differently, appropriately implemented by method getAnnualBonus in each subclass. Assuming that these classes are implemented, give a program segment that declares an array thathold up to 100 employees. Assuming that this array had been flled wiith employees of all types, give the code to calculate and display the total amount of annual bonuses given by the company.
public abstract class Employee
//assume constructors and methods implemented
public abstract int getAnnualBonus();
}
Explanation / Answer
abstract class Employee
{
protected int employeeID;
protected String firstName;
protected String lastName;
protected double salary;public Employee(int employeeID, String firstName, String lastName,
double salary)
{
this.employeeID = employeeID;
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}public abstract void raise();
}class Manager extends Employee {
public Manager(int employeeID, String firstName, String lastName,
double salary)
{
super(employeeID, firstName, lastName, salary);
}public void raise()
{
salary = 1.1 * salary;
}public String toString()
{
return "Manager [employeeID=" + employeeID + ", firstName=" + firstName
+ ", lastName=" + lastName + ", salary=" + salary + "]";
}
}class SalesAssociate extends Employee
{
private double commission;public SalesAssociate(int employeeID, String firstName, String lastName,
double salary, double commission) {
super(employeeID, firstName, lastName, salary);
this.commission = commission;
}public void raise()
{
salary += commission;
}public String toString()
{
return "SalesAssociate [commission=" + commission + ", employeeID="
+ employeeID + ", firstName=" + firstName + ", lastName="
+ lastName + ", salary=" + salary + "]";
}
}class Secretary extends Employee
{
public Secretary(int employeeID, String firstName, String lastName,
double salary)
{
super(employeeID, firstName, lastName, salary);
}public void raise()
{
salary = 1.05 * salary;
}public String toString()
{
return "Secretary [employeeID=" + employeeID + ", firstName="
+ firstName + ", lastName=" + lastName + ", salary=" + salary
+ "]";
}
}public class TestEmployee
{
public static void main(String[] args)
{
Employee e1 = new Manager(1, "Larry", "Paul", 99.99);
Employee e2 = new SalesAssociate(2, "Mary", "Janice", 97.2, 0.55);
Employee e3 = new Secretary(3, "Jay", "Peace", 88);
Employee[] es = new Employee[] { e1, e2, e3 };for (Employee e : es)
{
System.out.println("Before raise");
System.out.println(e);
System.out.println("After raise");
e.raise();
System.out.println(e);
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.