Chapter 2: Inheritance

Speed-boosted cars

face Josiah Wang

Here’s another quick exercise! Your software analyst is really into the racing car game, and has now decided to add special cars with their speed boosted when accelerating. 💨💨💨

Class diagram for BoosterCar

Your task

Your task is to implement the BoosterCar class.

BoosterCar is just like any other Car. Under the hood, it has an extra attribute called boost. When BoosterCar accelerates, it multiplies the given acceleration rate with the value of boost to achieve a higher rate. For example, if the boost attribute is 1.3 and accelerate(10) is invoked, then you should multiply 10 by 1.3 and accelerate the BoosterCar with a rate of 13 instead of 10. After taking into account the resistance from wheels (say 1), then the speed of the BoosterCar should increase by 12.

Implement the constructor for BoosterCar and override the accelerate() method. Again, this should be a quick exercise. You should only need to implement a single line for the accelerate() method.

Sample usage

>>> wheels = [Wheel(38, 0.5)] * 4  # 4 wheels with diameter 38 and resistance 0.5
>>> booster_car = BoosterCar("Super Sonic", 2021, True, wheels, boost=1.2)
>>> booster_car.accelerate(20)
>>> print(booster_car.speed)
23.0
>>> booster_car.accelerate(10)
>>> print(booster_car.speed)
34.0
>>> booster_car.decelerate(10)
>>> print(booster_car.speed)
25.0