ulrail.gif

HW J2: Vector Usage

  ur-2c.gif urrail.gif
blrail-nonavbar.gif

Home | Resources | Homeworks | Exams
Slides | Labs | Contacts | Submit | TAs

br-2a.gif   br-2c.gif brrail.gif
spacer.gif spacer.gif spacer.gif

 

This homework is due electronically by 10 a.m. on Friday 17 February 2006.

 

Purpose

This 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.*;

Background

There 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:

  • To print the Vector to the screen, you just put the Vector into a println() statement, as shown on the 8th line.  We will see why you can do this when we study chapter 4 in the textbook.
  • When you are retrieving an element from the Vector, you need to cast it into a String, as shown on the 10th line.  We will see why this is necessary at the very end of the course.

Compiler Warnings

When 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 Methods

The following are the methods in the Vector class that you will be using for this homework.

  • Vector (): The constructor, it creates a new Vector object.
  • add (Object o): Adds the passed object to the Vector in the next available spot.
  • int indexOf (Object which): Searches the Vector to find the position where the passed object is.  Returns -1 if the element is not found.
  • Object firstElement (): Returns the first element of the Vector.
  • Object lastElement (): Returns the last element of the Vector.
  • Object get (int which): Returns the element at the passed position
  • Object remove (int which): Removes the element at the passed position
  • set (int index, Object element): Sets the element at position index to the passed element.
  • clear (): Clears the Vector (removes all elements from it)
  • int size (): Returns the size of the Vector.

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.

Design

Your code must perform the following steps

  1. Create a new Vector
  2. Print out the Vector's size, and all the elements it contains (via a single System.out.println() statement -- see above)
  3. Read in 5 Strings from the keyboard, one at a time, and insert each of them into the Vector
  4. Print out the Vector's size, and all the elements it contains (via a single System.out.println() statement -- see above)
  5. Get another String from the keyboard
  6. Search the Vector for that String, and print out the result of that search (this uses the indexOf() method)
  7. Print the first and last elements of the Vector
  8. Get an int i from the keyboard
  9. Print out the element at position i
  10. Remove the element at position i (and print that you are doing so)
  11. Print out the Vector's size, and all the elements it contains (via a single System.out.println() statement -- see above)
  12. Get a String s and an int i from the keyboard
  13. Set the element at position i to the String s (and print that you are doing so)
  14. Print out the Vector's size, and all the elements it contains (via a single System.out.println() statement -- see above)
  15. Lastly, clear the Vector (remove all elements) (and print that you are doing so)
  16. Print out the Vector's size, and all the elements it contains (via a single System.out.println() statement -- see above)

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 practices

The good programming practices listed in HW J1 must be included in this (and all) homeworks.

Sample execution

The 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: []

Submission

When you are finished, submit the VectorUsage.java file.

spacer.gif
spacer.gif footer-middle.gif spacer.gif