If folders C:\files\Resize or C:\files\VectorFrame exist on your machine delete them.
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:
You are now ready to write the code for the resizeAndAdd function.
After you have completed and tested your program, show it to your
instructor.
In this portion of the lab, you will use some of the other methods available in the Vector class. Download a self-extracting archive of the Lab 12 project and store it in the C:\files directory. In Windows Explorer, double-click on the file and extract the project folder to the c:\files directory. This should create the project folder C:\files\VectorFrame. Compile and run the program in that project, 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. |
After you have completed and tested your program, show it to your instructor.