SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Inheritance
Exercise:
Programming Exercises
Question:1 | ISBN:9780132576376 | Edition: 2

Question

Employee and ProductionWorker Classes
Write an Employee class that keeps data attributes for the following pieces of information:
• Employee name
• Employee number
Next, write a class named ProductionWorker that is a subclass of the Employee class. The
ProductionWorker class should keep data attributes for the following information:
• Shift number (an integer, such as 1, 2, or 3)
• Hourly pay rate
The workday is divided into two shifts: day and night. The shift attribute will hold an integer
value representing the shift that the employee works. The day shift is shift 1 and the
night shift is shift 2. Write the appropriate accessor and mutator methods for each class.
Once you have written the classes, write a program that creates an object of the
ProductionWorker class and prompts the user to enter data for each of the object’s data
attributes. Store the data in the object and then use the object’s accessor methods to retrieve
it and display it on the screen.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Employee and ProductionWorker Classes program code:

# Employee class definition
class Employee:
    def __init__(self, name, emp_number):
        # Initialize data attributes
        self.name = name
        self.emp_number = emp_number
    
    # Accessor methods
    def get_name(self):
        return self.name
    
    def get_emp_number(self):
        return self.emp_number
    
    # Mutator methods
    def set_name(self, name):
        self.name = name
    
    def set_emp_number(self, emp_number):
        self.emp_number = emp_number


# ProductionWorker class definition, subclass of Employee
class ProductionWorker(Employee):
    def __init__(self, name, emp_number, shift_number, hourly_pay_rate):
        # Call superclass constructor to initialize inherited attributes
        super().__init__(name, emp_number)
        # Initialize additional data attributes
        self.shift_number = shift_number
        self.hourly_pay_rate = hourly_pay_rate
    
    # Accessor methods
    def get_shift_number(self):
        return self.shift_number
    
    def get_hourly_pay_rate(self):
        return self.hourly_pay_rate
    
    # Mutator methods
    def set_shift_number(self, shift_number):
        self.shift_number = shift_number
    
    def set_hourly_pay_rate(self, hourly_pay_rate):
        self.hourly_pay_rate = hourly_pay_rate


# Create an object of the ProductionWorker class and prompt the user to enter data
def main():
    name = input("Enter employee name: ")
    emp_number = input("Enter employee number: ")
    shift_number = int(input("Enter shift number (1 for day shift, 2 for night shift): "))
    hourly_pay_rate = float(input("Enter hourly pay rate: "))

    # Create ProductionWorker object
    worker = ProductionWorker(name, emp_number, shift_number, hourly_pay_rate)
    
    # Display the entered data using accessor methods
    print("Employee Name:", worker.get_name())
    print("Employee Number:", worker.get_emp_number())
    print("Shift Number:", worker.get_shift_number())
    print("Hourly Pay Rate:", worker.get_hourly_pay_rate())


# Call the main function to start the program
if __name__ == "__main__":
    main()

Executed Output:

Enter employee name: Charless
Enter employee number: 12345
Enter shift number (1 for day shift, 2 for night shift): 1
Enter hourly pay rate: 200
Employee Name: Charless
Employee Number: 12345
Shift Number: 1
Hourly Pay Rate: 200.0

 

0 0

Discussions

Post the discussion to improve the above solution.