UNB/ CS/ David Bremner/ teaching/ old/ cs1083/ java/ StopWatch.java
public class StopWatch{
   public void start()
   {  if (isRunning) return;
      isRunning = true;
      startTime = System.nanoTime();
   }

   /**
      Stops the stopwatch. Time stops accumulating and is
      is added to the elapsed time.
   */
   public void stop()
   {  if (!isRunning) return;
      isRunning = false;
      long endTime = System.nanoTime();
      elapsedTime = elapsedTime + endTime - startTime;
   }

   /**
      @return the total elapsed time
   */
   public long getElapsedTime()
   {  if (isRunning)
      {  long endTime = System.nanoTime();
         elapsedTime = elapsedTime + endTime - startTime;
         startTime = endTime;
      }
      return elapsedTime/1000000;
   }

   /**
      Stops the watch and resets the elapsed time to 0.
   */
   public void reset()
   {  elapsedTime = 0;
      isRunning = false;
   }

   private long elapsedTime;
   private long startTime;
   private boolean isRunning;
}