Task 7 - Inheritance in Java

Last Updated on Nov. 26, 2020

Program 7.1 

(With reference to Employee inheritance hierarchy, given in Chapter 9 of our reference book) You studied an inheritance hierarchy in which the class BasePlusCommissionEmployee inherited from class CommissionEmployee. However, not all types of employees are CommissionEmployees. In this exercise, you shall create a more general Employee superclass that factors out the attributes and behaviors in class CommissionEmployee that are common to all Employees. The common attributes and behaviors for all Employees are firstName, lastName, CNIC, getFirstName, getLastName, getCNIC and a portion of method toString. Create a new superclass Employee that contains these instance variables and methods and a constructor. Next, rewrite the class CommissionEmployee from Section 9.4.5 as a subclass of Employee. Class CommissionEmployee should contain only the instance variables and methods that are not declared in its parent type. Class CommissionEmployee’s constructor should invoke class Employee’s constructor and CommissionEmployee’s toString method should invoke Employee’s toString method.

Once you’ve completed these modifications, run the CommissionEmployeeTest and BasePlusCommissionEmployeeTest apps using these new classes to ensure that the apps still display the same results for a CommissionEmployee object and BasePlusCommissionEmployee object, respectively.

Program 7.2 

(Creating a New Subclass of Employee) Other types of Employees might include SalariedEmployees who get paid a fixed weekly salary, PieceWorker who get paid by the number of pieces they produce or HourlyEmployees who get paid an hourly wage with time-and-a-half—1.5 times the hourly wage—for hours worked over 40 hours.

Create class HourlyEmployee that inherits from class Employee and has instance variable hours (a double) that represents the hours worked, instance variable wage (a double) that represents the wages per hour, a constructor that takes as arguments a first name, a last name, a social security number, an hourly wage and the number of hours worked, set and get methods for manipulating the hours and wage, an earnings method to calculate an HourlyEmployee’s earnings based on the hours worked and a toString method that returns the HourlyEmployee’s String representation. Method setWage should ensure that wage is nonnegative, and setHours should ensure that the value of hours is between 0 and 168 (the total number of hours in a week). Use class HourlyEmployee in a test program that’s similar to the one in Fig. 9.5.

Program 7.3. 

Update you code to meet following new requirement. If an employee makes high commission, company want to give him/her bonus as per following formula:

Commission    | Bonus
10000-20000    | 5000
20000-50000    | 10000
50000+             | 20000

Modify the CommissionEmployee class earnings method in such a way that bonus shall be added for all type of employees who are belong to CommissionEmployee or its any sub-type.

Program 7.4

Create a Shape class that shall have color and filled instance variables of enum Color and boolean type. The filled boolean variable shall show, whether the shape is filled (colored). The enum Color shall be defined within the class having 4 color names (choose any four). 

Also define getArea and getPerimeter method in Shape class, both shall return 0. Create Rectangle class with length and width attributes and a Circle class with radius attribute, of type double. Properly encapsulate all fields. 

Circle and Rectangle shall extend Shape class and override getArea and getPerimters methods as per its type and specific formula. In Test class, create an object of Rectangle and Circle, by initializing all attributes in constructor and print both shapes' area and perimters.

In Test class, define a static method with following signature, 
public static void printEachShapeCount(Shape... shapes)
As you know, the parent type variable can hold the reference of any child class object. That means, we can call above method by passing any number of Circle and Rectangle objects. Define the method body, such that, it shall print how many circles and how many rectangles were passed to this method in variable length argument.