An odometer is the gauge on your car that measures distance traveled. In the United States, an odometer measures in miles; elsewhere else, it measures kilometers. Many vehicles with electronic odometer interfaces can switch between miles and kilometers. Something to consider: if an odometer gets replaced, a new one must be able to be set to some specified mileage. a) Write an Odometer class which must include the following methods: • _init__, _str_1_repr__, overloaded add and substract method The constructor must take two arguments mileage, and any other specification that uses units (you choose the unit specification) . Both arguments must have a default value (you may choose the default values) O Addition and subtraction both have one odometer operand and one numeric operand, where the numeric operand represents the miles being added/subtracted (not two odometer operands). o Addition should be commutative (but not subtraction). o Output methods should always be rounded to 1/10 mile (kilometer), but the odometer itself should maintain full floating-point accuracy. b) Include sample code that uses your class and demonstrates the use of all methods as well as demonstrating error handling. c) at the end of your code show an illustrated run of your code "W" "

Respuesta :

Answer:

class Odometer():

   def __init__(self, mileage=0, unit='miles'):

       if mileage < 0:  

           raise ValueError('Mileage cannot be in negative')

       self.mileage = mileage

       if unit in ['miles', 'kilometers']:  

           self.units = unit

       else:

           raise ValueError("Miles and kilometer unit are allowed only")

   def __repr__(self):

       return self.__str__()

   def __str__(self):

       return 'Mileage: {:.1f} {}'.format(self.mileage, self.units)

   def add(self, odometer, distance):

       if odometer.units == self.units:

           self.mileage = odometer.mileage + distance

       elif self.units == 'miles':  

           self.mileage = odometer.mileage / 1.6 + distance

       else:

           self.mileage = odometer.mileage * 1.6 + distance

   def subtract(self, odometer, distance):

       if odometer.units == self.units:

           self.mileage = odometer.mileage + distance

       elif self.units == 'miles':

           self.mileage = odometer.mileage / 1.6 - distance

       else:

           self.mileage = odometer.mileage * 1.6 - distance

honda_odometer = Odometer(100.56, 'miles')

print('Reading of Honda odometer:', honda_odometer)

new_odometer = Odometer(100, 'kilometers')

print('Reading of New odometer:', new_odometer)

new_odometer.add(honda_odometer, 10)

print('Reading of New odometer after adding honda odometer:', new_odometer)

new_odometer.subtract(honda_odometer,20)

print('Reading of New odometer after subtracting honda odometer:', new_odometer)

Explanation:

  • Create a class only supports miles and kilometers as units .
  • Create the add method that accepts an odometer, distance and adds it to the current object .
  • Create the subtract method that accepts an odometer and distance and subtract it to the current object .