← Unit 3 · all exercises
Saved in this browser only

A stopwatch

Write a Stopwatch that starts stopped at 0 seconds. start makes it running, stop makes it stopped, and tick adds a second only while it is running. getSeconds returns the total.

class Stopwatch { Stopwatch(); void start(); void stop(); void tick(); int getSeconds(); }

Stopwatch.java
Loading editor…
Results

Examples

  • Stopwatch w = new Stopwatch(); w.start(); w.tick(); w.tick(); w.getSeconds() → 2
  • Stopwatch w = new Stopwatch(); w.tick(); w.getSeconds() → 0not started

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