Now let's evaluate these classes for implementation: Employee, HourlyEmployee, SalariedEmployee, and Manager.
Problem Statement Your task is to implement payroll processing for different kinds of employees. • Hourly employees get paid an hourly rate, but if they work more than 40 hours per week, the excess is paid at “time and a half”. • Salaried employees get paid their salary, no matter how many hours they work. • Managers are salaried employees who get paid a salary and a bonus. Your program should compute the pay for a collection of employees. For each employee, ask for the number of hours worked in a given week, then display the wages earned.
Let's only consider variables for now. What are the common variables and what is unique to each class?
Goal: for each employee Print the name of the employee. Read the number of hours worked. Compute the wages due for those hours. So potential variables are: employee name, salary, bonus or total wage, working hours
Only employee name is common to all four classes since the salary/wage are calculated differently and working hours only apply to hourly employees.
Uniqueness: hourlyWage for class HourlyEmployee, annualSalary for class SalariedEmployee, and weeklyBonus for class Manager.
Create the above classes with corresponding instance variables. Since all instance variables are private, we need all the public getters and setters as well.
Now in the main() method of class Employee, declare an object: Employee e = new Employee(); Will you be able to print out e.name?
In the main() method of class HourlyEmployee, declare an object: HourlyEmployee he = new HourlyEmployee(); Will you be able to print out he.name? Can you call he.setName()? he.getName()? Why?
The question is: should private variables in a parent object be accessed directly in a child object? The answer is NO.
Parent-child relationship rules #1: a parent's privacy is not accessible to a child.
D. Try this:
Implement four classes of Player, NFLPlayer, NBAPlayer and MLBPlayer. Identify common variables and unique variables. Add setters and getters for each variable.
Create a Tester class and enter the code below in the main() method. Will you be able to print out the names? Why? Will p2 and fp2 be created successfully? Why? Will p2.name and fp2.name be printed successfully if these lines were in the main() method of Player class?
Player p1 = new Player(); NFLPlayer fp1 = new NFLPlayer(); System.out.println("Player 1 is:" + p1.name); System.out.println("Player 1 is:" + fp1.name); Player p2 = new NFLPlayer(); NFLPlayer fp2 = new Player(); System.out.println("Player 1 is:" + p2.name); System.out.println("Player 1 is:" + fp2.name);