Knowee
Questions
Features
Study Tools

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)

Question

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)

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

Solution

The correct answer is b.)

In this code segment, the Employee subclass is properly initialized with the department attribute. The super() function is used to call the init() method of the Person base class, which initializes the first name, last name, and title of the employee. Then, the Employee class's own init() method initializes the employee ID, salary, vacation days per year, current vacation days, and department of the employee.

The get_department() method is also correctly defined. It returns a string that includes the department of the employee. The self keyword is used to access the department attribute of the instance of the class.

In option a.) the super().init() method is incorrectly called with the department attribute. In option c.) the department attribute is not correctly initialized with the self keyword, so it would not be accessible outside of the init() method. In option d.) the get_department() method is incorrectly defined without the self keyword, so it would not be able to access the department attribute of the instance of the class.

This problem has been solved

Similar Questions

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.

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__()u = User()print(u.id)

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.

For the base public class Person example provided in the Unit 3 Revisiting the Employee Class Program tutorial, what if you need to add a subclass for Volunteers who have a host employee and are assigned to a project.What code segment could implement this subpublic class and create an instance of it called volunteer_1?public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project){ super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost(){ return "Host " + this.host; } public void setHost(String newhost){ this.host = newhost; } public String getProject(){ return "Project " + this.project; } public void setProject(String project){ this.project = project; } } Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person { private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project){ this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");public class Volunteer extends Person{ private String host; private String project; public Volunteer(String firstName,String lastName,String jobTitle,String host, String project) { super(firstName,lastName,jobTitle,host,project); this.host = host; this.project = project; } public String getHost() { return "Host " + this.host; } public void setHost(String newhost) { this.host = newhost; } public String getProject() { return "Project " + this.project; } public void setProject(String project) { this.project = project; }} Volunteer volunteer_1 = new Volunteer("John","Doe","Volunteer","Jane Doe","Project_X");

Design a class named Person and its two subclasses named Student and Employee.  Make Faculty and Staff subclasses of Employee. A person has a name,  address, phone_number, and e-mail address. A student has a status (freshman, sophomore, junior, or senior).  An employee  has an office, salary. A faculty member has office_hours and a rank. A staff member has a title. Override the toString() method in each of these classes to display their details. Write a Java application and subsequent pseudocode to implement/simulate the same.

1/3

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.