Create the parent class Vehicle. Give it attributes make, model, and mileage. It should have an accessor method displayInfo(). It should also have a modifier method addMileage() which adds an integer to the mileage (ex: addMileage(50) will add 50 to the mileage attribute.)
Create the subclass Bus. Give it the extra attribute fare.
Add the method calculateFare() with the parameter trips. Then, it should print the attribute fare multiplied by trips. (ex: If self.fare = 2.50 and we call calculateFare(4), it should return 10.0).
Override the method displayInfo() to also display the bus fare.
Instantiate/make an object of the class Bus. Run addMileage(100), then calculateFare(5), and lastly, displayInfo().
Resourses:
Class slides: Intro to Python 2022 (17) - Google Slides
Class code: Class-17
Contact Info:
shravyassathish@gmail.com (mentor)
kingericwu@gmail.com (TA)
felixguo41@gmail.com (TA)
class Car(vehicle):
def __init__(self, make, model, mileage, fare):
self.make = make
self.model = model
self.mileage = mileage
self.fare = fare
#3
def calculateFare(self, trips):
print(f"{self.fare * trips}")
#4
def displayInfo(self):
print(f"The make is {self.make}, the model is {self.model}, the mileage is {self.mileage}, the price is ${self.fare}.")
#5
car = Car("Car", "5682", 4423J, 6.45)
car.addMileage(100)
car.calculateFare(5)
car.displayInfo()
class vehicle:
def __init__(self, make, model):
self.make = make
self.model = model
self.mileage = 0
def displayInfo(self):
print(f" make {self.make} model {self.model} mileage {self.mileage}")
def addMileage(self, miles):
self.mileage += miles
class bus(vehicle):
def __init__(self, make, model, fare):
self.fare = fare
vehicle.__init__(self, make, model)
def calculateFare(self, trips):
print("fare = ", self.fare * trips)
def displayInfo(self):
print(f" make {self.make} model {self.model} mileage {self.mileage} fare {self.fare}")
abus = bus("toyota", "model 5", 2.5)
abus.addMileage(5)
abus.calculateFare(4)
abus.displayInfo()
abus.addMileage(8)
abus.displayInfo()
#1
class vehicle():
def __init__(self, make, model, mileage):
self.make = make
self.model = model
self.mileage = mileage
def displayInfo(self):
print(f"The make is {self.make}, the model is {self.model}, and the mileage is {self.mileage}.")
def addMileage(self, adding):
self.mileage += adding
#2
class Bus(vehicle):
def __init__(self, make, model, mileage, fare):
self.make = make
self.model = model
self.mileage = mileage
self.fare = fare
#3
def calculateFare(self, trips):
print(f"{self.fare * trips}")
#4
def displayInfo(self):
print(f"The make is {self.make}, the model is {self.model}, the mileage is {self.mileage}, the price is ${self.fare}.")
#5
bus = bus("Bus", "2341B", 12312, 3.45)
bus.addMileage(100)
bus.calculateFare(5)
bus.displayInfo()
class Vehicule():
def __init__(self, mk, mod, mi):
self.make = mk
self.model = mod
self.mileage = mi
def displayInfo(self):
print(f"{self.make} {self.model} with mileage {self.mileage} found!")
def addMileage(self, addedMileage):
self.mileage += addedMileage
class Bus(Vehicule):
def __init__(self, mk, mod, mi, f):
self.make = mk
self.model = mod
self.mileage = mi
self.fare = f
def calculateFare(self, trips):
return self.fare * trips
def displayInfo(self):
print(f"{self.make} {self.model} with mileage {self.mileage} found, with fare ${self.fare}")
untitledBus = Bus("OBI", "07.501 BRT", 0, 2.35)
untitledBus.addMileage(100)
untitledBus.calculateFare(5)
untitledBus.displayInfo()
class Vehicle():
def __init__(self, make, model, mileage):
self.make = make
self.model = model
self.mileage = mileage
def displayInfo(self):
print(f"Make: {self.make}\nModel: {self.model}\nMileage: {self.mileage}")
def addMileage(self,added):
self.mileage +=added
class Bus(Vehicle):
def __init__(self,make,model,mileage,fare):
self.make = make
self.model = model
self.mileage = mileage
self.fare = fare
def calculateFare(self,trips):
self.fare = trips*self.fare
def displayInfo(self):
print(f"Make: {self.make}\nModel: {self.model}\nMileage: {self.mileage}\nFare: {self.fare}")
bus = Bus("a","x",0,2.50)
bus.addMileage(100)
bus.calculateFare(5)
bus.displayInfo()
#I have the problem where I still have to initialize make, model, and self even though it is already in the parent class.