CMPSCI 187: Programming With Data Structures

David Mix Barrington

Fall, 2012

Programming Project #1: The StringBag Class

Originally posted 9 September 2012, due at 11:59 p.m. EDT on Monday 24 September 2012, by placing a .java file in your cs187 directory on your edlab account. For more information on accessing the edlab, see here or ask in discussion section on 12 September.

Goals of this project:

  1. Write a complete class implementing a given specification.
  2. Write a program using objects and classes.

Note in green added on 14 September.

Text in purple added on 20 September.

Text in orange added on 22 Septemeber.

Your assignment is to create a Java class called "StringBag", as described in Exercise 29 on page 151 of DJW. A StringBag object contains a collection of zero or more strings. Like a StringLog object, it supports the methods insert, clear, size, isFull, and toString. (Unlike StringLog objects, StringBag objects have no name parameter.) In place of the contains method of a StringLog, it has a method remove( ) (with no parameters) that removes and returns a randomly chosen element of the collection.

As with the ArrayStringLog class in DJW, you should have two constructors if you build an array-based class. The first creates an array of a size given as a parameter, and the second makes an array of a default size which we will make 100, as DJW did. If you make a linked implementation, you should have just one constructor with no parameter (and the isFull method always returns false). Note that StringBag objects need not have names as StringLog objects do, and the constructors should not take names as parameters.

To allow us to test the behavior of your class, for this project we are going to have you also implement a different remove method, that takes an int parameter. The method call remove (k) should remove and return the k'th element of the StringBag, in the order shown when we call the toString method. If k is less than 0 or greater than the size minus 1, your method should throw an exception. (An ArrayOutOfBoundsException is fine.) The removal of the object creates a gap in the sequence of strings, and we care how you fill the gap because we are going to test for the behavior we expect. You should move the last element of the sequence to fill the gap, unless the gap is already in the last place.

Most of our testing will involve the remove(k) method that takes an int parameter, but you should also implement the original remove ( ) method, by generating a random int k, in the range from 0 through size - 1, using the nextInt(int) method of a Random object as you did in Discussion #2, and then calling remove(k).

(This used to say "generating a random int named x, letting k be Math.abs(x) % size, and then calling remove(k). (We use Math.abs because Java's % doesn't work the way I would like it to on negative arguments.)" But using the method given in the Random class is simpler and more reliable.)

You should put your class in your EdLab space, in a directory called "cs187/proj1", and test the behavior with a main method or a driver class. (We will test the code with our own driver class.)

If you create your code within Eclipse (which we encourage but don't require), you will want to ignore the warning message that says "The use of the default package is discouraged". Bear in mind that if you make the mistake of declaring the StringBag class in a package, our driver class will not see it (since it will not be in a package) and we won't be able to test your code.

Here is a driver for the StringBag class, which we may or may not use to grade your projects. If your class does not work with this driver, you have a problem -- if it does work, that doesn't mean you should avoid doing more tests yourself.

Last modified 22 September 2012