Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab 05: Understanding Java Vector Class, Lab Reports of Computer Science

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-uqr
koofers-user-uqr 🇺🇸

10 documents

1 / 4

Toggle sidebar

Related documents


Partial preview of the text

Download Lab 05: Understanding Java Vector Class and more Lab Reports Computer Science in PDF only on Docsity! CS1110 Lab 05. Class Vector Fall 2008 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. A History Lesson 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. Caution: Another important history lesson Several years ago, Java switched from version 1.4 of the language to version 1.5. Java 1.5 and Java 1.6 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: 1. Deinstall the Java 1.4 compiler and install Java 1.5 or 1.6. 2. For this lab, follow the instructions in the comment that appears at the top of file Lab05.java, which you will obtain later from the course web page. What a Vector contains: 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 <C> v= new Vector <C>(); The appearance of <C> says that the Vector may contain only elements of class C. In this lab, we will be working with Vector<Character>, meaning a Vector whose elements are of class Character. Vector v has a capacity, which is the number of elements for which space has been allocated. This is different from its size, which is the number of elements in it. When an element is to be added to v but the size is already equal to the capacity, Java allocates space for more elements —for reasons of efficiency, the capacity is usually doubled. The capacity can also be controlled by the programmer. 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<C>, ob should be of class C or a subtype of C. v.insertElementAt(int k, Object ob) v.add(int k, Object ob) change v's list to v[0..k-1], ob, v[k..]. If v's type is class Vector<C>, ob should be of class C or a subtype of C. v.elementAt(int k) v.get(int k) = v[k] v.removeElement(Object ob) v.remove(Object ob) remove ob from the list in v (if it is there) v.removeElementAt(int k) v.remove(int k) remove v[k] from v's list, changing it to v[0..k-1], v[k+1..] v.removeAllElements() v.clear() remove all elements from v v.setElementAt(Object ob, int k) set(int k, Object ob) replace v[k] by ob Other useful methods in class Vector are: v.size() = the number of elements in v's list v.capacity() = the number of elements that are currently allocated for v's list --this can be different from the number of elements that are actually IN v's list! v.indexOf(Object ob) = i, where v[i] is the first occurrence of ob in the list v.lastIndexOf(Object ob) = i, where v[i] is the last occurrence of ob in the list v.toString() = a comma-separated list of the elements in v, enclosed in brackets Task 1. Experimenting with 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<Character> v, which you will use throughout this lab. It is public, so you can access it from the Interactions pane of DrJava. We have also defined two constructors, which will illustrate different qualities of Vector. Read the specifications so you understand what each one does. Don't worry about the stub methods yet —the ones you have to write; you'll get to them later. Compile class Lab05 and type this into the interactions pane.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved