COMP 1046 OOP Prac 04 Multiple Inheritance SOL PDF

Title COMP 1046 OOP Prac 04 Multiple Inheritance SOL
Course Object-Oriented Programming
Institution University of South Australia
Pages 6
File Size 264.3 KB
File Type PDF
Total Downloads 56
Total Views 124

Summary

with solutions...


Description

UniSA STEM

COMP 1046 Object-Oriented Programming Practical 4 – Solutions Abstract Classes and Multiple Inheritance

Using the computers on campus Internal students are required to attend weekly practical sessions in order to undertake their practical work. Practical sessions are held in computer pools. You will be required to login to a computer in order to use it. Login to the computer using your username and password. Your username and password are your electronic signature-you can use them to access the University's online resources and services. a) Type in your user name in lowercase (this is your UNINET username – e.g. bondj007). b) You will then be asked to enter your UNINET password. The password allocated to you at the beginning of your program is the first four letters of your family name (add x if it's less than four letters) and the day and month of your date of birth. For example, if your surname is Ng and your date of birth is 17 March, your password will be ngxx1703. To protect the privacy of your information it is important that you change your password from the default one described above. Your new password must be 6 to 8 character long and cannot start with a number. To help you in case you forget your password setup a password reset account. If you forget your password try using the password reset facility. If you have not setup your your password reset facility contact the IT Help Desk (Monday to Friday, 8.30am to 9.00pm) by phoning 25000 (internal phones) or +61 8 8302 5000 (externally). Country or interstate callers can phone 1300 558 654 for the cost of a local call. You can get further information or log a service request on line at: www.unisa.edu.au/ITHelpDesk Marking criteria for continuous assessment: • There are three tasks below for which you receive marks. • Practicals are performed in pairs and you can use any resource that you like. • You can achieve a maximum of 50 marks.

External Students Please ensure that Python, Visual Studio Code (VSC) and the Python Extension for VSC are installed on your home computer so that you are ready for this week’s practical. See the 'Resources' page on the course website for information on installing Python. You must perform this practical individually. Copy all Python code from the Tasks below into a Word document and submit it electronically via the Website until end of Sunday.

In this practical, we will focus on creating abstract classes and using multiple inheritances. In your previous practical you have created three classes, Employee, Teaching Staff and Administration Staff. The code of all three is shown below: class Employee: def __init__(self, first_name, last_name, employee_id): self.first_name = first_name self.last_name = last_name self.employee_id = employee_id self.base_salary = 0

def set_base_salary(self, salary): self.base_salary = salary

class TeachingStaff (Employee): def __init__(self, first_name, last_name, employee_id, teaching_area, category): super().__init__(first_name, last_name, employee_id) self.teaching_area = teaching_area self.category = category def get_salary(self): salary = (((self.category * 10) + 100)/100) * self.base_salary return salary def get_staff_info(self): return 'First name: ' + self.first_name + \ '\nLast name: ' + self.last_name + \ '\nEmployee ID: ' + str(self.employee_id) + \ '\nArea of Expertise: ' + self.teaching_area + \ '\nCategory: ' + str(self.category) + \ '\nSalary: ' + str(self.get_salary())

class AdministrativeStaff(Employee): def __init__(self, first_name, last_name, employee_id, level): super().__init__(first_name, last_name, employee_id) self.level = level

def get_salary(self): salary = (((self.level * 15) + 100)/100) * self.base_salary return salary

def get_staff_info(self): return 'First name: ' + self.first_name + \ '\nLast name: ' + self.last_name + \ '\nEmployee ID: ' + str(self.employee_id) + \ '\nLevel: ' + str(self.level) + \

'\nSalary: ' + str(self.get_salary())

Download the code from the website because you will need it for the first task. You can find it under the Week 4 tab and the Practical section.

Task 1 (10 marks) We can see that the two classes Teaching Staff and Administrative Staff, have two common methods: • get_salary method • get_staff_info method The only difference in implementing these methods in our derived classes is the approach (or the formula used). If we assume that we can get more types of staff in our organisation, we may want to create a template to guide future implementations. We can generalise those methods in an abstract class. Task: Create an abstract class, separate from the implementation above. Name this abstract class PaidEmployee. This abstract class should have the following abstract methods: o get_salary o get_staff_info from abc import ABC, abstractmethod class PaidEmployee(ABC): @abstractmethod def get_salary(self): pass @abstractmethod def get_staff_info(self): pass p=PaidEmployee()

Try to instantiate the class. Traceback (most recent call last): File "c:\Users\mrgeo\CloudStation\Work\teaching\COMP1046\svn-X1C7\Material\W04\Practical\solution.py", line 76, in p=PaidEmployee() TypeError: Can't instantiate abstract class PaidEmployee with abstract methods get_salary, get_staff_info

Task 2 (10 marks) Our second task is to introduce abstract methods in our Employee class. These methods will be the passed down to the child classes which are already implemented by Teaching Staff and Administrative Staff. Task: Implement abstract methods get_salary() and get_staff_info() in the Employee class. from abc import ABC, abstractmethod class Employee: def __init__(self, first_name, last_name, employee_id): self.first_name = first_name

self.last_name = last_name self.employee_id = employee_id self.base_salary = 0 def set_base_salary(self, salary): self.base_salary = salary @abstractmethod def get_salary(self): pass @abstractmethod def get_staff_info(self): pass

What is the difference between the approach in Task 1 and this task? In Task 1 we have created an abstract class which cannot be instantiated. In Task 2 we only created abstract methods. The class can be instantiated but the methods cannot be called on it because they are abstract.

Task 3 (30 marks) In this part of the workshop, our focus will be on multiple inheritance and how we can implement it in Python. Please review the diagram below:

Employee2

Here, we have two base (parent) classes, Person and Worker, and one derived (child) class, i.e., Employee2. Your task here is to implement these three classes in Python. You should first implement the base classes before moving on to the derived class. The derived class must inherit from both base classes.

class Person: def __init__(self, first_name, last_name, date_of_birth): self.first_name = first_name self.last_name = last_name self.dob = date_of_birth def print_person_details(self): return 'Name: ' + self.first_name + ' ' + self.last_name + \ '\nDate of Birth: ' + self.dob class Worker: def __init__(self, tax_file_number, super_number): self.tax_file_number = tax_file_number self.super_number = super_number def get_info(self): return 'TFN: ' + str(self.tax_file_number) + \ '\nSuper: ' + str(self.super_number) class Employee2(Person, Worker): def __init__(self, first_name, last_name, date_of_birth, tax_file_number, super_number, employee_id, position): Person.__init__(self, first_name, last_name, date_of_birth) Worker.__init__(self, tax_file_number, super_number) self.employee_id = employee_id self.position = position def get_details(self): return self.print_person_details() + \ '\n------------------------' + \ '\nEmployee ID: ' + str(self.employee_id) + \ '\nPosition: ' + self.position + \ '\n------------------------\n' + \ self.get_info()

Create an instance and run it using the following code:

kim = Employee2('Kim', 'White', '12/08/2020', 4556655, 567, 1121, 'Developer') print(kim.get_details())

Output should look like this: Name: Kim White Date of Birth: 12/08/2020 -----------------------Employee ID: 1121 Position: Developer ------------------------

TFN: 4556655 Super: 567 Congratulations! You have finished - well done! Show your results to your supervisor in the class or for external students, submit it electronically  Please make sure you save and keep all of your practical and assignment work. You will need to save your work on a USB or the like. Please ask your supervisor if you are having difficulties doing so.

End of Practical 4...


Similar Free PDFs