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:
In Chapter 3 of L&C, the authors define a class
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 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.
DropoutStack<T>that implements a dropout stack using a circular array. Define the constant
DEFAULT_CAPACITYto be 3.
DropoutStackobject 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
ArrayStackin L&C, will need to use a typecast to create an array of T elements when T is a type variable.
StackADTinterface. (But since we aren't including the code of that interface, don't bother to have your class implement
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
RuntimeExceptionrather than just
public void resize(int newCapacity)in the
DropoutStackclass. When called, this method should change the capacity of the calling object to
newCapacityand 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.
DropoutStackbut never discards any elements. When a push would bring it over capacity, it should call its
resizemethod to double its capacity, just as
In summary, you are including both .java and .class files for the classes
DropoutStack<T>, EmptyCollectionException, and
These should be in files
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
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