PlayTime.java


Below is the syntax highlighted version of PlayTime.java.


/*****************************************************************************
 * Example program to declare and initialize two StopWatch objects.
 * Then swap them by merely swapping the reference names.
 *
 * Compilation: javac PlayTime.java
 * Execution: java PlayTime
 * Dependencies: StopWatch.java, StdOut.java
 *
 ****************************************************************************/

public class PlayTime {
    public static void main(String[] args) {
        Stopwatch watchOne = new Stopwatch();
        // waste time so it is large enough to be measurable
        for (int i = 0; i < 1e8; i++);
        
        Stopwatch watchTwo = new Stopwatch(); 
        // waste time so it is large enough to be measurable
        for (int i = 0; i < 1e8; i++);

        // right now, watchOne is older
        StdOut.println("watchOne " + watchOne.elapsedTime());
        StdOut.println("watchTwo " + watchTwo.elapsedTime());
        
        Stopwatch watchTmp = watchOne;
        watchOne = watchTwo;
        watchTwo = watchTmp; 

        // swapped! now watchTwo is the older one
        // e.g. watchTwo.elapsedTime() returns a value
        // (slightly) larger than watchOne.elapsedTime()
        StdOut.println("watchOne " + watchOne.elapsedTime());
        StdOut.println("watchTwo " + watchTwo.elapsedTime());
        
    }
}