[an error occurred while processing this directive]

Programming Assignment 1 - Step 2


For this step, you will need declare variables for a VectorSorter, write a constructor for the class, write a print() method for the class, and write a main() method for the Test1 class.

Declaring Variables and a Constructor Method for the VectorSorter Class

A VectorSorter needs two variables to work with: a Vector that it sorts and a Comparator that it uses for comparing two items in its Vector. The declaration of a Java variable is much like a C variable declaration:

    Type variableName = initialization;
As in C, the initialization is optional. However, it is a good habit to initialize all object variables. The VectorSorter is designed so that both of its variables will be passed in from outside the class so an initial null value is appropriate. Thus the declaration for the Vector variable is
    Vector vec = null;
The declaration for the Comparator should be similar. It should be named comp. Both delarations should be placed inside the braces of the class definition. The classes for both of these variables are in the java.util package.

In order to do anything useful, a VectorSorter needs non-null values for its Vector and Comparator. In Java, you can define a constructor that creates an object and initializes it to a useful state. The declaration of a constructor has the form

    public ClassName(parameters) {
	initialization code
    }
A constructor is much like a C function definition, using the name of the class as the name of the constructor and omitting the return value type because it is redundant.

For the VectorSorter class, the constructor should have two parameters: a Vector v and a Comparator c. The declaration should look like

    public VectorSorter(Vector v, Comparator c) {
	initialization code
    }
The initialization code should assign v to vec and should assign c to comp. The code for doing this is just like C code. After putting the constructor declaration in your class definition, you may want to recompile the class to check for errors.

Writing a print() Method for the VectorSorter Class

Although a well-designed VectorSorter class would not need a method for printing its Vector, it is useful to include a print method in an introductory exercise. Defining a Java method is much like writing a C function. The form of the declaration is

    public ReturnType methodName(parameters) {
	method code
    }
If the method does not have a return value then ReturnType is void as in C. You should write a method named "print" for your VectorSorter class that prints the Vector variable of the VectorSorter. The code for the method is much like C code in structure - Java for loops have the same syntax as C for loops except that you can declare loop variables in the loop as follows:
    for (int i = 0; i < vec.size(); i++) {
	loop code
    }
Your for loop only needs one statement to print the Vector entry indexed by i. Printing is done with a statement of the form
    System.out.println(StringExpression);
The System.out refers to the Java standard output stream. It is much like stdout in C programs, but the syntax for using it is different. Since it is an object, instead of passing it as a parameter, you send it a message to get it to do something. In most cases, you will send either a print() or println() message to the System.out object, depending on whether or not you want a newline appended. The parameter can be any expression with a String (a class in the java.lang package) value.

For your print() method, you want to print a line with the i-th entry of the Vector. You get this by sending an elementAt() message to the Vector, passing i as a parameter. You often use more complex String expressions as println() arguments. For example, for String values, Java uses + as a concatenation operator. Thus the following expression is the string value of the i-th entry of vec with two leading spaces for indentation:

    "  " + vec.elementAt(i)
Java will automatically convert vec.elementAt(i) to a string value.

With this information, you can write the print() method for the VectorSorter class. It can be placed anywhere in the class definition, but the designers of the Java language recommend a style with methods following variable declarations. After you have added the method recompile the class.

Writing a main() Method for the Test1 Class

By now, you are getting tired of reading long instructions for writing a short piece of code, so here is the main() method for the Test1 class:

    public static void main(String [] args) {
	Integer A[] = {
	    new Integer(99),
	    new Integer(43),
	    new Integer(22),
	    new Integer(17),
	    new Integer(57),
	    new Integer(32),
	    new Integer(43),
	    new Integer(19),
	    new Integer(26),
	    new Integer(48),
	    new Integer(87),
	    new Integer(12),
	    new Integer(75),
	    new Integer(0)
	};
	Vector v = new Vector(Arrays.asList(A));
	Comparator c = Comparators.forInteger();
	VectorSorter vs = new VectorSorter(v, c);
	System.out.println("Before sorting, the entries of vec are:");
	vs.print();
    }

You should add this code to the Test1 class and recompile it. Since the class has a main() method, it is executable. Its execution will be described in the last section of this web page.

Explanation of the Test1 Class main() Method

As in C programs, execution of Java programs begins in a main() method. The declaration always has the following form:

    public static void main(String [] args) {
	.
	.
    }
The method is declared static because it is executed by sending a message to a class rather than an object; there are no objects other than classes when execution begins. The parameter args is an array of Strings, used like the argv parameter of a C main() function. For the Test1 class, you do not need an argc parameter since you can obtain the length of any Java array with a simple expression: args.length is the number of Strings in the args array.

You can declare initialized array variables like in C:

    Integer A[] = {
	new Integer(99),
	.
	.
    };
Here, the entries of the array A are Integers. The Integer class (defined in the java.lang package) is just an object form of an int variable. The Integers will soon be placed into a Vector, which can only contain objects, not simple data types.

The following statement declares a Vector variable, initializing it from the array A.

    Vector v = new Vector(Arrays.asList(A));
The conversion expression (Arrays.asList(A)) is not worth memorizing; you will rarely use it. It converts the array into a List object. However, the general form of the initialization is important:
    ClassName variableName = new ClassName(parameters);
This illustrates how objects are created using a new expression. The Vector class has a constructor that takes a List parameter and converts it to a Vector.

The constructor that you wrote for the VectorSorter class has two parameters, A Vector and a Comparator. The next line creates the Comparator.

    Comparator c = Comparators.forInteger();
This uses the Comparators class in the gshute.util package. It provides methods for constructing Comparators for several of the standard Java classes.

Now that you have a Vector and a Comparator, you can construct a VectorSorter using the constructor that you wrote earlier.

    VectorSorter vs = new VectorSorter(v, c);

The following statements generate some program output, printing the contents of the Vector that was used to construct the VectorSorter. This uses the print() method that you wrote earlier.

    System.out.println("Before sorting, the entries of vec are:");
    vs.print();

Executing the Test1 Program

To execute a Java program, you use the Unix java command, specifying the name of a class as a command-line argument. The class must contain a definition of a main() method. Since your Test1 class has a main() method, it can be executed with the Unix command

    java Test1
When you do this you should see a printout of the initialized contents of the Vector used by your VectorSorter. The values printed should be the numbers 99, 43, 22, 17, 57, 32, 43, 19, 26, 48, 87, 12, 75, and 0, in that order. These are just the values in the initialization of the main() method local variable A. [an error occurred while processing this directive]