*This problem will have two different Python programs. You\'ll submit TWO .py fi
ID: 3737031 • Letter: #
Question
*This problem will have two different Python programs. You'll submit TWO .py files for this one exercise (one with the Car class defined in it, and one with your main program). 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 accepts the car's year model and make as arguments. These values should be assigned to the objects __year_model and __make data attributes. It should also assign () to the __speed data attribute. The class should also have the following methods:
-accelerate accelerate method should add 5 to the speed data attribute each time it's called
-brake The brake method should subtract 5 from the speed data attribute each time it's called.
-get_speed The get_speed attribute method should return to the current speed.
Next, design a program that creates a Car object then calls the accelerate method 5 times. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method 5 times. After each call to the brake method, get current speed of the car and display it.
Explanation / Answer
#car.py
#this is car class program
class Car:
#self constructor for Car class
def __init__(self, year, make, speed):
self.__year_model = year
self.__make = make
self.__speed = 0
#this function assign year to car year model
def set_year_model(self, year):
self.__year_model = year
#this function assign make to car make
def set_make(self, make):
self.__make = make
#this function used to set speed here default speed is 0
def set_speed(self, speed):
self.__speed = 0
#this function get year of car model
def get_year_model(self):
return self.__year_model
#this function gets car make
def get_make(self):
return self.__make
#this function gets current car speed
def get_speed(self):
return self.__speed
#methods
def accelerate(self):
self.__speed +=5
def brake(self):
self.__speed -=5
def get_speed(self):
return self.__speed
-------------------------------------------------------------------------------------------------------------------------------
#car_main.py
#this is python main program for car class
from car import *
def main():
year = input('please Enter the car year: ')
make = input('please Enter the car make: ')
speed = 0
mycar = Car(year, make, speed)
#below code will Accelerate 5 times
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
mycar.accelerate()
print('The current speed is: ', mycar.get_speed())
#below code will Brake 5 times
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
mycar.brake()
print('The current speed after brake is: ', mycar.get_speed())
#the main function
main()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.