Objectives
The goal of this assignment is to learn the following:
- How to use inheritance
- How to create class actions and use derived actions
- How to use Polymorphism
Overview
In this assignment you will create a program that tells the user the different amounts an employee can make, depending on what kind of employee they are. There are three different kinds of employees: a salaried employee, an hourly employee, and a commissioned employee. Each one will require different inputs and will require a different method for calculating earnings.
Requirements
You will need five classes: Employee, SalariedEmployee, HourlyEmployee, CommissionEmployee, and Main. Class Employee will be your base class, and will contain actions and blueprint actions to be used in each class that inherits from Employee.
Class Employee
In this class, you will implement several setters and getters for variables that will be used in each class that inherits from Employee. Since every employee, regardless of how they are paid, share some common information (they have a name and a social security number), this class will obtain and provide that information. Class Employee will have the following actions:
- blueprint action Earnings returns number
Each class will have a separate implementation for Earnings, so a blueprint action will work well here.
- action SetFirstName(text name): Setter for the text variable firstName
- action GetFirstName returns text: Getter for the first name of an employee.
- action SetLastName(text name): Setter for the text variable lastName
- action GetLastName returns text: Getter for the last name of an employee.
- action SetSSN(integer value): Setter for the integer variable ssn
- action GetSSN returns integer: Getter for the social security number of an employee.
- action Print: The Print action will be generic in this class, with only a say statement to tell the user the first and last name and social security number. In other classes, you will use polymorphism to implement a more custom print action.
Class SalariedEmployee
This class will inherit from Employee. It needs the following actions:
- action SetWeeklySalary(number salary): Setter for the number variable weeklySalary.
- action GetWeeklySalary returns number: Getter for the weekly salary of an employee.
- action ReportSalariedEmployee(text first, text last, integer ssn, number salary): ReportSalariedEmployee should call the setters for the name(first and last), ssn, and weekly salary of an employee.
- action Earnings returns number: Earnings should return the weekly salary of an employee by making a call to action GetWeeklySalary. It should look similar to the following:
action Earnings returns number
return GetWeeklySalary()
end
Since action GetWeeklySalary returns a value, you can call the action in a return statement, as seen above.
Class HourlyEmployee
This class also inherits from Employee. It needs the following actions:
- action SetWage(number wage): Setter for the number variable hourlyWage.
- action GetWagereturns number: Getter for the hourly wage of an employee.
- action SetHours(number hours): Setter for the number variable hoursWorked.
- action GetHours returns number: Getter for the hours worked by an employee.
- action ReportHourlyEmployee(text first, text last, integer ssn, number wage, number hours): ReportHourlyEmployee does the same thing ReportSalariedEmployee does, only it should call the setters for wage and hours instead.
- action Earnings returns number: Earnings should return the weekly earnings of an hourly employee. If the hourly employee has worked over 40 hours, then make sure that action Earnings is giving the employee the proper overtime(overtime is 1.5x the normal pay).
Class CommissionEmployee
This class also inherits from Employee. It needs the following actions:
- action SetCommissionRate(number rate): Setter for the number variable commissionRate
- action GetCommissionRate returns number: Getter for the commission rate of an employee.
- action SetSales(number value): Setter for the number variable sales
- action GetSales returns number: Getter for the number of sales an employee makes.
- action ReportCommissionEmployee(text first, text last, integer ssn, number commissionRate, number sales): ReportCommissionEmployee will do as the previous "Report" actions do, except it will call the setters for commission rate and sales.
- action Earnings returns number: Earnings should return the earnings of a commissioned employee. This values can be calculated by commission rate * number of sales.
Class Main
In class Main, create variables that will satisfy all the arguments for each class. Then call the appropriate actions from each class to satisfy the requirements in the Sample Output section.
Sample Output
When run, the program should report to the user the name of the employee, what kind of employee they are, and what their earnings are. For the hourly employee, tell the user what their hourly wage and number of hours worked are, in addition to their name, earnings, and type of employee. For a commissioned employee, tell the user their commission rate and the number of sales they made, in addition to their name, earnings, and type of employee. Output should be similar to the following:
Salaried employee Brandon Spencer. Total earnings for this salaried employee is 60,000 dollars.
Commission employee Brandon Spencer. Gross sales is 20 sales at 4 dollars per a sale. Total earnings for this commissioned employee is 80 dollars.
Hourly employee Brandon Spencer. Hourly wage is 16.0 and number of hours worked is 40. Total earnings for this hourly employee is 640 dollars.
Next Tutorial
In the next tutorial, we will discuss Challenge 6.1, which describes an audio environment for battles using inheritance..