Python 3. Please write the codes with indentation or screenshot! NOTE: There sho
ID: 3710571 • Letter: P
Question
Python 3. Please write the codes with indentation or screenshot!
NOTE: There should be only ONE Frank 1 in the output.
Your script should contain the two classes below as well as the test code.
Employee Class
Create the class Employee with two hidden attributes
name
number
This class will only have a constructor, an __str__ method and getters for the two attributes.
The constructor must have the following header
ProductionWorker Class
Create the class ProductionWorker which is a subclass of Employee.
This constructor must have the same header as the one for Employee.
The constructor must call the Employee.
The constructor must also set two hidden attributes to their default values.
shift - 1
rate - 15.00
The class must have the following accessor methods
get_shift
get_rate
The class must have an __str__ method which returns a string containing all the attributes.
The class must also have the mutator set_shift which will only accept integers of 1, 2 and 3, and will print an error message if given a value that cannot be turned into an integer or a value other than the ones given above and leave the original value in place.
The class must have the mutator set_rate which will only accept a float value in the range 15.00 to 50, and will print an error message if given a value that cannot be turned into an integer or a value outside the range and leave the original value in place.
Suggestions
Testing
Your output should look something like this
Be sure to run this script on the Unix machine so you know it works in the environment in which I will run it when I score your homework.
Your script must contain the following test code
Your output should look something like this
Explanation / Answer
class Employee(object):
def __init__(self, name, number):
self._name = name
self._number = number
def __str__(self):
return self._name + ", " + str(self._number)
def get_name(self):
return self._name
def get_number(self):
return self._number
class ProductionWorker(Employee):
def __init__(self, name, number):
super(ProductionWorker, self).__init__(name, number)
self._shift = 1
self._rate = 15.00
def get_shift(self):
return self._shift
def get_rate(self):
return self._rate
def set_shift(self, shift):
try:
shift = int(shift)
if shift >=1 and shift <= 3:
self._shift = shift
else:
print("shift can only be 1, 2 or 3")
except:
print(shift + " cannot be converted into an integer")
def set_rate(self, rate):
try:
rate = float(rate)
if rate >= 15.0 and rate <= 50.0:
self._rate = rate
else:
print("rate must be between 15.00 and 50.00")
except:
print(rate + " cannot be converted into a float")
def __str__(self):
return '{0}, shift: {1._shift}, rate: ${1._rate}'.format(super().__str__(), self)
if __name__ == '__main__':
e1 = Employee("Frank", 1)
print(e1.get_name(), e1.get_number())
print(e1)
print()
e2 = ProductionWorker('Sam', 2)
print(e2.get_name(), e2.get_number(), e2.get_shift(), e2.get_rate())
e2.set_shift('one')
print('shift:', e2.get_shift())
e2.set_shift(4)
print('shift:', e2.get_shift())
e2.set_shift(2)
print('shift:', e2.get_shift())
e2.set_rate('twenty')
print('rate:', e2.get_rate())
e2.set_rate(10)
print('rate:', e2.get_rate())
e2.set_rate(20)
print('rate:', e2.get_rate())
print(e2)
Sample run
Indented code: https://paste.ee/p/RqThk
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.