StopWatch class

The StopWatch class (attached below) is designed to allow one to time things. Here is a summary of the methods:

//constructor
public StopWatch()

//starts the timer
public void start()

//adds the most recent time segment to the total elapsed time and stops the timer
public void stop()

//returns the time elapsed
public double getTime()

// resets and stops the timer
public void reset()

Here is a sample driver:

public class TimeThis
{
	public static void main(String[] args)
	{
		System.out.println("Look how fast I can count to 10,000");
		StopWatch myWatch = new StopWatch();
		myWatch.start();
		for(int i=1; i<=10000; i++)
		{
			System.out.print(i + " ");
		}
		myWatch.stop();

		System.out.println();
		System.out.println("Look!  I counted to 10,000 in just " + 
                            myWatch.getTime() + " seconds.");
	}
}
© Daubenmier/Ehrenfried 2007
ċ
StopWatch.java
(1k)
Mark Daubenmier,
Sep 8, 2011, 8:45 PM
Comments