In: Computer Science
Create a car class with three attributes: year, mpg, speed and list of owners. The class also should have 2 methods called accelerate and brake. Whenever the car accelerates, its speed is increased by 30 and whenever the car brakes, its speed is reduced by 60.
Use a python programming code
Program Code Screenshot:
Sample Output:
The screenshots are attached below for reference.
Please follow them for proper indentation and output.
Program code to copy:
class car:
def __init__(self,y,m,s):#constructor of class
self.year=y
self.mpg=m
self.speed=s
self.owner=[]
def accelerate(self):#increases speed by 30
self.speed+=30
def brake(self):#decreases speed by 60
self.speed-=60
def __str__(self):
return
"car_year_"+str(self.year)+"_mpg_"+str(self.mpg)+"_speed_"+str(self.speed)
car1=car(2016,20,80)#create car objects
car2=car(2017,30,80)
car1.owner.append("John Doe")
car1.owner.append("Jack Jones")
car2.owner.append("John Flack")#add owners to cars
car2.owner.append("Jeff Heath")
print("car1 owners are :",car1.owner)
print("car2 owners are :",car2.owner)
if car1.owner==car2.owner:#check if the owners are same
print("The owners are same")
else:
print("The owners are different")