Ask a question from expert

Ask now

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

2 Pages242 Words185 Views
   

Added on  2019-09-16

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

   Added on 2019-09-16

BookmarkShareRelated Documents
/**A stopwatch accumulates time when it is running. You can repeatedly 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;}}
/**. A stopwatch accumulates time when it is running. Y_1

End of preview

Want to access all the pages? Upload your documents or become a member.

Related Documents
IntListMethodsTest Class in Java
|2
|715
|415

Study on Counters and Timers | Assignment
|5
|660
|31

Memory Sequence Game - Java Program for Console Based Game
|8
|998
|259