In this laboratory, we will use our Stopwatch class to measure the efficiency of the Vector class. Specifically, we want to see how execution speed is affected by the increment parameter. Recall that increment is the amount by which the underlying data array is lengthened when the vector requires more space. If increment is set to zero, then the size of the data array is doubled. We’ll be using the Vector class since the ArrayList class only doubles the array length and does not give you incremental building as an option. Before you start coding make sure you look over the documentation for the Vector class.

  1. Begin by closing all of your open Eclipse projects, so that errors in them will not affect your work today. Use Project/Close.

  2. Create a new Eclipse project named Lab2. Remember to continue to the window in which you can add the BAILEY variable. Next, copy the file /common/cs/cs062/labs/lab02/Stopwatch.java into the src directory in your new Eclipse project and select File/Refresh.

  3. Create a new class VectorTimer. This class will contain only a main method and a few other static methods:
  4. Present the output in a table like the one below; see the tutorial below about formatting. The nanosecond precision of Stopwatch is too fine; you will need to adjust the scale of the timing values as they are printed, which can vary from computer to computer.

    size | linear (1) | linear (10) | double
--------------------------------------------
       0 |          0 |           0 |      0
    5000 |        148 |          14 |      1
   10000 |        580 |          58 |      0
   15000 |       1321 |         132 |      0
   20000 |       2733 |         267 |      1
   25000 |       4863 |         491 |      1
   30000 |       7781 |         781 |      1

We will discuss the significance of your results, and those of your classmates, as they appear. Some things to think about: what is the running time (i.e. Big-O running time) of increment vs. double? Does your data accurately reflect this?

More fun…

Once you’ve got all this working, if you have time we can try out a few additional things:

A note on formatting textual output.

The object System.out has type PrintStream, which in turn has a method format. format is very general and makes it easy to print the lines in the table. The call:

System.out.format("First: %8d, second: %-12s%n", num, str);

creates a string and prints it. The string is formed by:

If num and str are 47 and “XLVII” respectively, then the result will be:
  First:      47, second: XLVII       

The letters after the percent sign, d and s in this example, indicate the kind of data being formatted; they are not variables. The sequence %n is the OS independent newline character. You may have as many % expressions in the format string as you want; they are matched with the arguments that follow. There are many more options for format strings; for more information see the Java documentation for the classes PrintStream and Formatter or the tutorial at:

http://java.sun.com/docs/books/tutorial/java/data/numberformat.html

Extra submission instructions

Follow the submission instructions for lab/assignment 1, except:

Fill out the lab02.json file that you’ll find in the /common/cs/cs062/labs/lab02 folder.