/* * The class Timer and its constructor may be * used to produces instances of objects of * type Timer. An object of type Timer has * methods start() and stop() that start * and stop a timer, a method called reset * that resets a timer, and a method called * elaspedTimeInMs that returns an amount of * elapsed time. If a timer has been started * but not stopped, then elapsedTimeInMs * returns the amount of time (in milliseconds) * that has elapsed since the timer was started. * If a timer has been started and stopped, * then elapsedTimeInMs returns the amount of * time that elapsed between the time the * timer was started and stopped. */ class Timer { long timeStarted; long timeStopped; Timer() { this.reset(); } void reset() { this.timeStarted = 0; this.timeStopped = 0; } void start() { this.timeStarted = System.currentTimeMillis(); } void stop() { this.timeStopped = System.currentTimeMillis(); } long elapsedTimeInMs() { if (this.timeStarted != 0) { if (this.timeStopped != 0) { return this.timeStopped - this.timeStarted; } else { return System.currentTimeMillis() - this.timeStarted; } } else { return -1; } } }