/**. A stopwatch accumulates time when it is running. Y

Added on - 16 Sep 2019

  • 2

    Pages

  • 242

    Words

  • 86

    Views

  • 0

    Downloads

Trusted by +2 million users,
assist thousands of students everyday
Showing pages 1 to 1 of 2 pages
/**A stopwatch accumulates time when it is running. You canrepeatedly start and stop the stopwatch. You can use astopwatch to measure the running time of a program.*/public class StopWatch{private long elapsedTime;private long startTime;private boolean isRunning;/**Constructs a stopwatch that is in the stopped stateand has no time accumulated.*/public StopWatch(){reset();}/**Starts the stopwatch. Time starts accumulating now.*/public void start(){if (isRunning) { return; }isRunning = true;startTime = System.currentTimeMillis();}/**Stops the stopwatch. Time stops accumulating and isis added to the elapsed time.*/public void stop(){if (!isRunning) { return; }isRunning = false;long endTime = System.currentTimeMillis();elapsedTime = elapsedTime + endTime - startTime;}/**Returns the total elapsed time.@return the total elapsed time*/public long getElapsedTime(){if (isRunning){long endTime = System.currentTimeMillis();return elapsedTime + endTime - startTime;}else{return elapsedTime;}}
desklib-logo
You’re reading a preview
Preview Documents

To View Complete Document

Become a Desklib Library Member.
Subscribe to our plans

Download This Document