07 Teach : Team Activity - Polymorphism
This activity is designed to be completed with your team.
Overview
BYU-Idaho is having issues with its payroll system and has asked you to create a new one. You quickly recognize that there are two types of employees: hourly employees (those that get paid a certain amount per hour); and salary employees (those that receive an annual salary).
While these two types of employees have differences, they also have a significant amount in common. So you decide to create a common base class with each of these two types as a separate derived class.
Instructions
For this assignment, you will start with a blank file. To keep our focus directly on the abstract methods we are working with, you may put all of our classes in the same file rather than separate files.
Core Requirements
Create a base class
Employee, and two derived classesHourlyEmployeeandSalaryEmployee, as follows:Employeeshould have a string name and adisplayfunction that displays the name.HourlyEmployeeshould have anint hourly_wageand should override thedisplayfunction to display the name and wage in the format: "John - $8/hour".SalaryEmployeeshould have anint salaryand should override thedisplayfunction to display the name and salary in the format: "John - $50000/year".You may also find it beneficial to add parameters to your
__init__function, but that is up to you.
Create a
mainfunction that does the following:Declares a list for employees.
Loops until the user enters "q" and prompts the user for an "h" (hourly employee) or an "s" (salary employee) or a "q" to quit.
Then prompts for the
nameand thehourlyRate/salary.For each employee entered, creates a new employee of the correct type and adds it to the list.
After the user enters "q", have
mainloop through the list and call thedisplaymethod for each employee. Run your program and ensure that it looks correct.
Stretch Challenges
After completing the above steps, make sure that everyone on the entire team is to this point and understands the material. Then, if you have time, move onto the following stretch challenges.
Convert your
Employeeclass to an abstract base class and the display function to be an abstract method.-
Add to your
HourlyEmployeea member variable for hours. Then, add an abstract method to the employee class forget_paycheck(), and override it in the derived classes. For hourly employees, this is calculated as the number of hours multiplied by the hourly rate. For salary employees, this should be the salary divided by 24.Change your code that prompts for the hourly employee to also prompt for the number of hours.
In the loop in
mainthat calls thedisplayfunction, change it to also call theget_paycheck()function and display the pay check amount. Add a regular function (not a member function of any class),
display_employee_data, that accepts an employee and calls itsdisplayfunction as well as itsget_paycheck()function and displays the value. Then, remove this code frommain, and replace it with a call to your new function.
Instructor's Solution
As a part of this team activity, you are expected to look over a solution from the instructor, to compare your approach to that one. One of the questions on the I-Learn submission will ask you to provide insights from this comparison.
Please DO NOT open the solution until you have worked through this activity as a team for the one hour period. At the end of the hour, if you are still struggling with some of the core requirements, you are welcome to view the instructor's solution and use it to help you complete your own code. Even if you use the instructor's code to help you, you are welcome to report that you finished the core requirements, if you code them up yourself.
After working with your team for the one hour activity, click here for the instructor's solution.
Submission
You do not need to submit your program (just make sure that everyone has a copy of it for their reference). Instead, answer the accompanying questions in the I-Learn quiz.