← Unit 3 · all exercises
Saved in this browser only

A combination locker

Write a Locker that starts locked with a code given to the constructor. unlock takes a guess and only opens when it matches. lock closes it again. isOpen reports the state.

class Locker { Locker(int code); void unlock(int guess); void lock(); boolean isOpen(); }

Locker.java
Loading editor…
Results

Examples

  • Locker l = new Locker(1234); l.unlock(1234); l.isOpen() → true
  • Locker l = new Locker(1234); l.unlock(1111); l.isOpen() → false

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