Car Class
Write a class named Car that has the following data attributes:
• __year_model (for the car’s year model)
• __make (for the make of the car)
• __speed (for the car’s current speed)
The Car class should have an __init__ method that accept the car’s year model and make as arguments. These values should be assigned to the object’s __year_model and __make data attributes. It should also assign 0 to the __speed data attribute.
The class should also have the following methods:
• accelerate
The accelerate method should add 5 to the speed data attribute each time it is called.
• brake
The brake method should subtract 5 from the speed data attribute each time it is called.
• get_speed
The get_speed method should return the current speed.
Next, design a program that creates a Car object, and then calls the accelerate method five times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five t
Car Class Program code:
#Create a class name, Car
class Car:
#Create data attributes for class, Car
# __init__ method that accept the car’s
#year model and make as arguments.
def __init__(self, year_model, make):
self.__year_model = year_model
self.__make = make
self.__speed = 0
#accelerate method should add 5 to the speed data
#attribute each time it is called.
def accelerate(self):
self.__speed += 5
#brake method should subtract 5 from the
#speed data attribute each time it is called.
def brake(self):
if self.__speed >= 5:
self.__speed -= 5
# get_speed method should return the current speed.
def get_speed(self):
return self.__speed
# Create a Car object
car = Car("2023", "Hyndai")
# Accelerate the car five times
for _ in range(5):
car.accelerate()
print("Current speed after acceleration:", car.get_speed())
# Brake the car five times
for _ in range(5):
car.brake()
print("Current speed after braking:", car.get_speed())
Executed Output:
Current speed after acceleration: 5
Current speed after acceleration: 10
Current speed after acceleration: 15
Current speed after acceleration: 20
Current speed after acceleration: 25
Current speed after braking: 20
Current speed after braking: 15
Current speed after braking: 10
Current speed after braking: 5
Current speed after braking: 0