Question text in black, answers in blue.
Just that you aren't expected to have L&C's code for the StackADT interface present when you compile your DropoutStack class. So if you said "implements StackADT" in your declaration line, you'd get a compiler error. However, you should have code for the five basic stack methods contained in that interface, with the exact method signatures contained there.
When you declare a variable final
,
you can't modify it and you can only initialize it when you declare
it (preferred) or once in each constructor. Every DropoutStack is
going to have a capacity that may change with time, and you should
either
call this "capacity" or just refer to it as "stack.length" if, say,
"stack" is the name of your array. The purpose of DEFAULT_CAPACITY
in L&C's code is to have an array size ready if the user wants to
create an object without specifying one. That value wouldn't
change, and since it is the same for every object in the class, I
would have declared it to be public static final
, but
just having it public final
is fine.
Yes, you should start by getting the DropoutStack to work with the five methods push, pop, peek, isEmpty, and size. Only after that should you worry about the resize method and the ProjectThreeStack class -- you will get substantial partial credit for having the more basic functionality.
It's perfectly reasonable to make an assumption
that the capacity (the size of the array) is a positive number -- if
you do, just say so in a comment when you declare the variable. But
if you choose to allow a capacity of 0, here's how a push should
work. In a DropoutStack, the push is exceeding the capacity, so the
definition of the DropoutStack says that the oldest element of the
stack should be discarded with no exception thrown. So you just toss
the element to be pushed, and the push has no effect on the object.
In the case of the ProjectThreeStack, it is supposed to act like an
ordinary stack, so it should resize itself to 1 to fit the new
element. This would require a special case in the code, because
doubling a capacity of 0 would have no effect.
I wouldn't bother with that -- I can actually
imagine the user wanting to resize, say in order to trim the array to
the
size of the stack and thus save memory. We'll assume that the user is
smart
enough to call size
before calling resize
,
so that if they throw away any elements they know that they are doing
it.
I'll put it up later tonight, sorry.
What is the first line of your ProjectThreeStack code?
"public class ProjectThreeStack<T> extends DropoutStack"
It has to be "...extends DropoutStack<T>", so that the compiler knows that "T" has the same meaning in both pieces of code.
The important thing is that you have the correct
behavior
and that your basic operations all run within O(1) time (see the next
question
below). Given that, you can put the elements into the new array
wherever you want, as long as you have Remember that resizing to an
array
smaller than the size of the stack will lead to your dropping some
elements to make it fit -- make sure that you are in fact dropping the
oldest ones.
top
and
bottom
set correctly when you are done.
Making the new bottom always element 0 is a reasonable idea if it
helps you keep things straight, but it is not a requirement.
Along with having the correct input/output behavior, your methods for the five basic stack operations of DropoutStack are required to take O(1) time in the worst case. This means that they cannot take longer on bigger stacks -- if you had a million elements in your stack, they should take roughly the same time as if you had ten. The TA's tell me that they are seeing code in office hours where every element of the stack is moved on a push or a pop -- this is clearly not an O(1) time operation. The whole point of the circular array implementation is that you can carry out a pop, or a push with dropout, by only changing one or two entries in the array and changing the values of top, bottom, and count.
Last modified 10 October 2011