0% found this document useful (0 votes)
40 views4 pages

Quiz 2 OOP Lab

The document outlines a programming task to create a bonus calculation application for different employee types: Managers, Engineers, and Interns. It specifies the creation of a base class 'Employee' with methods for calculating and displaying bonuses, and three subclasses that override the bonus calculation method according to their respective percentages. The provided source code demonstrates the implementation of these classes and their usage in a main method to display each employee's bonus.

Uploaded by

ranajahangir3256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views4 pages

Quiz 2 OOP Lab

The document outlines a programming task to create a bonus calculation application for different employee types: Managers, Engineers, and Interns. It specifies the creation of a base class 'Employee' with methods for calculating and displaying bonuses, and three subclasses that override the bonus calculation method according to their respective percentages. The provided source code demonstrates the implementation of these classes and their usage in a main method to display each employee's bonus.

Uploaded by

ranajahangir3256
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Quiz 2

(Inheritance & Polymorphism)


Question 1: You are tasked with developing a small application for a company that calculates
the annual bonuses for different types of employees. The company has three types of employees:
Managers, Engineers, and Interns. Each type of employee receives a different percentage of their
salary as a bonus:

 Managers receive 10% of their salary as a bonus.


 Engineers receive 8% of their salary as a bonus.
 Interns receive 2% of their salary as a bonus.

Instructions:

1. Create a base class Employee that has:


o A constructor to initialize the employee's name and salary.
o A method calculateBonus() that returns the bonus amount (default to 5% of the
salary).
o A method displayBonus() that prints the employee's name and their bonus
amount.
2. Create three subclasses:
o Manager that overrides the calculateBonus() method to return 10% of the
salary.
o Engineer that overrides the calculateBonus() method to return 8% of the
salary.
o Intern that overrides the calculateBonus() method to return 2% of the salary.
3. In the main method of your program, create one object of each subclass (Manager,
Engineer, and Intern), and call the displayBonus() method for each to print their
respective bonus.

MOHAMMAD JAHANGEER(25592)

SOURCE CODE:

class Employee {

String name;

double salary;

Employee(String name, double salary) {

this.name = name;
this.salary = salary;

double calculateBonus() {

return 0.05 * salary;

void displayBonus() {

double bonus = calculateBonus();

System.out.printf("%s receives a bonus of Rs%.2f%n", name, bonus);

class Manager extends Employee {

Manager(String name, double salary) {

super(name, salary);

@Override

double calculateBonus() {

return 0.10 * salary;

class Engineer extends Employee {

Engineer(String name, double salary) {

super(name, salary);

@Override

double calculateBonus() {

return 0.08 * salary;

class Intern extends Employee {


Intern(String name, double salary) {

super(name, salary);

@Override

double calculateBonus() {

return 0.02 * salary;

public class Main {

public static void main(String[] args) {

Employee manager = new Manager("Jahangeer", 90000);

Employee engineer = new Engineer("zain", 75000);

Employee intern = new Intern("uzair", 30000);

manager.displayBonus();

engineer.displayBonus();

intern.displayBonus();

OUTPUT

You might also like