Skip to main content

CIS 163

Inheritance

Spring 2021

Objectives:

Activity

You may complete this lab in pairs.

  1. Begin by following this GitHub Classroom link and cloning the resulting repository: https://classroom.github.com/g/m0gBB2tX. You will find two classes, SalariedEmployee and HourlyEmployee that track an employee's pay and hours worked. These classes are nearly identical. The two main differences are:
    1. The algorithm for calculating weekly pay is different.
    2. The SalariedEmployee's statement contains a warning if he or she is working too few hours per week.
  2. Refactor these two classes by adding an abstract parent class named Employee that contains the data and code common to both SalariedEmployee and HourlyEmployee. As you refactor the code:
    • Make the pay method an abstract method in Employee.
    • Notice that SalariedEmployee#printPayStatement contains a little more code than HourlyEmployee#printPayStatement. Move the common code into Employee#printPayStatement and have SalariedEmployee#printPayStatement call super.printPayStatement.
  3. Add a third subclass called Intern. Interns track their hours each week; but
    • Their pay is always $0.
    • They get a grade. The grade should be an instance variable.
    • They don't receive credit until they have completed 160 hours and 10 weeks of work. (They must work at least 1 hour for a week to be counted.)
    • The pay statement should indicate (a) whether the intern has earned credit, and, if so, (b) the letter grade.
  4. After your refactor is complete, uncomment the code at the end of App.main, and an Intern object to the array, and make sure it compiles and runs as expected.
  5. Finally, create a UML diagram of your class hierarchy.

Challenge problem: Modify the classes so that the second line of the pay statement (the one between the name and "Week ending") displays a message custom to the employee type (e.g., "Salary: $...", "Hourly Wage: $...", "Status: Intern").

Submission

Commit the changes to the code. If you worked with a partner, be sure both names are in a comment at the head of each file.

Updated Wednesday, 12 May 2021, 8:27 AM

W3c Validation