CIS 163 |
Inheritance |
Summer 2020 |
Objectives:
- Practice writing a Java class hierarchy
Activity
You may complete this lab in pairs.
- Begin by following this GitHub Classroom link and cloning the resulting repository:
classroom.github.com/g/I5actJmp
. You will find two classes,SalariedEmployee
andHourlyEmployee
that track an employee's pay and hours worked. These classes are nearly identical. The two main differences are:- The algorithm for calculating weekly pay is different.
- The
SalariedEmployee
's statement contains a warning if he or she is working too few hours per week.
- Refactor these two classes by adding a parent class named
Employee
that contains the data and code common to bothSalariedEmployee
andHourlyEmployee
. As you refactor the code:- Make the
pay
method anabstract
method inEmployee
. - Notice that
SalariedEmployee#printPayStatement
contains a little more code thanHourlyEmployee#printPayStatement
. Move the common code intoEmployee#printPayStatement
and haveSalariedEmployee#printPayStatement
callsuper.printPayStatement
.
- Make the
- 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.
- The pay statement should indicate (a) whether the intern has earned credit, and, if so, (b) the letter grade.
- After your refactor is complete, uncomment the code at the end of
App.main
, and anIntern
object to the array, and make sure it compiles and runs as expected. - 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, 3 June 2020, 3:30 PM