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.4.3.1 Decompose an applied task
- 11.4.3.2 Solve applied problems of various subject areas
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.
Graded Tasks
What are the three main questions to ask when decomposing a problem?
Why is it better to have many small classes than one large class?
Design a TodoList system with Task objects. Each task has a
description and a completed status.
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.