[an error occurred while processing this directive]

Programming Assignment 1 - Step 3


For the third step of this assignment, you will need to write three methods in the VectorSorter class:

You will also need to add a few lines of code to the main() method of the Test1 class. The easiest method to write is the first, so you should start there.

The sort() Method

Here is the entire definition for this method:
    public void sort() {
	sort(0, vec.size() - 1);
    }  // public void sort()
As you can see, it looks a lot like a C function definition. The one statement in the function just calls the sort(int, int) method, supplying parameters 0 and vec.size() - 1 as parameters that specify the range of indices to be sorted. To determine the number of entries in a Vector, you send it a size() message. This returns an int that can be assigned to a variable, used as a parameter, or, in this case, used as part of a more complex expression.

The swap(int, int) Method

This method is also quite short. The idea is to copy the entry at index i of vec to a temporary variable, copy the entry at index j to the entry at index i, and then copy the temporary variable to the entry at index j. For the first statement, use the code
    Object temp = vec.get(i);
The variable is declared as an Object to allow any kind of object to be stored there.

To copy an value to the i-th position in a Vector you use code with the form

    vectorVariable.set(i, value);
That is, you send a set() message to the Vector, passing the index and desired value as arguments. You will need two statements like this to complete the swap() method. After it is done you can try recompiling the class to check for errors.

The sort(int, int) Method

The two parameter sort() method is the longest piece of code in the assignment. It's code is similar to a C quicksort function as described in quicksort.C. The pivot value variable described there should be declared as an Object initialized to null. The code in that web page uses in-line code in two places to swap elements. Your code should replace the in-line code with calls to your swap() method. This means that you will not need the tempEntry variable - it is incorporated into the swap() method. Most of the rest of the Java code is identical to the C code except for a different method names and differences in accessing entries of Vectors. Remember to use the get() method to retrieve a Vector entry and set() to modify a Vector entry. Finally, the Comparator comp of your VectorSorter is used for comparing two objects. For example, the following expression is true when the entry at index uhi of vec is greater than the pivot:
    comp.compare(vec.get(uhi), pivot) > 0
That is, you pass two things to be comped as arguments for the compare method of the Comparator. It returns a negative value if the first object is less than the second, 0 if the two objects are equal, and a positive value if the first object is greater than the second.

A Minor Change to the Test1 Class

A small change is needed to the main() method of the Test1 class. At the end of the method, send a sort() message to the VectorSorter vs. Then send another print() message to the VectorStatement. To make the output clearer, you should preceed the print() message with a short output statement indicating that the output that follows is after sorting.

When you are done with the modifications recompile the class. Then you can execute it again. This time you should see the Vector contents printed twice, once before sorting and once after sorting.

[an error occurred while processing this directive]