CSCI 201 Lab 12 -- The Vector Class in Java

Getting started

If folders C:\files\Resize or C:\files\VectorFrame exist on your machine delete them.

The Vector class and resizing

As you know from lecture, Vectors store collections of objects, where an object is created by instantiating a class, any class. In fact, a single Vector can store a collection of objects that have been instantiated from different classes, in other words, objects of different types. This is possible because, the Vector class stores its collection of objects using an array of Object references, e.g.,


Object[] objArray = new Object[10];
As you will learn in Chapter 7, Object references can point to any type of object because all classes are ultimately derived from the Object class.

Oh, but the Vector class does a lot more than that. For, example, what would happen if you created an array whose length was five, and you ultimately needed to store six items? You would have to resize the array in order to add the sixth element. Vectors automatically resize for you. To better appreciate what this means, you are asked to write a function that will resize an array.

First, create a new workspace:

  1. Open JCreator then choose, File, New.
  2. Next, pick Empty Project and name the file Resize.
  3. Again choose File, New. Pick Java File and name this one Resize also.
  4. Now, copy and paste the code from this link into your java file.

You are now ready to write the code for the resizeAndAdd function.


Lab Check-off 1

After you have completed and tested your program, show it to your instructor.


Other Vector methods

In this portion of the lab, you will use some of the other methods available in the Vector class. Download the following zip file and open the workspace after extracting that file. The zip file can be found here. Compile and run the program, you should see the following window:



The program consists of four classes: CenterPanel, VecFuncs, VecTest, and VectorFrame. You will modify the VecFuncs class, but it is also important for you to understand how the VecTest and the VectorFrame class work together to produce the display; you will use the display to test the methods that you write in VecFuncs.

Open the VecFuncs.java file and notice that, on line 27, an array of four button references is created. In the for-loop starting on line 29, the button objects are created and the button event listeners are specified as being in the VectorFrame object containing the buttons. The actionPerformed() method beginning on line 49 handles the button events. Notice that the if-else structure in actionPerformed() tests the name of the button, e.g.,


		else if(action.equals("Step Up"))
and calls a method in the VecTest class in response to each button push. For example, VecTest's method advance() is called when the "Step Up" button is pushed. Now switch to the VecTest class, and look at the methods called to handle each button. Notice that the VecTest methods, in turn, call methods in the VecFuncs class, i.e., the methods that you will write. Notice also that VecTest's instance variable FuncNum is used to select which VecFuncs methods are called.

Now run the program and depress the various buttons. Make sure that you understand how each button changes the display. Once you are comfortable with how the display works, modify the four functions in the VecFuncs class as described in the table below. Use the display to verify that your functions are working correctly.


Func1 Store four String objects in vector. The first is "Hello", the second "there.", third is "My name is", and last "Goofball.".
Func2 Remove the string "there" from vector. Then use the correct function to change "Hello" to "Hello there" and the string "Goofball" to your last name.
Func3 This method should insert your first name just before your last.
Func4 Use the appropriate Vector method to clear vector.


Lab Check-off 2

After you have completed and tested your program, show it to your instructor.