


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Instructions for lab 05 of cs1110 course, which focuses on the java vector class. It covers the history of vector class, its functionality, and methods. Students will learn how to create, manipulate, and experiment with vector<character> in java 1.5. The document also includes tasks for students to perform in drjava to understand vector's behavior.
Typology: Lab Reports
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Name ___________________ Netid_____________________ Class java.util.Vector provides the ability to maintain a growable/shrinkable list of objects. You don't know ahead of time how many objects will be in the final list. In this lab, you will gain some experience with class Vector and learn just how useful it can be. This material is covered in Sec. 5.3 (pp. 184--188) of the text. After the lab, study that section. Also, the API is your friend. Use it! For this lab, you can go directly to the Vector page of the API. Clicking the link will open the page in a new window.
The developers of Java knew early on that they wanted some kind of growable list, so they created class Vector and shipped it out with Java v1.0. Later, however, they wanted to generalize the idea of a list. So they created new classes that provide a more general implementation than Vector. Rather than get rid of Vector —for "backward compatability" reasons, you can't simply throw out old stuff— for Java v1.2 the developers added new methods to class Vector so that it would be consistent with the other, newer, classes. Many of these new methods do the same thing as the old ones. Some of the old ones are "deprecated"(deprecate means to disapprove of, often with mildness). But they will NOT go away, and you can use them.
Several years ago, Java switched from version 1.4 of the language to version 1.5. Java 1.5 and Java 1. makes it easier to work with Vectors of a particular class of elements, like a Vector of elements of class Character, which we will be working with here. If your computer has Java 1.4, you have two options:
A Vector v contains a list of elements, numbered 0, 1, 2, .... Function v.size() tells how many elements are in the list. We use the following non-Java notation to refer to parts of the list. The notation helps us write things more clearly and succinctly. We refer to the elements in the list as v[0], v[1], ..., v[v.size()– 1]. If we want to refer to part of the list, say elements v[h], v[h+1], ..., v[k], we write v[h..k]. In Java 1.5, you create a Vector that can contain elements only of a class C and store its name in a variable using this assignment and new expression: Vector
Here is a list of the old methods, the corresponding new ones, and what they do: Old method New method Purpose v.addElement(Object ob) v.add(Object ob) append ob to v's list. If v's type is class Vector
Download file Lab05.java from the course website or from here. This program will help you understand exactly what is happening when you call various methods of a Vector. Look over the code that we have provided. We have defined a Vector
lab.v.remove(new Character('7')); lab.v.indexOf(new Character('1')) lab.v.indexOf(new Character('B')) lab.v.get(5) lab.v.get(12) lab.v.indexOf(lab.v.get(2)) What is this call doing? lab.v.indexOf(lab.v.get(8)) Why doesn't this return 8? lab.v.firstElement() lab.v.set(1, new Character('O')); lab.v.capacity() lab.v.size() lab.v.toString() lab.v.trimToSize(); lab.v.setSize(12); What is in the cells that have red question marks? Is the new capacity also 12?
We have written four method stubs for you to implement; implement them: public void swap(int first, int second) Swap the objects at v[first] and v[second] public boolean moreThanOne(Object obj) = "there is more than one occurrence of obj in v" Hint: We did something similar in Lab 03 with Strings. public boolean hasExtraSpace() = "there is space allotted to v that is not being used" public String toString() = a string that has contains the characters of v, in order Show your work to your TA or a consultant.