← Back to Portfolio

Finals Lab Task 1

Encapsulation

Problem #1

For this program, you are required to define a class named Car with specific properties, methods, and a constructor. The class should have three properties: color (type: str) to represent the car’s color, price (type: float) to hold the car’s price, and size (type: str) to indicate the size of the car, where 'S' stands for small, 'M' for medium, and 'L' for large. The constructor init(self, color: str, price: float, size: str) initializes these properties, with the size value automatically converted to uppercase using size.upper(). The class should include getter methods (get_color, get_price, and get_size) that return the respective property values. It should also have setter methods (set_color, set_price, and set_size) to modify each property, ensuring that the size value is one of 'S', 'M', or 'L', and any lowercase input is converted to uppercase. Lastly, the str method should return a formatted string showing the car’s details in the format "Car (color) - P(price, formatted to two decimal places) - (size descriptor)", where the size descriptor corresponds to 'small', 'medium', or 'large' based on the size property. For example, a red car priced at 19999.85 with a medium size should display as "Car (red) - P19999.85 - medium", while a blue car priced at 50000.00 with a large size should display as "Car (blue) - P50000.00 - large". Each class must be saved in its own file using camelCase naming conventions, such as carClass.py.

Code

Main.py


from carClass import Car

def main():
    car1 = Car("red", 19999.85, "M")
    car2 = Car("blue", 50000.00, "L")
    car3 = Car("green", 12345.67, "S")

    print("Action: Invoking the Car class constructor using Car('red', 19999.85, 'M').")
    print("Output:")
    print(car1)

    print("\nAction: Invoking the Car class constructor using Car('blue', 50000.00, 'L').")
    print("Output:")
    print(car2)

    print("\nAction: Invoking the Car class constructor using Car('green', 12345.67, 'S').")
    print("Output:")
    print(car3)


if __name__ == "__main__":
    main()
        

carClass.py


class Car:
    def __init__(self, color: str, price: float, size: str):
        self.__color = color
        self.__price = price
        self.__size = size.upper()

    # Set
    def set_color(self, color: str) -> None:
        self.__color = color

    def set_price(self, price: float) -> None:
        self.__price = price

    def set_size(self, size: str) -> None:
        self.__size = size.upper()

    # Get
    def get_color(self) -> str:
        return self.__color

    def get_price(self) -> float:
        return self.__price

    def get_size(self) -> str:
        return self.__size

    # String
    def __str__(self) -> str:
        size_desc = {
            'S': 'small',
            'M': 'medium',
            'L': 'large'
        }.get(self.__size, 'unknown')

        return f"Car ({self.__color}) - ₱{self.__price:.2f} - {size_desc}"
        

Output


Action: Invoking the Car class constructor using Car('red', 19999.85, 'M').
Output:
Car (red) - ₱19999.85 - medium

Action: Invoking the Car class constructor using Car('blue', 50000.00, 'L').
Output:
Car (blue) - ₱50000.00 - large

Action: Invoking the Car class constructor using Car('green', 12345.67, 'S').
Output:
Car (green) - ₱12345.67 - small
        
View PDF Here