Single-Class OOP: Design → Code → Trace
Integrate the whole B3.1 skill set in one coherent Python class rather than treating each concept separately.
One class, several examinable skills
- design a class from requirements using UML;
- choose class-level versus instance-level state;
- construct a constructor, methods and object instantiation;
- apply encapsulation to protect state;
- evaluate whether OOP is appropriate for the application.
Rapid Recall Deck
Attempt each card first. If a term is new, flip and study it, then answer it again without looking. Mark only secure recall as “Know it”.
Translate requirements before writing code
CourseSection belongs to one school. Each section has a course code, teacher, maximum capacity and current enrolment. Enrolment must never exceed capacity. The class must support adding one student, reporting spaces left and returning a short summary.Construct the UML class design
CourseSection ------------------------- school_name course_code teacher capacity _enrolment ------------------------- add_student() spaces_left() summary()
Implement the design in Python
Construct the Python class
__init__.class CourseSection:
school_name = "North Campus"
def __init__(self, course_code, teacher, capacity):
self.course_code = course_code
self.teacher = teacher
self.capacity = capacity
self._enrolment = 0
def add_student(self):
if self._enrolment < self.capacity:
self._enrolment += 1
return True
return False
def spaces_left(self):
return self.capacity - self._enrolment
def summary(self):
return self.course_code + " - " + self.teacherState changes should follow class rules
Trace object state
cs = CourseSection("CSHL", "Ms Lee", 2)
print(cs.add_student())
print(cs.add_student())
print(cs.add_student())
print(cs.spaces_left())
_enrolment after every call.The outputs are True, True, False, 0. Enrolment changes 0 → 1 → 2 and then remains 2 because capacity has been reached.
Explain the design
Why is _enrolment better controlled through add_student() than directly modified by outside code?
The method enforces the capacity rule before changing state. Direct outside assignment could bypass the rule and create an impossible enrolment value.
Object creation and OOP judgement
Construct objects
Create two CourseSection objects with different course codes, teachers and capacities. Then print the shared school name through the class.
a = CourseSection("CSHL", "Ms Lee", 18)
b = CourseSection("BIO1", "Mr Khan", 22)
print(CourseSection.school_name)Evaluate the paradigm
Explain one reason OOP is appropriate for a system containing many course-section objects, and one limitation if this were only a tiny one-off calculation.
OOP supports many independent section objects with the same rules and behaviour, improving modelling and reuse. For a tiny one-off calculation, defining a class could add unnecessary structure compared with a direct function or procedural solution.
Mixed B3.1 practice
Paper 2 mini-set
1. List the important operations that occur when x = CourseSection("MATH", "Dr Cruz", 20) executes.
2. Distinguish school_name from teacher in terms of class-level versus instance-level state.
3. Construct a method remove_student() that decreases enrolment only when enrolment is above 0.
- A new object is created, the constructor runs, the instance attributes are initialized, and the reference is assigned to
x. school_nameis shared class-level state;teacherbelongs to an individual section and can differ between objects.def remove_student(self): if self._enrolment > 0: self._enrolment -= 1
Can you diagnose a single-class OOP design?
Exit check
A class has a constructor, but all of its data is stored in class variables even though each object represents a different customer. Explain the design error.
Customer-specific values should be instance variables. Class variables would incorrectly share the same state across all customer objects.
Finish the learning cycle
IA — main task
Continue Criterion D development/testing and update your evidence log with one completed feature and one test result.
Paper 2 — short practice
Reconstruct the CourseSection class from requirements without looking at the model; then redo the three-part mini-set.