Module 29: OOP Practice

Let's build some blueprints and create objects!

Dissecting a Class

Classes are blueprints. Click on the highlighted parts of this simple `Robot` class definition to understand its components.

class Robot:
    def __init__(self, name):
        self.name = name  # Store the robot's name

Identify all parts!

Build an `__init__` Method

The `__init__` method initializes an object. Drag the pieces to construct the `__init__` method for a `Car` class that takes `make` and `model` as input and stores them.

Drop pieces here for the __init__ method...

Instance Creation Simulator

We've defined a `Book` class below. In the editor, write code to: 1. Create an instance (object) of the `Book` class called `my_book` with title "AI Adventures". 2. Print the `title` attribute of `my_book`.

Provided Class Definition:

class Book:
    def __init__(self, title):
        self.title = title
        self.pages = 100 # Default value
Output will appear here...

Spot the Attribute Assignment

Instance attributes store an object's data. They are usually assigned inside `__init__` using `self.attribute_name = value`. Click the line(s) below that assign instance attributes.

OOP Mini Missions!

Combine your knowledge! Complete these small OOP tasks. The simulator checks your code's structure and basic output.

Mission 1: Define a `Cat` Class

Define a class named `Cat`. Inside it, define an `__init__` method that takes `self` and `name` as parameters. Inside `__init__`, assign the `name` parameter to an instance attribute called `name` (i.e., `self.name = name`).

Mission 2: Create & Print

Assuming the `Cat` class from Mission 1 exists (you don't need to redefine it here), write code to: 1. Create an instance of `Cat` named `whiskers` with the name "Whiskers". 2. Print the `name` attribute of the `whiskers` object.

Mission 3: Add a Method

Modify the `Cat` class definition (you'll need to write the full class again here). Add a method called `meow` that takes only `self` as a parameter and prints the string "Meow!".

Module 29 Practice Complete!

Excellent work building classes and creating objects! You're grasping the core of OOP.
Ready for more? Try the Advanced Practice for Module 29.