WARNING: This one might go down in flames or be subject to recall!
class Timer {
long timeStarted;
long timeStopped;
Timer() {
this.reset();
}
void reset() {
this.timeStarted = -1;
this.timeStopped = -1;
}
void start() {
this.timeStarted = System.currentTimeMillis();
}
void stop() {
this.timeStopped = System.currentTimeMillis();
}
boolean isStarted() {
return this.timeStarted != -1;
}
boolean isStopped() {
return this.timeStopped != -1;
}
boolean isRunning() {
return this.isStarted() && !this.isStopped();
}
long elapsedTimeInMs() {
if (this.timeStarted != -1) {
if (this.timeStopped != -1) {
return this.timeStopped - this.timeStarted;
}
else {
return System.currentTimeMillis() - this.timeStarted;
}
}
else {
return -1;
}
}
}
/** FROM: https://en.wikipedia.org/wiki/Stopwatch
*
* A stopwatch is a handheld timepiece designed to measure
* the amount of time elapsed from a particular time when it
* is activated to the time when the piece is deactivated.
*
* The timing functions are traditionally controlled by two
* buttons on the case. Pressing the top button starts the
* timer running, and pressing the button a second time stops
* it, leaving the elapsed time displayed. A press of the
* second button then resets the stopwatch to zero. The second
* button is also used to record split times or lap times.
* When the split time button is pressed while the watch is
* running, the display freezes, allowing the elapsed time to
* that point to be read, but the watch mechanism continues
* running to record total elapsed time. Pressing the split
* button a second time allows the watch to resume display of
* total time.
*/
class Stopwatch {
Timer timer;
Stopwatch() {
this.timer = new Timer();
}
/** topButtonPress:
* Starts timer if timer is not started.
* Stops timer if timer is running.
* Returns:
* 0 if button press causes timer to start.
* Elapsed time in ms otherwise.
*/
long topButtonPress() {
// MISSING CODE!
return -666;
}
/** sideButtonPress:
* Resets timer if timer is not running.
* Returns:
* -1 if button press causes timer to reset.
* Elapsed time in ms otherwise.
*/
long sideButtonPress() {
// MISSING CODE!
return -666;
}
}
class TraditionalStopwatch {
public static void main(String[] args) {
Stopwatch s1 = new Stopwatch();
s1.topButtonPress();
// Long.MAX_VALUE is 9,223,372,036,854,775,807
for (long i=0; i < 1000000000; i++);
long elapsedTime = s1.topButtonPress();
System.out.println(elapsedTime);
}
}
function Timer() {
this.reset();
}
Timer.prototype = {
reset: function() {
this.timeStarted = null;
this.timeStopped = null;
},
start: function() {
this.timeStarted = new Date();
},
stop: function() {
this.timeStopped = new Date();
},
isStarted: function() {
return this.timeStarted != null;
},
isStopped: function() {
return this.timeStopped != null;
},
isRunning: function() {
return this.isStarted() && !this.isStopped();
},
elapsedTimeInMs: function() {
if (this.timeStarted != null) {
if (this.timeStopped != null) {
return this.timeStopped - this.timeStarted;
}
else {
return new Date() - this.timeStarted;
}
}
else {
return undefined;
}
}
};
/** FROM: https://en.wikipedia.org/wiki/Stopwatch
*
* A stopwatch is a handheld timepiece designed to measure
* the amount of time elapsed from a particular time when it
* is activated to the time when the piece is deactivated.
*
* The timing functions are traditionally controlled by two
* buttons on the case. Pressing the top button starts the
* timer running, and pressing the button a second time stops
* it, leaving the elapsed time displayed. A press of the
* second button then resets the stopwatch to zero. The second
* button is also used to record split times or lap times.
* When the split time button is pressed while the watch is
* running, the display freezes, allowing the elapsed time to
* that point to be read, but the watch mechanism continues
* running to record total elapsed time. Pressing the split
* button a second time allows the watch to resume display of
* total time.
*/
function Stopwatch() {
this.timer = new Timer();
}
Stopwatch.prototype = {
/** topButtonPress:
* Starts timer if timer is not started.
* Stops timer if timer is running.
* Returns:
* 0 if button press causes timer to start.
* Elapsed time in ms otherwise.
*/
topButtonPress: function() {
// MISSING CODE!
},
/** sideButtonPress:
* Resets timer if timer is not running.
* Returns:
* -1 if button press causes timer to reset.
* Elapsed time in ms otherwise.
*/
sideButtonPress: function() {
// MISSING CODE!
},
}
var s1 = new Stopwatch();
s1.topButtonPress();
// Number.MAX_SAFE_INTEGER in JavaScript is 2^53 - 1
for (var i=0; i <= 1000000000; i++);
var elapsedTime = s1.topButtonPress();
alert(elapsedTime);