IB Computer Science HL · Year 2 · Lesson 14

Single-Class OOP: Design → Code → Trace

Integrate the whole B3.1 skill set in one coherent Python class rather than treating each concept separately.

Paper 250 minutesB3.1.1–B3.1.5 application
B3.1 integration

One class, several examinable skills

2 / 10
  • 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.
B3.1.1B3.1.2B3.1.3B3.1.4B3.1.5
Learn + retrieve

Rapid Recall Deck

3 / 10

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”.

Design

Translate requirements before writing code

4 / 10
RequirementsA 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

Class structure — include the class name, attributes and methods.
Ownership — decide which state is shared by all sections and which state belongs to each object.
Behaviour — include methods that enforce the requirements rather than exposing raw state changes.
CourseSection
-------------------------
school_name
course_code
teacher
capacity
_enrolment
-------------------------
add_student()
spaces_left()
summary()
Construct

Implement the design in Python

5 / 10

Construct the Python class

Use a class variable for the shared school name.
Initialize instance state in __init__.
Protect enrolment as intended private state.
Reject additions when the section is full.
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.teacher
Trace + explain

State changes should follow class rules

6 / 10

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())
Track _enrolment after every call.
Track every returned value.

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.

Apply

Object creation and OOP judgement

7 / 10

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.

Exam lens

Mixed B3.1 practice

8 / 10

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.

  1. A new object is created, the constructor runs, the instance attributes are initialized, and the reference is assigned to x.
  2. school_name is shared class-level state; teacher belongs to an individual section and can differ between objects.
  3. def remove_student(self):
        if self._enrolment > 0:
            self._enrolment -= 1
Exit check

Can you diagnose a single-class OOP design?

9 / 10

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.

Homework

Finish the learning cycle

10 / 10

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.