Unit 11.4A · Term 4

Applied OOP Problems

Real-world software is complex. Decomposition breaks down large problems into manageable classes and objects. This lesson focuses on applying OOP principles to solve practical problems in various domains.

Learning Objectives

  • 11.3.2.5 Define class and instance identifiers in the proposed code snippet
  • 11.3.3.4 Solve applied problems from various subject areas

Lesson Presentation

11.4A-lesson-25-oop-problems.pdf · Slides for classroom use

Conceptual Anchor

The Assembly Line Analogy

Building a car is a huge task. But an assembly line decomposes it: the Engine team focuses on power, the Chassis team on structure, and the Interior team on comfort. In OOP, each "team" is a Class, handling its own data and logic, eventually coming together to form the complete application.

Rules & Theory

Decomposition (11.4.3.1)

When faced with a problem, ask:

  • What are the "nouns"? These become your Classes (e.g., Student, Course, Grade).
  • What are the "verbs"? These become your Methods (e.g., enroll, grade_assignment).
  • What data does each need? These become Attributes (e.g., student_id, score).

Example: Library System

Nouns: Book, Member, Library
Verbs: Borrow, Return, Check Availability

class Book: def __init__(self, title, author): self.title = title self.author = author self.is_borrowed = False def borrow(self): if not self.is_borrowed: self.is_borrowed = True return True return False def return_book(self): self.is_borrowed = False class Member: def __init__(self, name): self.name = name self.borrowed_books = [] def borrow_book(self, book): if book.borrow(): self.borrowed_books.append(book) print(f"{self.name} borrowed {book.title}") else: print(f"{book.title} is not available")

Worked Examples

1 E-Commerce Shopping Cart

Problem: Calculate total price of items in a cart, applying discounts.

class Product: def __init__(self, name, price): self.name = name self.price = price class Cart: def __init__(self): self.items = [] def add(self, product): self.items.append(product) def total_price(self): return sum(item.price for item in self.items) p1 = Product("Laptop", 1000) p2 = Product("Mouse", 20) cart = Cart() cart.add(p1) cart.add(p2) print(f"Total: ${cart.total_price()}")

2 Geometry: Area Calculator

Problem: Calculate area of different shapes using inheritance.

class Shape: def area(self): pass class Rect(Shape): def __init__(self, w, h): self.w, self.h = w, h def area(self): return self.w * self.h class Circle(Shape): def __init__(self, r): self.r = r def area(self): return 3.14 * self.r**2 shapes = [Rect(10,5), Circle(2)] total_area = sum(s.area() for s in shapes) print(f"Total Area: {total_area}")

Pitfalls & Common Errors

Over-Coupling

Don't make classes too dependent on each other. A Book shouldn't need to know about a Member to exist. Keep classes independent where possible.

God Class

Avoid creating one massive class (e.g., SystemManager) that does everything. Break it down into smaller, focused classes.

Pro-Tips for Exams

OOP Problem-Solving Strategy

  • Nouns become Classes (e.g., Student, Book, Cart).
  • Adjectives/State become Attributes (e.g., name, price, is_available).
  • Verbs become Methods (e.g., enroll(), checkout(), calculate_discount()).
  • Store collections of objects in standard Python structures: catalog = [Book("1984"), Book("Dune")].

Graded Tasks

Remember

What are the three main questions to ask when decomposing a problem?

Understand

Why is it better to have many small classes than one large class?

Apply

Design a TodoList system with Task objects. Each task has a description and a completed status.

Create

Create a simple RPG Battle System. Create Hero and Monster classes, each with health and attack power. Simulate a turn-based fight until one falls.

Self-Check Quiz

1. In OOP problem analysis, real-world verbs typically map to what code construct?
Click to reveal answer
2. What architectural antipattern occurs when one class controls all program logic?
Click to reveal answer
3. How can a shopping cart class hold multiple product instances?
Click to reveal answer