4 Inheritance with Employee and Freelancer
-
If needed, retrieve the OOP Company project up to chapter 3 from DLO. It contains the
Program,Person, andDepartmentclasses as you left them at the end of chapter 3. -
The company recognises two types of persons: employees and freelancers. In this exercise these become subclasses of the
Personclass. ThePersonclass must also be adapted. -
Add the
ToString()method to theDepartmentclass.- The desired result is "department name at location" with the attribute values in place of the italics.
-
Modify the
Personclass according to the class diagram:- Pay attention to visibility modifiers.
- Use a constant for the default value of the
nameattribute. - Adjust the constructors and keep applying constructor chaining.
- The
CalculateYearlyIncome()method always returns 0. - Remove properties of removed attributes.
- Add the
ToString()method.- The desired result is "name lives in city and works at department name at location".
- Use the
ToString()method of theDepartmentclass!
-
Create the
Employeesubclass according to the class diagram:- Create constructors and apply constructor chaining. Use the known default values.
- Use the constructors of the
Personclass. - Create the properties.
- Yearly income is 12 times monthly salary plus any bonus. The bonus equals monthly salary.
- Add the
ToString()method.- The desired result is "name lives in city and works at department name at location and is an employee with/without right to a bonus".
- Use the
ToString()method of thePersonclass! - The "with/without" text is determined by the
HasRightToBonus()method.
-
Create the
Freelancersubclass according to the class diagram:- Use the constructors of the
Personclass. The default value forhoursWorkedis 0. - Create the properties.
- Yearly income is hourly rate times hours worked.
- Hours worked is increased when the freelancer is hired via the
Hire()method. - Add the
ToString()method.- The desired result is "name lives in city and works at department name at location and is a freelancer with an hourly rate of hourlyRate".
- Use the
ToString()method of thePersonclass!
- Use the constructors of the
-
Adjust your
Mainmethod as follows:- Create an array named
departmentswith 4 departments:- The department Operations in Hilversum;
- The department Support in Amsterdam;
- The department Management in Almere;
- The department Documentation in Gouda.
- Create an employee named
boss(name: Mark, city: Den Haag, monthly salary: 10000, department: Management). Note: use the array for the department. - Create an employee named
employee(name: Caroline, city: Delft, monthly salary: 4000, department: Support); - Create a freelancer named
assistant(name: Klaas, city: Diemen, hourly rate: 50,00, department: Documentation); - Hire Klaas for 160 hours.
- Add code so that your output looks like this. Use the
ToString()method (implicitly):
1 2 3 4
The number of people in the company is 3 Mark lives in Den Haag and works at department Management at Almere and is an employee with right to a bonus Caroline lives in Delft and works at department Support at Amsterdam and is an employee without right to a bonus Klaas lives in Diemen and works at department Documentation at Gouda and is a freelancer with an hourly rate of 50.0- Add code so that the following appears below the output above:
1 2 3
Mark earns 130000,00 per year Caroline earns 48000,00 per year Klaas earns 8000,00 per year - Create an array named