previous | start | next

Example Class Type II

Stopwatch is a class that might not represent an concrete object in a problem application, but is used to measure timing and performance of the application.

public class StopWatch {

        private final long start;

        /**
         * Constructor to initialize the data member of a new stopwatch instance.
         */
        public StopWatch() {
                reset();
        }

        public void reset() {
                start = System.currentTimeMillis();
        }

        /**
         * Return elapsed time (in seconds) since this instance was created.
         */
        public double elapsedTime() {
                final long now = System.currentTimeMillis();
                return (now - start) / 1000.0;
        }

}


previous | start | next