← Unit 3 · all exercises
Saved in this browser only

A thermostat

Write a Thermostat holding a target temperature, starting at 20. setTarget accepts values from 10 to 30 inclusive and ignores anything else. warmer adds one degree, still respecting the limit. getTarget returns it.

class Thermostat { Thermostat(); void setTarget(int t); void warmer(); int getTarget(); }

Thermostat.java
Loading editor…
Results

Examples

  • Thermostat t = new Thermostat(); t.setTarget(25); t.getTarget() → 25
  • Thermostat t = new Thermostat(); t.setTarget(50); t.getTarget() → 20out of range

There are also 4 hidden tests you can't see. Solve the problem, not the examples.