Complete the code to define an association relationship between two classes.
class Teacher: def __init__(self, name): self.name = name class Student: def __init__(self, name, teacher): self.name = name self.teacher = [1]
The student holds a reference to the teacher object, representing an association.
Complete the code to show aggregation where a Library has many Books.
class Book: def __init__(self, title): self.title = title class Library: def __init__(self): self.books = [] def add_book(self, book): self.books.[1](book)
Aggregation means the library holds references to book objects. We add books to the list using append.
Fix the error in the composition example where a Car has an Engine.
class Engine: def __init__(self, power): self.power = power class Car: def __init__(self, power): self.engine = [1](power)
In composition, the Car creates and owns the Engine instance. We must call the Engine class to create it.
Fill both blanks to complete the aggregation example where a Team has multiple Players.
class Player: def __init__(self, name): self.name = name class Team: def __init__(self): self.players = [] def add_player(self, player): self.players.[1](player) def remove_player(self, player): self.players.[2](player)
To add a player, use append. To remove a specific player, use remove.
Fill all three blanks to complete the composition example where a House has Rooms with names and sizes.
class Room: def __init__(self, name, size): self.name = [1] self.size = [2] class House: def __init__(self): self.rooms = [] def add_room(self, name, size): room = [3](name, size) self.rooms.append(room)
The Room constructor assigns name and size to its attributes. The House creates Room instances to compose rooms.