This homework is due electronically by 10 a.m. on Friday 17 February 2006.
PurposeThis homework will introduce you to the Vector class. You will be creating a Vector object, and calling various methods to manipulate that object. There is one file that you will need to submit for this homework, called VectorUsage.java. We are not providing you with skeleton code. The class declaration MUST be: public class VectorUsage. Note that capitalization (the capital 'V' and 'U') -- if you do not capitalize it as shown, Java will not compile in properly, and you will have points taken off. All of your code must be within the main() method in your VectorUsage class. As the Vector class is in the java.util library (along with the Scanner class), you will need to have the following line at the top of your file: import java.util.*; BackgroundThere are many times in a computer program that we want to store a number of "things", such as objects. A Vector object allows us to do that. Note that the Vector class has nothing to do with a mathematical vector. A Vector allows you to add and remove "things", so that you can keep them in a collection. For this homework, we will be creating String objects, and putting them into our Vector. Think of a Vector object like a container (such as a backpack) -- you can insert (add) items into it, remove items from it, count how many items are in it, etc. Elements inserted into a Vector have an index value, in the same way that the characters in a String have an index. Thus, the first element in a Vector has index 0, etc. The following code illustrates the use of a Vector. Vector v = new Vector(); // Creates a new Vector object v v.add ("first string"); // Adds "first string" as the first element in // the vector (it has index 0) v.add ("second string"); // Adds "second string" as the second element in // the vector (it has index 1) // At this point, the Vector contains two elements System.out.println (v.size()); // Prints the size (2) to the screen System.out.println (v); // Prints the entire Vector (meaning each element) // to the screen String s = (String) v.get(0); // Gets the first element from the Vector v.remove (0); // Removes the first element (the one at position 0) // At this point, the Vector contains one element: // "second string" A few things to notice in this code segment:
Compiler WarningsWhen you compile your program for HW J2 (and the above code), you will receive the following warning: Note: VectorUsage.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. This is a warning, not an error message. You can safely ignore this message -- your program will still have compiled properly (assuming there were no other error messages). Vector MethodsThe following are the methods in the Vector class that you will be using for this homework.
As mentioned above, any time you obtain an element from the Vector (via firstElement(), lastElement(), get(), or remove()), you need to cast the returned element back as a String, as shown below. String s = (String) v.get(0); Lastly, you will notice that the methods take in (and return) an Object instead of a String. For this homework, you can assume that they are the same thing. DesignYour code must perform the following steps
Thus, your program will require 9 values to be input from the keyboard: 5 Strings, another String, an int, a String, and an int. Make sure your program asks for these (and only these!) nine inputs in that order. You can assume that the int values entered will be valid (i.e. that they won't be negative or larger than the size of the Vector). The methods for doing the above steps are described below. As the course progresses, we will provide you with less step-by-step instructions for the homeworks -- for example, we don't specify that you need to create a Scanner object to obtain the input (this should be obvious from the fact that you need to get user input), or that you need to print out a legend (which is included in the good programming practices, below). The easiest way is to progress through the 16 steps, in order, testing after every step or two. Testing is easy -- just run the program, and see if it works properly up to that point. The steps where you print out the Vector (steps 2, 4, 11, 14, and 16) allow you to see what's going on, and if your program is working properly. Although there are a lot of steps, each step is relatively small, and (with the exception of step 3) will only take a few lines of code. Scanner warning: The Scanner class sometimes acts a bit finicky, and this homework may encounter this. To avoid any problems, use next() (instead of nextLine()) to read in a String. We won't be testing your code with Strings that contain spaces, so you don't need to either. This should allow you to avoid any of these issues. Good programming practicesThe good programming practices listed in HW J1 must be included in this (and all) homeworks. Sample executionThe text in red is the 9 values that were input by the user. Vector Manipulation The Vector has size 0, and contains the following elements: [] Enter the 1st string to insert into the Vector: aaa Enter the 2nd string to insert into the Vector: eee Enter the 3rd string to insert into the Vector: iii Enter the 4th string to insert into the Vector: ooo Enter the 5th string to insert into the Vector: uuu The Vector has size 5, and contains the following elements: [aaa, eee, iii, ooo, uuu] Enter a String to search for: bbb String 'bbb' is in vector: -1 First element: aaa Last element: uuu Enter an int value to get the element at that index: 2 Retrieved element from position 2: iii Removing element at position 2 The Vector has size 4, and contains the following elements: [aaa, eee, ooo, uuu] Enter a String to insert into the Vector: rrr Enter an int position to insert that String: 3 Setting the element at position 3 to 'rrr'... The Vector has size 4, and contains the following elements: [aaa, eee, ooo, rrr] Clearing the Vector... The Vector has size 0, and contains the following elements: [] SubmissionWhen you are finished, submit the VectorUsage.java file. |