need help. Can someone tell me what i have missed from the below problem?
Problem:
Create a class called “Vehicle” and methods that allow you to set the “Make”, “Model”, “Year,”, and “Weight”.
The class should also contain a “NeedsMaintenance” boolean that defaults to False, and and “TripsSinceMaintenance” Integer that defaults to 0.
Next an inheritance classes from Vehicle called “Cars”.
The Cars class should contain a method called “Drive” that sets the state of a boolean isDriving to True. It should have another method called “Stop” that sets the value of isDriving to false.
Switching isDriving from true to false should increment the “TripsSinceMaintenance” counter. And when TripsSinceMaintenance exceeds 100, then the NeedsMaintenance boolean should be set to true.
Add a “Repair” method to either class that resets the TripsSinceMaintenance to zero, and NeedsMaintenance to false.
Create 3 different cars, using your Cars class, and drive them all a different number of times. Then print out their values for Make, Model, Year, Weight, NeedsMaintenance, and TripsSinceMaintenance
Code:
import random
Define vehicle class
class Vehicle:
def init(self, make, model, year, weight):
self.make = make
self.model = model
self.year = year
self.weight = weight
self.needsMaintenance = False
self.tripsSinceMaintenance = 0
getters
def getMake(self):
return self.make
def getModel(self):
return self.model
def getYear(self):
return self.year
def getWeight(self):
return self.weight
def repair(self):
self.tripsSinceMaintenance = 0
self.needsMaintenance = False
setters
def setMake(self, make):
self.make = make
def setModel(self, mode):
self.mode = mode
def setYear(self, year):
self.year = year
def setWeight(self, weight):
self.weight = weight
Inheritance class
class Cars(Vehicle):
def init(self, make, model, year, weight, isDriving=False):
Vehicle.init(self, make, model, year, weight)
self.isDriving = isDriving
def drive(self):
self.isDriving = True
def stop(self):
if self.isDriving:
self.tripsSinceMaintenance += 1
if self.tripsSinceMaintenance > 100:
self.needsMaintenance = True
self.isDriving = False
drive and stop car random no of times
def driving_car(car):
drive_times = random.randint(1, 101)
for i in range(drive_times):
car.drive()
car.stop()
creating car instances
FirstCar = Cars(“Toyota”, “Altis”, 2009, 1055)
SecondCar = Cars(“Mitsubishi”, “Mirage”, 2019, 1040)
ThirdCar = Cars(“Honda”, “City”, 2018, 1022)
car attributes
def print_car_specs(car):
print('Make: ', car.make)
print('Model: ', car.model)
print('Year: ', car.year)
print('Weight: ', car.weight)
print('Needs Maintenance: ', car.needsMaintenance)
print('Trips Since Maintenance: ', car.tripsSinceMaintenance)
print()
randomly driving cars
driving_car(FirstCar)
driving_car(SecondCar)
driving_car(ThirdCar)
printing cars
print(FirstCar.make, “Vehicle”)
print("-----------------")
print_car_specs(FirstCar)
print(SecondCar.make, “Vehicle”)
print("-----------------")
print_car_specs(SecondCar)
print(ThirdCar.make, “Vehicle”)
print("-----------------")
print_car_specs(ThirdCar)