My current for you to answer question:Write the Python code that would set the m
ID: 3700574 • Letter: M
Question
My current for you to answer question:Write the Python code that would set the mileage of your variable myCar to 1908. (Hint: use the method you created called setMileage.)
I have answered these questions already: Create your own class definition called Vehicle. • Every Vehicle has a Vehicle Identification Number (VIN) which is a string of letters and numbers. • Every Vehicle has mileage on it, which is an integer. The class constructor should require a VIN and store that value internally. The constructor should set the mileage to zero. Add a method to the class called setMileage that takes an integer and saves it to the Vehicle’s mileage.
And this question: Write the Python code that would create an instance of your Vehicle class (as if you were using the Python interpreter.) Call your Vehicle myCar. Its VIN should be “CSIT104”.
Here is my current code:
class Vehicle:
# class constructor which sets VIN and sets mileage to 0
def __init__(self, VIN):
self.VIN = VIN
self.mileage = 0
# method that sets mileage of vehicle
def setMileage(self, mileage):
self.mileage = mileage
vehicle = Vehicle("UP98TY")
vehicle.setMileage(23)
print ("Vehicle Identification Number: ", vehicle.VIN)
print ("Vehicle mileage: ", vehicle.mileage)
class Vehicle:
def __init__(self, VIN):
self.VIN = VIN
self.mileage = 0
def setMileage(self, mileage):
self.mileage = mileage
vehicle = Vehicle("UP98TY")
vehicle.setMileage(23)
print("Vehicle Identification Number: ", vehicle.VIN)
print("Vehicle mileage:", vehicle.mileage)
myCar = Vehicle("CSIT104")
myCar.setMileage(1908)
print("Vehicle Identification Number: ", myCar.VIN)
print("Vehicle mileage:", myCar.mileage)
Explanation / Answer
class Vehicle: # class constructor which sets VIN and sets mileage to 0 def __init__(self, VIN): self.VIN = VIN self.mileage = 0 # method that sets mileage of vehicle def setMileage(self, mileage): self.mileage = mileage if __name__ == '__main__': vehicle = Vehicle("UP98TY") vehicle.setMileage(23) print("Vehicle Identification Number: ", vehicle.VIN) print("Vehicle mileage: ", vehicle.mileage) myCar = Vehicle("CSIT104") myCar.setMileage(1908) print("Vehicle Identification Number: ", myCar.VIN) print("Vehicle mileage:", myCar.mileage)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.