
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

CS 331 Project #2 Sept. 22, 2004
Due at start of class on Wednesday, Sept. 29, 2004 50 points
The class java.util.Vector implements a growable array of objects. (Exercise 19, Page 208, Textbook) Simple correspondences between ADT List operations and java.util.Vector methods are as below:
ADT List operations java.util.Vector methods createList() Vector () Vector (int initialCapacity, int capacityIncrement) isEmpty() isEmpty () size() size () add(index, item) add (int index, Object element) remove(index) removeElementAt (int index) removeAll() removeAllElements () get(index) get (int index) capacity () Returns the current capacity of this vector.
Write a client class that implements a list by using Vector and completes the following tasks: (10 points each)
a. Declare a vector using the default constructor. Add 6 items (milk, eggs, butter, apples, bread, and chicken) to the vector. What do methods size() and capacity() return?
b. Write a method displayList() that prints all items stored in the vector.
c. Write a method replace(aList, i, newItem) that replaces the item in index i with a new item. Use this method to replace apples by nuts in the list.
d. How many items must be added to the vector to cause the capacity to change? What is the new capacity?
e. Remove all of the elements from the vector. Does the capacity change? Why do you think the Vector class behaves this way?
Document your source code according to the following rules (page 37, Textbook):