Knowee
Questions
Features
Study Tools

BackgroundConsider the following (base) class that has been loaded into your starter code:class Employee(object): def __init__(self, name: str, salary: float) -> None: self._name = name self._salary = salary def get_name(self) -> str: return self._name def wage(self) -> float: return self._salary/26 # fortnight payTaskDefine a subclass of Employee called Worker. A worker has a manager (who is another employee) and this manager is given as an argument to the constructor.Define a method in Workerget_manager(self) -> Employeethat returns the worker's manager.Define a subclass of Employee called Executive. An executive has a yearly bonus in addition to a wage and this bonus is given as an argument to the constructor.Override the Employee.wage method to take a bonus into account. You must call Employee.wage from Executive.wage (using super). Remember the existing wage method calculates a fortnightly pay, but the bonus is annual.Examples>>> burns = Employee('Mr. Burns', 1000000)>>> smithers = Worker('Waylon Smithers', 25000, burns)>>> smithers.get_manager() is burnsTrue>>> executive = Executive('Joseph Bloggs', 510000, 10000)>>> executive.wage()20000.0Note for executive that 10000 is the bonus.

Question

BackgroundConsider the following (base) class that has been loaded into your starter code:class Employee(object): def init(self, name: str, salary: float) -> None: self._name = name self._salary = salary def get_name(self) -> str: return self._name def wage(self) -> float: return self._salary/26 # fortnight payTaskDefine a subclass of Employee called Worker. A worker has a manager (who is another employee) and this manager is given as an argument to the constructor.Define a method in Workerget_manager(self) -> Employeethat returns the worker's manager.Define a subclass of Employee called Executive. An executive has a yearly bonus in addition to a wage and this bonus is given as an argument to the constructor.Override the Employee.wage method to take a bonus into account. You must call Employee.wage from Executive.wage (using super). Remember the existing wage method calculates a fortnightly pay, but the bonus is annual.Examples>>> burns = Employee('Mr. Burns', 1000000)>>> smithers = Worker('Waylon Smithers', 25000, burns)>>> smithers.get_manager() is burnsTrue>>> executive = Executive('Joseph Bloggs', 510000, 10000)>>> executive.wage()20000.0Note for executive that 10000 is the bonus.

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is the Python code for the task:

class Worker(Employee):
    def __init__(self, name: str, salary: float, manager: Employee) -> None:
        super().__init__(name, salary)
        self._manager = manager

    def get_manager(self) -> Employee:
        return self._manager


class Executive(Employee):
    def __init__(self, name: str, salary: float, bonus: float) -> None:
        super().__init__(name, salary)
        self._bonus = bonus

    def wage(self) -> float:
        return super().wage() + self._bonus / 26

In the Worker subclass, we define a new __init__ method that takes an additional manager argument. We call the Employee's __init__ method using super().__init__(name, salary) to initialize the name and salary attributes, and then we initialize the manager attribute.

The get_manager method simply returns the manager attribute.

In the Executive subclass, we define a new __init__ method that takes an additional bonus argument. We call the Employee's __init__ method using super().__init__(name, salary) to initialize the name and salary attributes, and then we initialize the bonus attribute.

The wage method in Executive overrides the wage method in Employee. It calls the Employee's wage method using super().wage() to get the base wage, and then adds the bonus divided by 26 (since the wage is calculated fortnightly).

This problem has been solved

Similar Questions

For the subclass Employee of the base class Person example provided in the tutorial, what if we need to keep track of the department for each employee? What code segment could be used to properly initialize, and add methods to set and get this value? a.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title, department) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(self): return "Department: " + str(self.department) b.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(self): return "Department: " + str(self.department) c.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear emp_department = department def get_department(self): return "Department: " + str(self.department) d.) class Employee(Person): def __init__(self,fname,lname,title,sal,empid,department): super().__init__(fname,lname,title) self.employeeid = empid self.salary = sal self.vacationdaysperyear = 14 self.vacationdays = self.vacationdaysperyear self.department = department def get_department(): return "Department: " + str(self.department)

Traceback (most recent call last): File "codes/mainc-5557-1702547553.7481453.py", line 17, in base_salary, bonus, total_salary = calculate_salary(name,designation,years_of_service) File "codes/mainc-5557-1702547553.7481453.py", line 7, in calculate_salary base_salary = salary_structure[designation]['base_salary']KeyError: '3'

class Base(): """ My base class """ __nb_instances = 0 def __init__(self): Base.__nb_instances += 1 self.id = Base.__nb_instancesclass User(Base): """ My User class """ def __init__(self): super().__init__() self.id = 89u = User()print(u.id)

class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__bobj = Demo()obj.a=45print(obj.a)

class People():         def __init__(self, name):                  self.name = name       def namePrint(self):               print(self.name)person1 = People("Sally")person2 = People("Louise")person1.namePrint()A. person1 and person2 are two different instances of the People class.B. The __init__ method is used to set initial values for attributes.C. 'self' is not needed in def namePrint(self):D. person2 has a different value for 'name' than person1.

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.