1. Write a class named Car that has the following data attributes:
a) __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 accepts 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:
b) 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
class Car:
def __init__(self,year,model,make):
self.__year=year
self.__model=model
self.__make =make
self.__speed =0
def Accelerate(self):
self.__speed +=5
def Brake(self):
self.__speed -=5
def get_speed(self):
return self.__speed
def main():
c=Car(2015,"Ford","X5")
print("Accelerate")
c.Accelerate()
print("Accelerate")
c.Accelerate()
print("Accelerate")
c.Accelerate()
print(f"Current speed is {c.get_speed()}")
print("Brake")
c.Brake()
print(f"Current speed is {c.get_speed()}")
main()
Comments
Leave a comment