Final challenge: a class that cannot be broken
After this: Predict, complete and repair a thermostat that keeps its target between 10 and 30.
This challenge has five steps, and each step is scored on its own. They are all about one class: a thermostat whose target temperature must stay between 10 and 30, both ends included. You will predict what a few changes print, complete the check in its adjust method, follow three changes from 20, find the new method that breaks the rule, and say what the constructor does with a bad starting value. Every step shows the code in Python and in Java; the answers are the same in both. You can try as many times as you like, and hints work as they do in the lessons.
Step 1 of 5Predict it
Predict it
A thermostat keeps its target temperature between 10 and 30. It starts at 20. What does this print?
class Thermostat: def __init__(self, target): if target < 10 or target > 30: raise ValueError("target must stay between 10 and 30") self._target = target
@property def target(self): return self._target
def adjust(self, by): wanted = self._target + by if wanted < 10 or wanted > 30: raise ValueError("target must stay between 10 and 30") self._target = wanted
t = Thermostat(20)t.adjust(5)print(t.target)try: t.adjust(10)except ValueError as e: print("refused:", e)print(t.target)t.adjust(-15)print(t.target)class Thermostat { private int target;
Thermostat(int target) { if (target < 10 || target > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } this.target = target; }
int getTarget() { return target; }
void adjust(int by) { int wanted = target + by; if (wanted < 10 || wanted > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } target = wanted; }}
public class Main { public static void main(String[] args) { Thermostat t = new Thermostat(20); t.adjust(5); System.out.println(t.getTarget()); try { t.adjust(10); } catch (IllegalArgumentException e) { System.out.println("refused: " + e.getMessage()); } System.out.println(t.getTarget()); t.adjust(-15); System.out.println(t.getTarget()); }}Complete the guard
Complete the test in adjust that refuses a wanted target outside 10 to 30, so that the program prints the refusal and then 30.
class Thermostat: def __init__(self, target): if target < 10 or target > 30: raise ValueError("target must stay between 10 and 30") self._target = target
@property def target(self): return self._target
def adjust(self, by): wanted = self._target + by ______________ # ← refuse a target outside 10 to 30 raise ValueError("target must stay between 10 and 30") self._target = wanted
t = Thermostat(28)try: t.adjust(5)except ValueError as e: print("refused:", e)t.adjust(2)print(t.target)class Thermostat { private int target;
Thermostat(int target) { if (target < 10 || target > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } this.target = target; }
int getTarget() { return target; }
void adjust(int by) { int wanted = target + by; ______________ // ← refuse a target outside 10 to 30 throw new IllegalArgumentException("target must stay between 10 and 30"); } target = wanted; }}
public class Main { public static void main(String[] args) { Thermostat t = new Thermostat(28); try { t.adjust(5); } catch (IllegalArgumentException e) { System.out.println("refused: " + e.getMessage()); } t.adjust(2); System.out.println(t.getTarget()); }}Three changes
A new thermostat starts at 20. The program calls adjust(8), then adjust(5), then adjust(-18), and reads the target after each call (a refused call is caught, and the program goes on). Which three targets does it see?
Which method breaks the rule
A teammate added two methods to the thermostat. Which method can leave the target outside 10 to 30?
class Thermostat: def __init__(self, target): if target < 10 or target > 30: raise ValueError("target must stay between 10 and 30") self._target = target
@property def target(self): return self._target
def adjust(self, by): wanted = self._target + by if wanted < 10 or wanted > 30: raise ValueError("target must stay between 10 and 30") self._target = wanted
def reset(self): self._target = 20
def boost(self): self._target = self._target + 5class Thermostat { private int target;
Thermostat(int target) { if (target < 10 || target > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } this.target = target; }
int getTarget() { return target; }
void adjust(int by) { int wanted = target + by; if (wanted < 10 || wanted > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } target = wanted; }
void reset() { target = 20; }
void boost() { target = target + 5; }}The first door
What happens when the program runs Thermostat(5)new Thermostat(5)?
class Thermostat: def __init__(self, target): if target < 10 or target > 30: raise ValueError("target must stay between 10 and 30") self._target = target
@property def target(self): return self._target
def adjust(self, by): wanted = self._target + by if wanted < 10 or wanted > 30: raise ValueError("target must stay between 10 and 30") self._target = wantedclass Thermostat { private int target;
Thermostat(int target) { if (target < 10 || target > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } this.target = target; }
int getTarget() { return target; }
void adjust(int by) { int wanted = target + by; if (wanted < 10 || wanted > 30) { throw new IllegalArgumentException("target must stay between 10 and 30"); } target = wanted; }}- Predict what a guarded class prints when some changes are allowed and some are refused, with both ends of a range included.
- Complete the check at the top of a changing method, so that a value outside the rule is refused before anything changes.
- Find the method that breaks a class's rule by storing a value it never tested, and say why the constructor must check its starting value too.