Write an Employee class and three subclasses – Developer, Tester and Designer – each with its own bonus rule; compute annual bonuses for a team using an array of Employee.
Solution
Main.java
class Employee {
protected final String name;
protected final double salary;
protected final int rating; // 1-5
Employee(String name, double salary, int rating) { this.name = name; this.salary = salary; this.rating = rating; }
double bonus() { return salary * 0.05; }
}
class Developer extends Employee {
Developer(String n, double s, int r) { super(n, s, r); }
@Override double bonus() { return salary * (rating >= 4 ? 0.15 : 0.08); }
}
class Tester extends Employee {
Tester(String n, double s, int r) { super(n, s, r); }
@Override double bonus() { return super.bonus() + rating * 5000; }
}
class Designer extends Employee {
Designer(String n, double s, int r) { super(n, s, r); }
@Override double bonus() { return Math.min(salary * 0.1 * rating / 3, 120000); }
}
public class Main {
public static void main(String[] args) {
Employee[] team = {new Developer("Asha", 1200000, 5), new Developer("Ravi", 900000, 3), new Tester("Meena", 700000, 4),
new Designer("Zoya", 1000000, 5), new Employee("John", 500000, 3)};
double total = 0;
for (Employee e : team) {
System.out.printf("%-6s %-9s %,10.0f%n", e.name, e.getClass().getSimpleName(), e.bonus());
total += e.bonus();
}
System.out.printf("total bonus %,.0f%n", total);
}
}
Output Verified by compiling and running
Asha Developer 180,000
Ravi Developer 72,000
Meena Tester 55,000
Zoya Designer 120,000
John Employee 25,000
total bonus 452,000
Zoya's formula gives 166,667, which the cap reduces to 120,000. Tester extends its parent's rule with super.bonus().
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!