An odometer (from the Greek hodos = road) is an apparatus that is used to measure the distance covered. This usually happens by counting the number of rotations of a wheel and multiplying this number by the circumference of the wheel. The best known form of an odometer is the speedometer as it is used in cars, for example. In a number of countries (like the US), the distance covered is expressed in miles. In other countries it is measured in kilometres. Many vehicles that are equipped with an electronic odometer, have the possibility to change between kilometres and miles. The accuracy with which the distance is displayed is 1/10 of a mile or a kilometre. When an odometer is replaced, a new odometer can be set up on a specified number of kilometres or miles.
Your assignment consists of writing a class OdoMeter. Your class must support the following methods:
The initializing method __init__ gets the total distance covered and the unit used as an optional parameter. The unit is given as a Boolean value that is True if the distance covered was measured in kilometres and False in the case that the distance is expressed in miles. Deduct the standard values for the parameters of the constructor from the example below.
The methods __str__ and __repr__ print the string representation of the odometer. Use the example below as a base for the format in which the data is printed.
The methods __add__ and __sub__. These methods have an OdoMeter object as a first parameter and a numeric value that must be added to or subtracted from the distance covered as a second parameter. Note that the distance covered can not be negative. When the numeric value that is subtracted is larger than the current distance covered, the new distance in the odometer is zero.
The method switch that changes the unit of the odometer (from miles to kilometres or from kilometres to miles). The distance covered is also adjusted, knowing that 1 mile equals 1609,344 metres.
Note that the odometer asked shows the distance covered to 1/10 of a mile or a kilometre sharp, but that the odometer intern floating point uses precision 6 (that is six digits after the comma).
>>> h = OdoMeter()
>>> print(h)
0.0 km
>>> h
OdoMeter(0.000000, True)
>>> h = h + 10.4
>>> print(h)
10.4 km
>>> g = OdoMeter(10, False)
>>> print(g)
10.0 mi
>>> g
OdoMeter(10.000000, False)
>>> print(g - 50)
0.0 mi
>>> h.switch()
>>> print(h)
6.5 mi
>>> h
OdoMeter(6.462260, False)