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.

## Preparation

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

Next, make a copy of `VectorTester.java`. As always, you can simply type
`% example VectorTester.java`
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
[aardvark]

> 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

• `indexOf(Object)`,
• `removeElementAt(int)`, and
• `setElementAt(Object, int)`

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.

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 `VectorTester.java`

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

```

import java.io.IOException;
import java.lang.ArrayIndexOutOfBoundsException;
import java.lang.NumberFormatException;
import java.util.Vector;
import rebelsky.io.SimpleOutput;
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
* @version 1.0 of February 1998
*/
public class VectorTester
{
// +------------+---------------------------------------------------------
// | Attributes |
// +------------+

/**
* The object used for output.
*/
protected SimpleOutput out;

/**
* The object used for input.
*/

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

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

/**
* Build a new tester.
*/
public VectorTester()
{
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()
{
out.println(vec.toString());
} // 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
tester.giveHelp();

// 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 {
LOOP:
while (true)
{
// Print a prompt
tester.out.println();
tester.out.print("> ");
// Get the first response
// Make sure the response is non-empty
if (resp.length() == 0) {
tester.out.println("You must give a non-empty response.");
tester.giveHelp();
continue;
}
// Determine what to do based on the first character
switch (resp.charAt(0)) {
case 'a':				// append
// Get the value and append it
tester.append(val);
break;

case 'h':				// help
tester.giveHelp();
break;

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

case 'p':				// print
tester.print();
break;

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.