CMSC 341 Fall 2007
Project 1

Assigned Wednesday, Sep 5, 2007
Due 11:59pm, Friday Sep 21, 2007
Updates

Objectives

Background

The Java Collections Framework was introduced in the Java 2 platform. The framework's major objective was to provide an architecture for the representation and manipulation of collections. A collection is an object that represents a group of heterogeneous objects. In Java 5, generics were introduced such that a collection could represent a group of homogeneous objects. One of the advantages to using the collections framework is that it implements many powerful data structures and the algorithms for sorting and searching them so that you don't have to implement them yourself. Because these high-performance implementations share a common interface, you can switch between implementations simply by changing the type of your collection.


Description

In this project you will be creating a benchmark to compare the performance of the different implementations of the interfaces in the Java Collections Framework.The implementations you will compare consist of two Lists - the ArrayList and the LinkedList and two sets - the TreeSet and the HashSet. You will also compare the synchronized versions of these data structures and the vector for a total of 9 different classes which can all be found in the java.util package. You will submit your code (2 file), a text file indicating your results, and a README file.

Before you begin, you should read and understand the Sun's tutorials on the Collection classes and on Generics.

Your program will test each of the forementioned classes for four different operations - adding a given number of elements to the collection, iterating thru these elements, finding a given element in the collection and removing the given element. If the collection is a list, it will also test the speed at which the elements may be randomly accessed. Because we are assuming that this is your first Java project we will be giving you a Timer thread that will calculate the number of microseconds for a given operation. In your program, you must create a static array of objects of type Integer and of size 1000 that is accessible to your all the methods within the benchmark. The array will contain random integer values between the 0 and 999. The name of this array is found in the benchmark code. One of the elements in the array must be the Integer o that serves as a parameter for the findElement and the removeElement.

The code for the benchmark follows:
public static void benchmark(Collection c) {
		   System.out.printf("%-50s",c.getClass().getName());
		   Timer clock = new Timer();
		   clock.start();
		   clock.begin();
		   addElements(c);
		   clock.end();
		   //add
		   System.out.printf("%15d ns",
		                      clock.getElapsedTime());
		   clock.reset();
		   clock.begin();
		   iterateOverElements(c);
		   clock.end();
		   //iter
		   System.out.printf("%15d ns",
		                      clock.getElapsedTime());
		   clock.reset();
		   clock.begin();
		   findElement(c, o);
		   clock.end();
		   //find
		   System.out.printf("%15d ns",
		                      clock.getElapsedTime());
		   //random
		   if (c instanceof List) {
		      List l = (List)c;
		      clock.reset();
		      clock.begin();
		      randomAccess(l);
		      clock.end();
		      System.out.printf("%15d ns",
		                         clock.getElapsedTime());
		   }
		   else
		   	  System.out.printf("%15d ns", 0);
		   //remove
		   clock.reset();
		   clock.begin();
		   removeElement(c, o);
		   clock.end();
		   System.out.printf("%15d ns\n",
		                      clock.getElapsedTime());
}
We will also be providing you with the code for the three of the static methods used above:
public static void addElements(Collection c) {
   for (int i = 0; i < NUM_ITEMS; i++) {
      c.add(objects[i]);
   }
}
public static void iterateOverElements(Collection c) {
   for (int i = 0; i < NUM_ITEMS; i++) {
      Iterator iter = c.iterator();
      while (iter.hasNext()) {
         Object o = iter.next();
      }
   }
}

public static void randomAccess(List c) {
   for (int i = 0; i < NUM_ITEMS; i++) {
      Object o = c.get(random());
   }
}
You are expected to write the random, removeElement, and findElement methods. You should use the static random method in the java.lang.Math class to implement this method. The random method in the randomAccess method should return a random integer between 0 and 999. The benchmark method currently does not incorporate any generic programming.

Dr. Rheingans' public directory for this project is

/afs/umbc.edu/users/r/h/rheingan/pub/341/Proj1 and it contains the Timer.java code mentioned above.

Sample Results

CLASS NAME                                                       ADD           ITERATE              FIND            RANDOM            REMOVE
java.util.ArrayList                                        443911 ns       39319522 ns         178515 ns        1381460 ns         128508 ns
java.util.Collections$SynchronizedRandomAccessList        1413029 ns       35398633 ns         546718 ns         537778 ns          70121 ns
java.util.LinkedList                                      1008788 ns       28832994 ns         101410 ns        3488432 ns          72076 ns
java.util.Collections$SynchronizedList                    2026794 ns       29971687 ns          55594 ns        2248331 ns          50565 ns
java.util.TreeSet                                         5107353 ns       36716957 ns          37155 ns              0 ns          41067 ns
java.util.Collections$SynchronizedSet                     5661893 ns       34539306 ns          13689 ns              0 ns          14248 ns
java.util.HashSet                                         2049981 ns       38050087 ns          27377 ns              0 ns          32685 ns
java.util.Collections$SynchronizedSet                     2936966 ns       35672690 ns          10895 ns              0 ns          18717 ns
java.util.Vector                                           997613 ns       49625861 ns          77663 ns         310375 ns         127391 ns

Project Notes, Hints, and Requirements

  1. For this first project, we will not be using Eclipse, a very popular IDE (Itegrated Development Environment) for Java. We ask that you program using a Java enabled editor like (X)emacs, gedit, or TextPad. We would like for you to compile and run your program from the command line.
  2. You will create a file named Proj1.java which will contain a main method from which you will create an instance of each of the 9 Collection classes which you will be testing. Then you will then pass each instance to the static benchmark method to view the results. The results will be written to a text file in the format as seen here.
  3. To compile a Java program from the command line you use the javac command. Thus, if your program is named Proj1.java, you will compile it using the command javac Proj1.java. This command will work if Proj1.java and Timer.java are in the same directory as where you are typing the command and it creates two files, Proj1.class and Timer.class. You can now run the program using the java Proj1 command. When you compile the classes, you may receive warnings on unchecked and unsafe operations. Ignore them unless you want to do the extra credit.
  4. Once you have created a program that runs using the default package (i.e. no package name declaration), you will need to add the package name proj1 to both the Proj1.java and Timer.java files. Now to compile the class, you will need to use the -d option so that the compiler will create the directory hierarchy that is required of the package. To compile your program using packages, use the javac -d . Proj1.java command. This command will create the package directory hierarchy in the current directory. You can now run the program using the java proj1.Proj1 command.
  5. Threads are light weight programs. Java provides a built-in capability to create thread. For this project, we are providing you with a thread that will do the timing for the project. The thread is named Timer and is at /afs/umbc.edu/users/r/h/rheingan/pub/341/Proj1. The unusual thing about threads is that when you create them and call the start method, they begin to execute the run method in a separate execution space than main such that it appears as though the main method at the start method are running in parallel. Simply use the thread as shown in the example code here.
  6. To create the file containing your output, use unix redirection on the command line to redirect standard output to a file. Thus, once you have the output on the screen as shown in the sample results above, then you can redirect your output to file by using the command java proj1.Proj1 > p1-output.txt.
  7. All assignments and exams in the course are expected to be your INDIVIDUAL work. You may discuss assignments with anyone, but must write your programs independently. For this first assignment only, you can help each other compile and debug one another's programs. Any help you receive must be documented, including discussion, books, papers, and web resources. With each assignment, you will turn in a README text file indicating the sources you used while working on it and the type of help you received from each. If you received no help, say so. Failure to include this README file will result in your program being returned ungraded.

Extra Credit

For extra credit, you will need to make the code implement generics so that it does not issue warnings about unchecked or unsafe operations. You will need to slightly modify the benchmark code in order to do this. Only changes with respect to generics will be permitted on the benchmark method.

Files To Be Submitted

Submit the following files:
  1. Proj1.java,
  2. Timer.java,
  3. p1-output.txt,
  4. README

Be sure that the p1-output.txt file is in plain text format.

Submit the files using the procedure below.

Submission Tools
There are a number of tools available to you to check on your submittal. It is your responsibility to ensure that the submittal is correct and will result in a successful compilation of your project. Do not wait till the last minute to submit your files. Give yourself enough time to check that your submittal is correct.

If you don't submit a project correctly, you will not get credit for it. Why throw away all that hard work you did to write the project? Check your submittals. Make sure they work. Do this before the due date.

Documentation for the submit program is on the web at http://www.gl.umbc.edu/submit/. The class name is cs341 and the project name is Proj1. One of the tools provided by the submit program is submitls. It lists the names of the files you have submitted.

Additionally, there are two programs for use only by CMSC-341 students (not part of the UCS submit program). They are in the directory /afs/umbc.edu/users/r/h/rheingan/pub/341/ and are named submitmake and submitrun. You can use these programs to compile and run your submitted projects.

The syntax is similar to that for submit:

submitmake <class> <project>

Example:  submitmake cs341 Proj1 <parameter list>


Project grading is described in the Project Policy handout.
Cheating in any form will not be tolerated. Please re-read the Project Policy handout for further details on honesty in doing projects for this course.

Remember, the due date is firm. Submittals made after midnight of the due date will not be accepted. Do not submit any files after that time.