Answer to Question #194527 in Python for desmond

Question #194527

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





1
Expert's answer
2021-05-19T12:41:32-0400
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()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS