Question: Please write the function in python and also please include explantations for each line of code in the function and a screenshot to see the
Please write the function in python and also please include explantations for each line of code in the function and a screenshot to see the correct indentation.
Define a class named Worker which stores information about an worker's surname, first_name, id and weekly salary. The class must have the following:
- Create a worker from surname, first_name, worker_id and weekly_salary (float). For example: worker1 = Worker('Smith', 'John', 'SJ001', 522.70) Note: The data fields (surname, first_name, worker_id, weekly_salary) must all be defined as private instance variables.
- Create a worker from just the names and worker_id, but without the weekly_salary. For example: worker2 = Worker('Smith', 'Roger', 'SR002') Note: The default value for the weekly salary is 0.0.
- Return a string representing information about the worker. For example: the statement: print(worker1) would produce the output: John Smith (ID SJ001), weekly salary = $522.70
- The class should also provide functionality to get the names, the worker id and the weekly salary. For example, the code: print(worker1.get_names(), worker1.get_worker_id(), worker1.get_weekly_salary()) would produce the output: John Smith SJ001 522.70
- Finally, the class should provide functionality to set the weekly salary. For example: worker2.set_weekly_salary(522.70) Note: The weekly salary data field should only be changed if the corresponding parameter is larger than 0.0.
| Test | Result |
worker1 = Worker('Smith', 'John', 'SJ001', 522.70) print(worker1) print(worker1.get_names(), worker1.get_worker_id(), worker1.get_weekly_salary()) | John Smith (ID WS001), weekly salary = $522.70 John Smith WS001 522.70 |
worker2 = Worker('Smith', 'Roger', 'SR002') print(worker2) worker2.set_weekly_salary(-10.2) print(worker2) worker2.set_weekly_salary(333) print(worker2) | Roger Smith (ID SR002), weekly salary = $0.00 Roger Smith (ID SR002), weekly salary = $0.00 Roger Smith (ID SR002), weekly salary = $333.00 |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
