# Initialize Otter
import otter
grader = otter.Notebook("11_lab_object_oriented_programming.ipynb")
Summer Travel Planner¶
In this lab, you will build three classes from scratch that help plan a summer trip. Each class follows the same core pattern: define the class, write __init__ to store data using self, and add methods that work with that data.
By the end, you will have written:
- A
Destinationclass that stores where you want to go - A
PackingListclass that tracks what to bring - A
Tripclass that ties your travel plans together
# Validation: Check Python version and file location
import os
import sys
# Supported Python versions for this course
SUPPORTED_VERSIONS = ["3.12", "3.13"]
current_version = f"{sys.version_info.major}.{sys.version_info.minor}"
if current_version not in SUPPORTED_VERSIONS:
raise EnvironmentError(
f"Python version mismatch!\n"
f"Supported: {', '.join(SUPPORTED_VERSIONS)}\n"
f"Found: {current_version}\n"
f"Please use Python 3.12 or 3.13 for this course."
)
print(f"Python version: {current_version}")
# Expected filename for this assignment
EXPECTED_FILE = "11_lab_object_oriented_programming.ipynb"
# Check file is in the correct directory
current_path = os.path.abspath("")
required_path_segment = os.path.join("Documents", "CSC115", "lab_notebooks")
allowed_envs = ["submission", "dist", "live", "source"]
in_allowed_env = any(env in current_path for env in allowed_envs)
if required_path_segment not in current_path and not in_allowed_env:
raise EnvironmentError(
f"File location error!\n"
f"This notebook should be in: ~/Documents/CSC115/lab_notebooks/\n"
f"Current location: {current_path}\n"
f"Please move this file to the correct directory."
)
# Verify filename (skip in grading/development environments)
if not in_allowed_env:
if not os.path.exists(EXPECTED_FILE):
notebooks = [f for f in os.listdir(".") if f.endswith(".ipynb")]
raise EnvironmentError(
f"Filename mismatch!\n"
f"Expected: {EXPECTED_FILE}\n"
f"Found notebooks: {notebooks}\n"
f"Please rename your notebook to '{EXPECTED_FILE}'."
)
print(f"File location: {os.path.join(current_path, EXPECTED_FILE)}")
Example: The Transportation Class¶
Before you start writing your own classes, here is a complete class that is already written for you. Read through it carefully — your classes will follow the same pattern.
Notice the structure:
class Transportation:starts the class definitiondef __init__(self, ...)runs automatically when you create a new instance — it stores the data- Each piece of data is saved with
self.something = something - Other methods use
selfto access the stored data
# PROVIDED — run this cell, do not edit it
class Transportation:
"""Stores information about how a traveler is getting to their destination."""
def __init__(self, mode, origin, destination, cost):
self.mode = mode # e.g. "flight", "train", "bus"
self.origin = origin # departure city
self.destination = destination # arrival city
self.cost = cost # total cost in dollars
# Create an instance — this calls __init__ automatically
my_flight = Transportation("flight", "Miami", "Tokyo", 850)
# Access attributes with dot notation
print(f"Mode: {my_flight.mode}")
print(f"From: {my_flight.origin}")
print(f"To: {my_flight.destination}")
print(f"Cost: ${my_flight.cost}")
When you create Transportation("flight", "Miami", "Tokyo", 850), Python automatically calls __init__ and passes those four values in. The self parameter is handled by Python — you never pass it yourself.
You will use this Transportation class later in the lab. For now, focus on the pattern: class, __init__, self, dot notation.
1. Destination¶
Now it is your turn. You will write a Destination class that stores information about a place you want to visit this summer.
You will build this class in two steps:
- Q1: Write the class with
__init__to store data - Q2: Add a method to your class
Question 1: Write the Destination Class (3 points)¶
Write a class called Destination that stores information about a travel destination. Your __init__ method should accept and store three attributes:
- The city name (like "Tokyo" or "Barcelona")
- The country (like "Japan" or "Spain")
- The trip budget in dollars (a number like 2500)
Look at the Transportation class above for reference — your structure will be very similar.
After writing your class, create a Destination for any place you would like to visit and store it in q1_destination. Then access its city, country, and budget attributes and store them in q1_city, q1_country, and q1_budget.
# ✏️ YOUR ANSWER HERE
class Destination:
# Write your __init__ here, then add your method for Q2
...
...
q1_destination = ...
q1_city = ...
q1_country = ...
q1_budget = ...
print(f"Destination: {q1_city}, {q1_country}")
print(f"Budget: ${q1_budget}")
grader.check("q1_destination_class")
Question 2: Add is_affordable to Destination (3 points)¶
Go back to your Destination class above and add a new method.
This method should accept the amount of money the traveler has available to spend (as a parameter — do not store it on the class). It should return True if the traveler has enough money to cover the budget, or False if not.
After adding the method to your class above, re-run that cell, then create your test variables below:
- Create a destination to Rome, Italy with a budget of 2000 and store it in
q2_destination - Call your method with 2500 available to spend and store the result in
q2_can_afford - Call your method with 1500 available to spend and store the result in
q2_cannot_afford
# ✏️ YOUR ANSWER HERE
q2_destination = ...
q2_can_afford = ...
q2_cannot_afford = ...
print(f"Can afford Rome with $2500? {q2_can_afford}")
print(f"Can afford Rome with $1500? {q2_cannot_afford}")
grader.check("q2_is_affordable")
2. PackingList¶
Time to write your second class from scratch. This one tracks what a traveler needs to bring.
This class introduces a new idea: __init__ can create attributes that are not passed in as arguments. For example, you might want every new packing list to start with an empty list of items — you create it inside __init__ rather than passing it in.
You will build this class in two steps:
- Q3: Write the class with
__init__and a method to add items - Q4: Add two more methods to your class
Question 3: Write the PackingList Class (3 points)¶
Write a class called PackingList that tracks what a traveler needs to bring on their trip.
Your __init__ method should:
- Accept the traveler's name and store it as an attribute
- Create an attribute that starts as an empty list to hold packed items (this is not passed in — you create it inside
__init__)
Also write a method that accepts an item name and adds it to the list of packed items.
After writing your class:
- Create a
PackingListfor a traveler named "Alex" and store it inq3_packing_list - Add "sunscreen" and "passport" to the list using your method
- Store the list of items in
q3_items_after_add
# ✏️ YOUR ANSWER HERE
class PackingList:
# Write your __init__ here, then add more methods for Q4
...
...
...
...
q3_packing_list = ...
...
...
q3_items_after_add = ...
print(f"{q3_packing_list.traveler_name}'s packing list: {q3_items_after_add}")
grader.check("q3_packing_list_class")
Question 4: Add More Methods to PackingList (3 points)¶
Go back to your PackingList class above and add two new methods:
- A method that returns how many items have been packed so far
- A method that accepts an item name and returns whether that item is in the list (True or False)
After adding the methods to your class above, re-run that cell, then create your test variables below:
- Create a
PackingListfor "Sam" and store it inq4_packing_list - Add "towel", "charger", and "sunscreen" to the list
- Store the total number of packed items in
q4_item_count - Check whether "sunscreen" has been packed and store the result in
q4_has_sunscreen
# ✏️ YOUR ANSWER HERE
q4_packing_list = ...
...
...
...
q4_item_count = ...
q4_has_sunscreen = ...
print(f"Items packed: {q4_item_count}")
print(f"Has sunscreen? {q4_has_sunscreen}")
grader.check("q4_packing_list_methods")
3. Trip¶
For your final class, you will write a Trip that ties your travel plans together. This class will use the Transportation class from the top of the notebook — showing how one class can store an instance of another class as an attribute.
When your Trip stores a Transportation instance, you can access the transportation's data using chained dot notation. For example, if you store it as self.transportation, then self.transportation.cost gives you the cost.
Question 5: Write the Trip Class (4 points)¶
Write a class called Trip that pulls together a traveler's plans.
Your __init__ method should:
- Accept a
Transportationinstance and the number of days for the trip - Store both as attributes
- Create an attribute that starts as an empty list of planned activities
Also write two methods:
- A method that accepts an activity name and adds it to the activities list
- A method that calculates the daily budget by dividing the transportation cost by the number of days (access the cost from the transportation instance using dot notation)
After writing your class:
- Create a
Transportationfor a flight from Miami to Barcelona costing 700, and store it inq5_transport - Create a
Tripusing that transportation for 5 days, and store it inq5_trip - Add "beach" and "museum" as activities
- Store the daily budget in
q5_daily_budget - Store the number of planned activities in
q5_activity_count
# ✏️ YOUR ANSWER HERE
class Trip:
# Replace below with your __init__ and methods
...
...
...
q5_transport = ...
q5_trip = ...
...
...
q5_daily_budget = ...
q5_activity_count = ...
print(f"Trip to {q5_transport.destination} for {q5_trip.num_days} days")
print(f"Daily budget: ${q5_daily_budget}")
print(f"Activities planned: {q5_activity_count}")
grader.check("q5_trip_class")
Try it out¶
Run the cell below to see everything working together.
# Build a complete trip from scratch
my_transport = Transportation("flight", "Miami", "Tokyo", 1200)
my_trip = Trip(my_transport, 7)
my_trip.add_activity("visit temples")
my_trip.add_activity("try ramen")
my_trip.add_activity("explore Shibuya")
print(f"Trip to {my_transport.destination} for {my_trip.num_days} days")
print(f"Daily budget: ${my_trip.daily_budget():.2f}")
print(f"Activities: {my_trip.activities}")
Submitting to Gradescope¶
This file is saved on your computer — you do not need to download it from Jupyter.
Find the file on your computer:
- Mac: Open Finder →
Documents/CSC115/lab_notebooks/11_lab_object_oriented_programming/11_lab_object_oriented_programming.ipynb - Windows: Open File Explorer →
Documents\CSC115\lab_notebooks\11_lab_object_oriented_programming\11_lab_object_oriented_programming.ipynb
- Mac: Open Finder →
Upload to Gradescope:
- Go to gradescope.com and open this assignment
- Click "Submit" and upload the
.ipynbfile - The filename must be exactly
11_lab_object_oriented_programming.ipynb
Do NOT use File > Download in Jupyter — this can rename the file and cause submission errors.
You have completed the Object Oriented Programming notebook.
To double-check your work, the cell below will rerun all of the autograder tests.
grader.check_all()