CPSC 203, 2025 W2
January 21, 2026
A bracelet contains a list of beads and has a radius.
This is composition: a Bracelet contains Beads.
# First, create some beads
b1 = Bead(radius=5.0, colour=(255, 0, 0))
b2 = Bead(radius=3.0, colour=(0, 255, 0))
b3 = Bead(radius=4.0, colour=(0, 0, 255))
# Then, create a bracelet with those beads
my_bracelet = Bracelet(radius=20.0, beads=[b1, b2, b3])
# Access the beads
print(len(my_bracelet.beads)) # 3
print(my_bracelet.beads[0].radius) # 5.0
print(my_bracelet.beads[1].colour) # (0, 255, 0)The beads fit on the bracelet if the sum of their diameters is less than or equal to the circumference.
What do bracelet1.fits() and bracelet2.fits() return?
02:00
@dataclass
class Bracelet:
radius: float
beads: list[Bead]
def circumference(self) -> float:
return 2 * math.pi * self.radius
def fits(self) -> bool:
total = 0
for bead in self.beads:
total += bead.diameter()
return total <= self.circumference()
def slack(self) -> float:
total = 0
for bead in self.beads:
total += bead.diameter()
return self.circumference() - totalThe bracelet decides where each bead goes (around the circle).
def draw(self) -> None:
if not self.fits():
print("Beads don't fit!")
return
spacing = self.slack() / len(self.beads)
arc_position = 0
for bead in self.beads:
arc_position += bead.radius + spacing / 2
angle = arc_position / self.radius
x = self.radius * math.cos(angle)
y = self.radius * math.sin(angle)
bead.draw(ax, x, y)
arc_position += bead.radius + spacing / 2@dataclass
class Bead:
radius: float
colour: tuple[int, int, int]
def diameter(self) -> float:
return 2 * self.radius
def draw(self, ax, x: float, y: float) -> None:
# implementation uses matplotlib
...
@dataclass
class Bracelet:
radius: float
beads: list[Bead]
def circumference(self) -> float: ...
def fits(self) -> bool: ...
def slack(self) -> float: ...
def draw(self) -> None: ...Try the bracelet code on PrairieLearn!
10:00
Identify the objects in your problem (nouns)
List the data each object needs (attributes)
List the operations each object supports (methods)
Decide which object is responsible for what
Implement and test incrementally
Now let’s apply these ideas to a new domain…








![]()
![]()
![]()
A handcraft is a collection of __________. Every __________ has a __________, and a collection of __________. Every __________ is a collection of __________. Every __________ is either “knit” or “purl,” and is drawn as a __________.
Mechanism for creating user-defined types.
Used to identify attributes with an object. Associates functionality with the relevant objects.
Example:
Declaring an object of type color():
Accessing the data of an object of type color():
Decompose a problem into classes
List the data associated with each class
Write the “driver” code that illustrates the functionality you expect from each class.
Implement the functions you expect.
Run the driver code to test your functionality.

Handcraft:
Block:
Row:
Stitch:
Image:
https://us.prairielearn.com/pl/course_instance/193300/assessment/2588207