Module 5: Introduction to Object-Oriented Programming (OOP)
Lab: Lab 5.1: Class Builder - My First Object
Create a Car
class. The constructor (__init__
) should take brand
and year
as arguments and store them as attributes. Add a method called display_info()
that prints a message like "This is a 2020 Toyota.". Create an instance of your Car class and call its display_info()
method.
Tasks to Complete:
- Define a class named `Car`.
- Inside the `Car` class, define the `__init__` method. It should accept `self`, `brand`, and `year` as parameters and assign `brand` and `year` to instance attributes (e.g., `self.brand = brand`).
- Define another method named `display_info` within the `Car` class. It should take `self` as a parameter.
- Inside `display_info`, print a formatted string like "This is a [year] [brand]." using the instance attributes.
- Create an object (instance) of the `Car` class, providing a brand (e.g., "Toyota") and a year (e.g., 2020).
- Call the `display_info()` method on the object you created.
Python Sandbox
Python execution is powered by Pyodide, running directly in your browser. The `input()` function is not supported in this sandbox; please define variables directly in your code.