COMP 1046 Object-Oriented Programming
SAIBT
SAMPLE EXAMINATION SOLUTION
COMP 1046 Object-Oriented Programming
Page 1 of 10
COMP 1046 Object-Oriented Programming
PART 1: SHORT ANSWER QUESTIONS. Total 40 marks.
Five short answer questions. 8 marks for each question.
1. Class definition (8 marks): Write Python code to implement the class
in the following UML diagram:
Include an initialization method. No testing of the datatype is required. Only the method skeleton is required. No code inside the methods is needed.
WRITE YOUR ANSWER HERE.
SOLUTION:
class Car:
def __init__(self, numberOfDoors, vinNumber):
self.numberOfDoors = numberOfDoors self._vinNumber_=vinNumber
def printManual(self):
pass
Page 2 of 10
2. UML Relationship (8 marks): Describe the relationship in the diagram between classes Building and FamilyHouse:
Name the relationship and explain in your own words the meaning of it. Explain how a FamilyHouse object can access the attributes rooms and size specified in the Building class.
WRITE YOUR ANSWER HERE.
SOLUTION:
The relationship is called inheritance. The FamilyHouse class inherits all attributes and methods from Building.
The attributes rooms and size can be access directly in the FamilyHouse object using the dot notation. For example,
f=FamilyHouse() f.rooms = 4 f.size = 250
COMP 1046 Object-Oriented Programming
Page 3 of 10
3. Iterator Pattern (8 marks):
Given is the following class EventsIterable in a UML diagram.
Extend the UML class diagram with an iterator class. Specify in the diagram which attributes and/or methods need to be implemented so the following code works in Python.
events = EventsIterable([“Easter”, “Halloween”,
“Christmas”],[“12/04/2020”, “31/10/2020”, “25/12/2020”])
for e in events
print(e)
WRITE YOUR ANSWER HERE.
COMP 1046 Object-Oriented Programming
SOLUTION:
Page 4 of 10
4. Data Structures (8 marks):
Given are the following two sets of TV shows:
tv_shows1={“Stranger Things”, “Discovery”, “Mandalorian”,
“Utopia”, “Black Mirror”}
tv_shows2={“Picard”, “Stranger Things”, “Dark”,
“Utopia”, “Knight Rider”}
Write down the resultings sets x, y, and z of the following code and explain each operator in a single sentence:
x=tv_shows1.union(tv_shows2)
y=tv_shows1.intersection(tv_shows2)
z=tv_shows1.symmetric_difference(tv_shows2)
WRITE YOUR ANSWER HERE.
SOLUTION:
x={‘Picard’, ‘Mandalorian’, ‘Knight Rider’, ‘Utopia’, ‘Black Mirror’, ‘Discovery’, ‘Stranger Things’, ‘Dark’}
The Union combines all values in the first and all values in the second set and removes duplicates. In other words it combines all values that are part in the first or the second set.
y={‘Utopia’, ‘Stranger Things’}
The Intersection contains only those values that are in both sets.
z={‘Black Mirror’, ‘Picard’, ‘Knight Rider’, ‘Dark’, ‘Mandalorian’, ‘Discovery’}
The symmetric difference contains all values that are in either sets but not in both.
COMP 1046 Object-Oriented Programming
Page 5 of 10
5. Testing (8 marks): Explain the output of unittest on stdout.
.F ============================================================ FAIL: test_str_float (__main__.CheckNumbers) ————————————————————–
Traceback (most recent call last):
File “first_unittest.py”, line 9, in test_str_float self.assertEqual(1, “1”)
AssertionError: 1 != ‘1’ ————————————————————– Ran 2 tests in 0.001s
FAILED (failures=1)
Explain the output of the unittest:
How many tests have been performed? What kind of tests have been performed? Did the tests pass or not? What was the expected value in each test?
WRITE YOUR ANSWER HERE.
SOLUTION:
There are two tests performed indicated by “.F” in the first line and also mentioned in the second last line “Ran 2 tests”.
The type of the first test is not revealed because it passed successfully. The second test failed and was an assert equal test where it is tested if the number 1 is equal to the string “1”. It fails because Python does not consider both equal.
COMP 1046 Object-Oriented Programming
Page 6 of 10
PART 2: OO DESIGN. Total 30 marks.
Design a UML Class Diagram for a student enrollement system. The system should cover the following requirements:
• Student
• Course
• Class
• Practical
• Workshop • Tutorial
• Enrollment
A student has a student id, name, address and contact phone number. A student is able to enrol into a course.
A student is able to enrol into a class of course. The class can be a practical, workshop or tutorial.
Each class has a weekday, time and room number. Each class has a maximum capacity.
Students can also unenroll from a class and a course.
The diagram should include classes, attributes and methods in classes, and relationships between classes.
Attributes must contain datatypes. The exact names from the requirements above should be used to name the classes and attributes. All classes must be related to at least one other class. All relationships except inheritance must contain a label and cardinalities.
Methods must contain parameters if applicable and a return type.
INSERT YOUR DIAGRAM HERE.
COMP 1046 Object-Oriented Programming
Page 7 of 10
SOLUTION:
COMP 1046 Object-Oriented Programming
Page 8 of 10
COMP 1046 Object-Oriented Programming
PART 3: OO Programming. Total 30 marks.
Implement the following UML class diagram in Python 3. It represents a library management system.
• Implement all classes by declaring the classes, creating initialisation methods and attributes.
• Implement the composition and association relationship by attributes and methods in the appropriate classes to establish the relationship between the objects of related classes.
INSERT YOUR CODE HERE.
SOLUTION:
class Catalog:
def __init__(self,libraryItems):
self.libraryItems=libraryItems def search(self):
pass
class LibraryItem:
def __init__(self,title,subject,contributors,contributorsWithType): self.title=title
self.subject=subject
self.contributors=contributors
Page 9 of 10
COMP 1046 Object-Oriented Programming
# association relationship writtenBy
self.writtenBy=contributorsWithType def locate(self):
pass
class ContributorWithType:
def __init__(self,type,name): self.type=type
# Compositon relationship means that the object is contained in compos ite object:
self.contributor=Contributor(name)
# Association relationship writtenBy in the direction from Contributor WithType to LibraryItem:
self.writtenWorks = []
# For the association relationship writtenBy in the direction from Contrib utorWithType to LibraryItem:
def addLibraryItem(self,libItem): self.writtenWorks.append(libItem)
class Contributor:
def __init__(self,name):
self.name=name
class Book(LibraryItem):
def __init__(self,title,subject,contributors,isbn):
super().__init__(title, subject, contributors) self.isbn=isbn
class Magazine(LibraryItem):
def __init__(self,title,subject,contributors,volume,issue):
super().__init__(title, subject, contributors) self.volume=volume
self.issue=issue
class DVD(LibraryItem):
def __init__(self,title,subject,contributors,genre,rating):
super().__init__(title, subject, contributors) self.genre=genre
self.rating=rating
Page 10 of 10