Using Python write a class named Car that has the following

Using Python write a class named Car that has the following data attributes:

• _ _yearModel (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 __yearModel and _ _make data attributes. It should also assign 0 to the _ _speed data attribute.

The class includes Accessor and Mutator methods for the attributes yearModel, make, and speed.

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.

• applyBrake The brake method should subtract 5 from the speed data attribute each time it is called.

• _ _str_ _ Returns the string representation of the Car object.

Next, design a driver program that creates three Car objects with different yearModels and makes. Display these cars’ Model year, Make, and Year by calling _ _str_ _ method

Call the accelerate method five times for one the car objects. After each call to the accelerate method, get the current speed of the car and display it. Then call the brake method five times. After each call to the brake method, get the current speed of the car and display it.

Solution

#create class called car

class Car:
def __init__(self,year,make,speed):
self.__year_model=year
self.__make=make
self.__speed=0

#set the arguments for speed,year, and make
def set_year_model(self,year):
self.__year_model=year

def set_make(self,make):
self.__make=make

def set_speed(self,speed):
self.__speed=0

#the returns for speed, year, and make
def get_year_model(self):
return self.__year_model

def get_make(self):
return self.__make

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

#actuall program
#create car object
def main():
my_car=Car()

year=print(input(\'car year: \'))
make=print(input(\'car make: \'))
speed= print(\'Current speed is 0\')

#accelerate 5 times

my_car.accelerate()
print(\'My current speed:\',my_car.get_speed())
my_car.accelerate()
print(\'My current speed:\',my_car.get_speed())
my_car.accelerate()
print(\'My current speed:\',my_car.get_speed())
my_car.accelerate()
print(\'My current speed:\',my_car.get_speed())
my_car.accelerate()
print(\'My current speed:\',my_car.get_speed())

#brake five times
my_car.brake()
print(\'My current speed after brake:\',my_car.get_speed())
my_car.brake()
print(\'My current speed after brake:\',my_car.get_speed())
my_car.brake()
print(\'My current speed after brake:\',my_car.get_speed())
my_car.brake()
print(\'My current speed after brake:\',my_car.get_speed())
my_car.brake()
print(\'My current speed after brake:\',my_car.get_speed())

main()

Using Python write a class named Car that has the following data attributes: • _ _yearModel (for the car’s year model) • _ _make (for the make of the car) _ _sp
Using Python write a class named Car that has the following data attributes: • _ _yearModel (for the car’s year model) • _ _make (for the make of the car) _ _sp

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site