CMPSCI 187: Programming With Data Structures

David Mix Barrington

Fall, 2011

Programming Project #3: The Dropout Stack

Originally posted 30 September 2011, due at 11:59 p.m. EDT on Tuesday 11 October 2011, by placing .java files and .class files in your cs187 directory on your edlab account. Please place files for this assignment in a subdirectory of cs187 called "project3".

As we get questions on this assignment we will put answers on the Q&A page.

Corrections in purple added 1 October 2011. Text in green added (and obsolete text removed) 6 October 2011.

Goals of this project:

  1. Implement a stack-like class using arrays.
  2. Use circular arrays and dynamic sizing.
  3. Write a generic class including a constructor with a type cast.

In Chapter 3 of L&C, the authors define a class ArrayStack that implements their StackADT interface. In this project (based on their Programming Project 3.5 on page 67) you will implement a similar class called a dropout stack. A dropout stack is like an ordinary stack except that it has a fixed capacity. When a push would cause the number of elements in the stack to exceed the capacity, the push succeeds as normally but the oldest (bottom) element in the stack is discarded.

In order to implement a dropout stack with an array and maintain O(1) time for all five stack operations, we will need to use a circular array. In a circular array x of capacity k, the first element x[0] is thought of as coming immediately after the last element x[k-1]. As we implement a series of pushes and pops without moving the mass of elements in the array, the "top" and "bottom" locations in the array, representing the top and bottom of the stack, will both move around the array in much the same way that the "top" location moves in ArrayStack. You can see a circular array implementation of a queue in L&C Chapter 5.

Assignment:

  1. Write a generic class DropoutStack<T> that implements a dropout stack using a circular array. Define the constant DEFAULT_CAPACITY to be 3.

  2. Write two constructors, a zero-parameter one to create a DropoutStack object with the default capacity, and another with a single int parameter c that creates an object with capacity c. Remember that these constructors, like the one for ArrayStack in L&C, will need to use a typecast to create an array of T elements when T is a type variable.

  3. You must implement the five stack methods push, pop, peek, isEmpty, and size from the StackADT interface. (But since we aren't including the code of that interface, don't bother to have your class implement StackADT.)

  4. Peeking or popping from an empty stack should cause an EmptyCollectionException -- you will need to declare this exception type in a separate file. (This used to incorrectly say "pushing or popping".) Also, you should declare the new exception class to extend RuntimeException rather than just Exception.

  5. Write a method public void resize(int newCapacity) in the DropoutStack class. When called, this method should change the capacity of the calling object to newCapacity and copy the relevant parts of old array into a new one of that size. If the new array is larger than the current size of the stack, add null entries for the new spaces. If the new array is smaller, discard enough of the oldest elements to make the rest of the stack fit into the array.

  6. Write a class ProjectThreeStack that extends DropoutStack but never discards any elements. When a push would bring it over capacity, it should call its resize method to double its capacity, just as ArrayStack does.

In summary, you are including both .java and .class files for the classes DropoutStack<T>, EmptyCollectionException, and ProjectThreeStack<T>. These should be in files DropoutStack.java, EmptyCollectionException.java, and ProjectThreeStack.java. The class name for the generic classes has angle brackets inside the file, but the file name has no angle brackets.

You may borrow code from any of L&C's classes, with specific attribution in a comment. In particular, the implementation of ArrayStack should be similar in many ways to what you want, since it is a generic class that uses arrays.

Last modified 6 October 2011