In: Computer Science
class Car:
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
my_car = Car("Corvette", "Black", “30”)
print my_car.model
print my_car.color
print my_car.MPG
print my_car.condition
Add class “getter” methods to return the values for model, color,
and MPG
Add class “setter” methods to change the existing values for model,
color, and MPG. The value to be used in the methods will be passed
as an input argument.
Add class method that will calculate and return the number of miles driven, where the method is provided the size of the gas tank as an integer input
Below your Car class, create a new child class called
mySportsCar that inherits from Car class. Make sure you call the
Car class constructor with the appropriate inputs in the child
class constructor.
Instantiate an object of the mySportsCar class. Use whatever
appropriate input values that you would like.
In case of any query do comment. Please rate answer as well.
Code:
class Car:
condition = "new"
def __init__(self, model, color, mpg):
self.model = model
self.color = color
self.mpg = mpg
#accessors
def get_model(self):
return self.model
def get_color(self):
return self.color
def get_mpg(self):
return self.mpg
#mutators
def set_model(self,value):
self.model = value
def set_color(self,value):
self.color = value
def set_mpg(self,value):
self.mpg = value
#miles driven method will return miles per gallon multiplied by size of tank
def milesDriven(self,sizeOfTank):
return int(self.mpg)*sizeOfTank
#new class mySportsCar inherited from Car
class mySportsCar(Car):
#constructor calling base class constructor
def __init__(self, model, color, mpg):
Car.__init__(self,model,color,mpg)
#main driver program
my_car = Car("Corvette", "Black", "30")
print (my_car.get_model())
print (my_car.get_color())
print (my_car.get_mpg())
print (my_car.condition)
print("Miles driven by the {} car is : {}".format(my_car.get_model(), my_car.milesDriven(25)))
print("My Sports Car details are: ")
my_sportsCar = mySportsCar("BMW M3", "Red", "20")
print (my_sportsCar.get_model())
print (my_sportsCar.get_color())
print (my_sportsCar.get_mpg())
print (my_sportsCar.condition)
print("Miles driven by the {} car is : {}".format(my_sportsCar.get_model(), my_sportsCar.milesDriven(40)))
===Screen shot of the code for indentation===
Output: