question archive create a base class called Vehicle with name, max_speed, and tank_size attributes
Subject:Computer SciencePrice:2.84 Bought7
create a base class called Vehicle with name, max_speed, and tank_size attributes. Then create a derived class called Truck that inherits from the Vehicle class. The Truck should have an additional attribute called seating_capacity with a default value of 5. The child class should have a method named range to calculate fuel efficiency - the calculation should be: max_speed * seating_capacity * tank_size. Override the method such that it can accept 1, 2, or 3 positional arguments.
1)In-Vehicle, give all parameters a default value of None
2)In Truck, please remove the __init__ method. By not including an __init__ method, it will by default use the parent's
3)Your range method should take in a single parameter (seating_capacity) which is set to a default of 5. Inside this method, assign self.seating capacity, then use if statements to check what class attributes have been assigned
4)If max_speed, tank_size, and seating_capacity are all not None, then you can multiply
5)If max_speed and seating_capacity are not None, you can multiply those instead
6)If the only seating_capacity is not None (which it won't be because you have provided a default value of 5), that will be what you return
class Vehicle:
def __init__(self, name=None, max_speed=None, tank_size=None):
'''instance constructor'''
self.name = name
self.max_speed = max_speed
self.tank_size = tank_size
class Truck(Vehicle):
def range(self, seating_capacity=5):
'''instance method'''
# assign atribute
self.seating_capacity = seating_capacity
# initialize total to default to seating_capacity as it is always not None
total = self.seating_capacity
# check attributes if not None
if self.max_speed is not None:
total *= self.max_speed
if self.tank_size is not None:
total *= self.tank_size
# return total to caller
return total
Step-by-step explanation
Here is the complete source code, kindly see inline comments for explanation. You may also view and run it from here for testing: https://repl.it/@dobizz/SoreNecessaryArchitect
class Vehicle:
def __init__(self, name=None, max_speed=None, tank_size=None):
'''instance constructor'''
self.name = name
self.max_speed = max_speed
self.tank_size = tank_size
class Truck(Vehicle):
def range(self, seating_capacity=5):
'''instance method'''
# assign atribute
self.seating_capacity = seating_capacity
# initialize total to default to seating_capacity as it is always not None
total = self.seating_capacity
# check attributes if not None
if self.max_speed is not None:
total *= self.max_speed
if self.tank_size is not None:
total *= self.tank_size
# return total to caller
return total
def tests():
v = Vehicle('Vehicle1', 100, 60)
assert isinstance(v, Vehicle)
assert type(v) == Vehicle
truck1 = Truck()
truck2 = Truck('Truck 2', 100)
truck3 = Truck('Truck 3', 120, 60)
# check type and inheritance
assert isinstance(truck1, Vehicle)
assert isinstance(truck1, Truck)
assert type(truck1) == Truck
assert isinstance(truck2, Vehicle)
assert isinstance(truck2, Truck)
assert type(truck2) == Truck
assert isinstance(truck3, Vehicle)
assert isinstance(truck3, Truck)
assert type(truck3) == Truck
# check for range implementation
assert truck1.range() == 5
assert truck2.range() == 500
assert truck3.range(3) == 120*60*3
if __name__ == '__main__':
tests()