Fundamentals of Computer Science II (CSC-152 98S)

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

Lab 6: Vectors and Arrays

Description: In the laboratory, you will be developing an interactive tester for java.util.Vector. Your tester can be based on a sample tester I've developed.

Purpose: In this lab, you will gain experience with arrays and Vectors in Java. You will also begin to consider the process of testing your classes.


Since we'll be working with Vectors, make sure that you've read the documentation for the Vector class. Consider which operations may be particularly confusing.

Next, make a copy of As always, you can simply type
% example
This program is a primitive tester that you will be extending in this lab. Read through the code and make sure you understand how it is organized. You may want to compile and run it. Here is a sample run

% ji VectorTester
a val  append a value
h      get this help message
l pos  look up the value at position pos
p      print the current vector
q      quit

> a aardvark

> p

> a zebra

> p
[aardvark, zebra]

> l 0
0: aardvark

> l 1
1: zebra

> l -1
-1 is an invalid position

> l 2
2 is an invalid position

> q

Initial extensions

Extend the tester so that it is possible to test many of the other methods provided by java.util.Vector. At a minimum, you should develop methods to test

In addition, you should include a method of filling a subvector so that it is possible to indicate that we should put the value XXX in all positions between A and B.

Additional extensions

If you still have time, see if you can determine other interesting test methods and uses. For example, you might want to find all the positions at which a particular string occurs.

Code for sample

Here is the code for the primitive that you may use as the starting point.

import java.lang.ArrayIndexOutOfBoundsException;
import java.lang.NumberFormatException;
import java.util.Vector;
import rebelsky.util.NumberRangeException;

 * Allow interactive testing of the java.util.Vector.
 * Used to extend understanding of what that class does in various
 * situations.
 * @author Samuel A. Rebelsky
 * @author YOUR NAME HERE
 * @version 1.0 of February 1998
public class VectorTester
  // +------------+---------------------------------------------------------
  // | Attributes |
  // +------------+

   * The object used for output.
  protected SimpleOutput out;
   * The object used for input.
  protected SimpleReader in;

   * The vector we're testing.
  protected Vector vec;

  // +--------------+-------------------------------------------------------
  // | Constructors |
  // +--------------+

   * Build a new tester. 
  public VectorTester()
    in = new SimpleReader();
    out = new SimpleOutput();
    vec = new Vector();
  } // VectorTester()

  // +---------+------------------------------------------------------------
  // | Methods |
  // +---------+

   * Append an element to the current vector.
  public void append(String val)
  } // append(String)

   * Look up a value.
  public void lookup(int pos)
    try {
      out.println(pos + ": " + vec.elementAt(pos));
    catch (ArrayIndexOutOfBoundsException e) {
      out.println(pos + " is an invalid position");
  } // lookup(int)

   * Give instructions for using this program.
  public void giveHelp()
    out.println("a val  append a value");
    out.println("h      get this help message");
    out.println("l pos  look up the value at position pos");
    out.println("p      print the current vector");
    out.println("q      quit");
  } // giveHelp()

   * Print the current vector.
  public void print()
  } // print()

  // +------------+---------------------------------------------------------
  // | Attributes |
  // +------------+

   * Repeatedly prompt for what to do and then do it.
  public static void main(String[] args)
    // The object used to drive the testing
    VectorTester tester = new VectorTester();
    // The base part of the user's response
    String resp;
    // The value the user wants to add or retrieve
    String val;
    // The index the user wants to use
    int index;

    // Provide some basic instructions

    // Keep going until we hit the end of file or the user quits.
    // One exit is done using "break", and the other is done using
    // a catch of the end-of-file exception, so it looks as if our
    // loop continues indefinitely.
    try {
      while (true)
        // Print a prompt
        tester.out.print("> ");
        // Get the first response
        resp =;
        // Make sure the response is non-empty
        if (resp.length() == 0) {
          tester.out.println("You must give a non-empty response.");
        // Determine what to do based on the first character
        switch (resp.charAt(0)) {
          case 'a':				// append
            // Get the value and append it
            val =;

          case 'h':				// help

          case 'l':				// lookup
            // Get the position and look up
            try {
              index =;
            catch (NumberFormatException e) {
              tester.out.println("Invalid position");
            catch (NumberRangeException e) {
              tester.out.println("Invalid position");

          case 'p':				// print

          case 'q':				// quit
            break LOOP;
        } // switch
        // Skip anything else on the line;
      } //while
    } // try doing the input
    catch (IOException e) {
      // We're done
    } // catch(IOException)

  } // main
} // VectorTester

[Instructions] [Search] [Current] [Changes] [Syllabus] [Handouts] [Outlines] [Labs] [Assignments] [Examples] [Bailey Docs] [SamR Docs] [Tutorial] [API]

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

Source text last modified Tue Feb 10 11:32:00 1998.

This page generated on Tue Feb 10 11:30:15 1998 by SiteWeaver.

Contact our webmaster at