Thermostat Between Two Limits
Implement a class Thermostat whose target temperature always stays between low and high, both included. Write these methods:
Thermostat(low, high)sets the limits. The target starts atlow.turn_up(d)raises the target byd, but not abovehigh. Returns the new target.turn_down(d)lowers the target byd, but not belowlow. Returns the new target.set_target(t)sets the target totand returnstrueiflow <= t <= high. Otherwise nothing changes and it returnsfalse.read_target()returns the target.
The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
Thermostat(18, 24)
turn_up(4) returns 22
turn_up(5) returns 24
set_target(30) returns false
set_target(20) returns true
turn_down(9) returns 18
read_target() returns 18
Turning up by 5 from 22 would pass 24, so the target stops at 24. 30 is outside the limits and is refused, 20 is accepted, and turning down by 9 stops at 18.
Constraints
10 <= low <= high <= 300 <= d <= 150 <= t <= 40- 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.