Encapsulation means controlling how internal state is accessed and changed. Without it, any code can put the system into an invalid state.
Implement a Thermostat class:
setTemperature(temp) ā sets the temperature, but only if it's between -10 and 40 (inclusive). If out of range, print Out of range and leave unchanged.getTemperature() ā returns the current temperatureYour program receives comma-separated operations:
start:<n> ā create thermostat at n°Cset:<n> ā call setTemperatureget ā print the temperatureInput: start:20,set:25,get,set:99,get
Output: 25
Out of range
25
Out of range (exact) on rejected sets.