Vehicle Classes - Python Programming Exercise

In this exercise, you will develop a Python project with corresponding classes, spread across multiple files, based on the provided class diagram. Each class should have attributes and methods as specified in the diagram, including Get and Set methods for the "Vehicle" class, and "Has" methods (like "HasDualSlidingDoors") for the "MiniVan" class. This exercise is perfect for practicing class definition, inheritance, and project organization in Python. By implementing this project, you will gain hands-on experience in handling class definitions, inheritance, and project organization in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

OOP Object-Oriented Programming

 Exercise

Vehicle Classes

 Objective

Develop a Python project with corresponding classes, spread across multiple files, based on the provided class diagram. Each class should have attributes and methods as specified in the diagram, including Get and Set methods for the "Vehicle" class, and "Has" methods (like "HasDualSlidingDoors") for the "MiniVan" class.

Additionally, create a test program that instantiates objects from each class and calls a "Drive" method on them.

 Example Python Exercise

 Copy Python Code
# vehicle.py
class Vehicle:
    def __init__(self, make, model, year):
        self.make = make  # The make of the vehicle (e.g., Toyota)
        self.model = model  # The model of the vehicle (e.g., Corolla)
        self.year = year  # The year the vehicle was made (e.g., 2020)

    def get_make(self):
        return self.make  # Get the make of the vehicle

    def set_make(self, make):
        self.make = make  # Set the make of the vehicle

    def get_model(self):
        return self.model  # Get the model of the vehicle

    def set_model(self, model):
        self.model = model  # Set the model of the vehicle

    def get_year(self):
        return self.year  # Get the year of the vehicle

    def set_year(self, year):
        self.year = year  # Set the year of the vehicle

    def drive(self):
        print(f"The {self.year} {self.make} {self.model} is driving.")  # Drive method


# minivan.py
class MiniVan(Vehicle):
    def __init__(self, make, model, year, has_dual_sliding_doors):
        super().__init__(make, model, year)  # Call to the parent class constructor
        self.has_dual_sliding_doors = has_dual_sliding_doors  # New attribute for MiniVan

    def has_dual_sliding_doors(self):
        return self.has_dual_sliding_doors  # Check if the minivan has dual sliding doors

    def set_dual_sliding_doors(self, has_dual_sliding_doors):
        self.has_dual_sliding_doors = has_dual_sliding_doors  # Set the dual sliding doors attribute

    def drive(self):
        print(f"The {self.year} {self.make} {self.model} Minivan is driving.")  # Minivan-specific drive
        if self.has_dual_sliding_doors:
            print("This Minivan has dual sliding doors.")
        else:
            print("This Minivan does not have dual sliding doors.")


# Main program to test the classes
if __name__ == '__main__':
    # Create a Vehicle object
    vehicle1 = Vehicle("Toyota", "Corolla", 2020)
    vehicle1.drive()  # Call the drive method for Vehicle

    # Create a MiniVan object with dual sliding doors
    minivan1 = MiniVan("Honda", "Odyssey", 2021, True)
    minivan1.drive()  # Call the drive method for MiniVan

    # Create another MiniVan object without dual sliding doors
    minivan2 = MiniVan("Chrysler", "Pacifica", 2022, False)
    minivan2.drive()  # Call the drive method for MiniVan

 Output

The 2020 Toyota Corolla is driving.
The 2021 Honda Odyssey Minivan is driving.
This Minivan has dual sliding doors.
The 2022 Chrysler Pacifica Minivan is driving.
This Minivan does not have dual sliding doors.

 Share this Python Exercise

 More Python Programming Exercises of OOP Object-Oriented Programming

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Class Rectangle

    In this exercise, you will develop a Python project called "Shapes," incorporating a class named "Square." Each square will store the starting X and Y coordinates (re...

  •  Class Purchases

    In this exercise, you will develop a Python project with the necessary classes (distributed across multiple files) based on the given class diagram. Each class should...

  •  Class Colored Sphere

    In this exercise, you will develop a Python project and extend the previous exercise to include "Colored Circles". Each circle should have a radius, center coordinate...

  •  Classes: Learner and Instructor

    In this exercise, you will develop a Python program that incorporates a Person class. Then, create two additional classes, Student and Teacher, that inherit from Pers...

  •  Class Picture Collection

    In this exercise, you will develop a Python program with a class named "PhotoAlbum" that includes a private attribute "pageCount." This exercise is perfect for...

  •  Class GeometricShapes

    In this exercise, you will develop a Python project with the required classes, organizing them into separate files, according to a class diagram. This exercise...