Use OOD to build a Python car application Define a Car class Properties: make, m
ID: 3569961 • Letter: U
Question
Use OOD to build a Python car application
Define a Car class
Properties:
make, model, color, speed
Methods:
run(speed)
stop()
Define GasEngineCar, subclass of Car
Properties
gasTankIndicator (0 to 100%)
Methods
fillupGasTank()
run(speed)
-run will not work if gasTankIndicator is 0%
Define ElectricCar, subclass of Car
Properties:
batteryLevel (0 to 100%)
chargingTime (hours)
maxDistanceOnFullCharge (miles)
Methods
pluginAndCharge()
run(speed)
-speed can only gets up 150(MPH)
otherwise, print error
-run will not work if batteryLevel is less
than 5%
Ideas for testing
car1 = GasEngineCar("Toyota", "Camry", "red")
car1.run(10);// No gas yet - error
car1.fillupGasTank()
car1.run(80);
car1.stop();
car2 = ElectricCar("Nissan", "Leaf", "green")
car2.run(100)// No charge yet - Error
car2.pluginAndCharge()
car2.run(80);
car2.run(200);// Too fast - Error
car2.stop()
Explanation / Answer
Program Code:
class Car:
def __init__(self, make, model, color):
self.make=make
self.model=model
self.color=color
self.speed=0
def run(self,speed):
print ("Car: ",self.make)
print (" Model: ",self.model)
print (" Color: ", self.color)
print (" Runs at: ",speed, "kmph speed")
def stop(self):
self.speed=0
print ("Car is stopped.")
# ElectricCar class is a subclass of Car class
class ElectricCar(Car):
def __init__(self, make, model, color):
self.gec=Car(make, model, color)
self.chargingTime=0
self.batteryLevel=5
def pluginAndCharge(self):
if self.chargingTime >=0 and self.chargingTime <=1:
self.batteryLevel=5
elif self.chargingTime>1 and self.chargingTime <= 3:
self.batteryLevel=25
elif self.chargingTime >3 and self.chargingTime <=6:
self.batteryLevel=50
elif self.chargingTime >6 and self.chargingTime <=9:
self.batteryLevel=75
elif self.chargingTime>9 and self.chargingTime <=12:
self.batteryLevel=100
else:
print ("Battery is overcharged or nocharge")
print ("Batter is charged to "+str(self.batteryLevel)," %")
def chargeTime(self, time):
self.chargingTime=int(time)
def run(self, speed):
if speed > 150:
print ("Car is going in over speed. ")
elif self.batteryLevel <= 5:
print ("Car is running out of battery. ")
else:
self.gec.run(speed)
def stop(self):
self.gec.stop()
# GasEngineCar class is a subclass of Car class
class GasEngineCar(Car):
def __init__(self, make, model, color):
self.gec=Car(make, model, color)
self.gasTankIndicator=0
def fillupGasTank(self, gastank):
self.gasTankIndicator=gastank
print ("Tank is filled with ",self.gasTankIndicator ," %gas")
def run(self, speed):
if self.gasTankIndicator > 0:
self.gec.run(speed)
else:
print ("Sorry! There is no gas in the Car. ")
def stop(self):
self.gec.stop()
def main():
car=GasEngineCar("Toyota", "Camry", "red")
car.run(60)
car.fillupGasTank(60)
car.run(70)
car.stop()
car=ElectricCar("Nissan", "Leaf", "green")
car.run(50)
car.chargeTime(6)
car.pluginAndCharge()
car.run(80)
car.stop()
if __name__=='__main__':
main()
Sample output:
Sorry! There is no gas in the Car.
Tank is filled with 60 %gas
Car: Toyota
Model: Camry
Color: red
Runs at: 70 kmph speed
Car is stopped.
Car is running out of battery.
Batter is charged to 50 %
Car: Nissan
Model: Leaf
Color: green
Runs at: 80 kmph speed
Car is stopped.
sh-4.2#
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.