Last active
November 12, 2023 10:49
-
-
Save APAC-GOLD/c60f959abd87a019605e7aca3b4da694 to your computer and use it in GitHub Desktop.
Vehicle Performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Vehicle Performance & Fuel Costs | |
#How To - Calculate Fuel Consumption | |
#Step1 - customize the behavior of drive method in ElectricCar and IceCar classes | |
#Car | |
class Car: #class = Car. Method & definition(s) are defined below this level. | |
def __init__(self, brand, milage_km): #method = __Self@Car__(parameter1, parameter2, parameter3). | |
self.brand = brand #parameter.variable = parameter. | |
self.milage_km = milage_km #parameter.variable = parameter. | |
def drive(self, distance_km): | |
self.milage_km = self.milage_km + distance_km | |
#Electric_Car | |
class Electric_Car(Car): #Writing "Electric(Car)" allows the "Electric_Car class" to inherit the properties of "Car" from above. | |
def __init__(self, brand, milage_km, range): | |
super().__init__(brand, milage_km) | |
self.range = range #parameter.variable = paramater 'range of the vehicle' | |
def drive(self, distance_km): | |
self.range -= distance_km #parameter(Self@Electric_Car).variable(range) -= parameter range subtracted by distance parameter | |
#IceCar | |
class IceCar(Car): | |
def __init__(self, brand, milage_km, range, fuel_consumption, fuel_level): | |
super().__init__(brand, milage_km) | |
self.range = range | |
self.fuel_consumption = fuel_consumption | |
self.fuel_level = fuel_level | |
def drive(self, distance_km): | |
self.range -= distance_km | |
self.fuel_level -= distance_km * (self.fuel_consumption/100) | |
#====== ==== === == =Vehicle Information= == === ==== =======# | |
# Key: | |
# (i) "Make"/brand of vehicle, (ii) Milage/Odometer Reading, (iii) Range/Distance kilometers(km), (iv) Fuel Consumption (v) Fuel Level | |
#Vehicles | |
my_ev = Electric_Car("BYD", 10000, 100) | |
my_ev.drive(distance_km = 10) | |
print(my_ev.__dict__) | |
my_ev = Electric_Car("Nissan", 16000, 50) | |
my_ev.drive(distance_km = 10) | |
print(my_ev.__dict__) | |
print(my_ev) | |
print("") | |
my_icecar = IceCar("Snowmobile", 4500, 50, 50, 25) | |
my_icecar.drive(distance_km = 50) | |
print(my_icecar.__dict__) | |
print(my_icecar) | |
print("") | |
#Step2 - Test drive behavior vehicle class | |
my_ice_car = IceCar("Snowmobile", 4500, 50, 50, 25) | |
my_ice_car.drive(distance_km = 50) | |
print(my_ice_car.__dict__) | |
my_ice_car = IceCar("Snowmobile", 1000, 100, 70, 90) | |
my_ice_car.drive(distance_km = 50) | |
print(my_ice_car.__dict__) | |
my_ice_car = IceCar("Snowmobile", 10, 100, 150, 10) | |
my_ice_car.drive(distance_km = 50) | |
print(my_ice_car.__dict__) | |
#====== ==== === == =Vehicle Information= == === ==== =======# | |
# Key: | |
# (i) "Make"/brand of vehicle, (ii) Milage/Odometer Reading, (iii) Range/Distance kilometers(km), (iv) Fuel Consumption (v) Fuel Level | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Given this example, customize the behavior of drive:
Inside ElectricCar class
redefine the
drive(self, distance)
function againit will have same behavior regarding
milage_km
but add in at the end some code so that the
self.range
gets subtracted by the drivedistance
Test the drive behavior of ElectricCar class
initialize an object from ElectricCar class
my_ev
call
my_ev.drive(some_distance)
a few timesnotice via
print(my_ev.__dict__)
that therange
has been reducedInside IceCar class
redefine the
drive(self, distance)
function againit will have same behavior regarding
milage_km
but add in at the end some code so that the
self.fuel_level
gets subtracted by the drivedistance*fuel_consumption/100
(how much fuel we will consume)Test the drive behavior of IceCar class
initialize an object from ElectricCar class
my_ice_car
call
my_ice_car.drive(some_distance)
a few timesnotice via
print(my_ice_car.__dict__)
that thefuel_level
has been reducedAWS re/Start Course Homework, 11 November 2023