Odometer With a Trip Meter
Implement a class Odometer for the distance display of a car. It shows a total that rolls over like a five-digit counter, and a trip distance that the driver can reset. Write these methods:
Odometer(start)shows a total ofstartand a trip of0.drive(km)addskmto the trip and to the total. The total has only five digits, so after99999it goes on from0(it is kept modulo100000). Returns the new total.read_trip()returns the trip distance. The trip never rolls over.reset_trip()sets the trip back to0and returns what it showed just before.read_total()returns the total.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Odometer(99950)
drive(30) returns 99980
drive(40) returns 20
read_trip() returns 70
reset_trip() returns 70
drive(5) returns 25
read_trip() returns 5
read_total() returns 25
The second drive takes the total past 99999, so it rolls over to 20. The trip keeps counting up to 70 until it is reset.
Constraints
0 <= start <= 999990 <= km <= 1000- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Classes problems
- Panel of Light SwitchesBeginner
- Rectangle on the TableBeginner
- Scoreboard by RoundsBeginner
- Stack With a Size LimitBeginner
- Take a NumberBeginner
- Tally CounterBeginner
- Thermostat Between Two LimitsBeginner
- Whose Turn Is ItBeginner