|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectscranton.stack.StackViaArray
An implementation of the Stack interface using an array via composition.
A stack S is
|
| Field Summary | |
protected java.lang.Object[] |
actual
This field contains the array that holds the stack, actual[0] holds the bottom, actual[count-1] holds the top. |
protected int |
count
This field contains the count of the number of objects in the stack. |
| Constructor Summary | |
StackViaArray()
Constructs a stack, the client does not specify an intial size. |
|
StackViaArray(int size)
Constructs a stack of a size determined by the client. |
|
| Method Summary | |
java.lang.Object |
clone()
|
private java.lang.Object[] |
doubler(java.lang.Object[] inArray)
Creates an array that is twice the size of the array passed as a parameter, actual, returns an array that is twice the size and the contents of the parameter are placed in the first half of the new array. |
Stack |
factory()
|
boolean |
isEmpty()
Stack does not change, returns true iff the stack is empty. |
java.lang.Object |
pop()
Decrease the size of the stack by one by removing and returning the object at the top. |
void |
push(java.lang.Object item)
Increase the size of the stack by one by placing item as the new top of the stack. |
java.lang.Object |
topOf()
Stack does not change, returns the reference to the top object in the stack. |
java.lang.String |
toString()
Returns a string representation of the stack. |
| Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
protected java.lang.Object[] actual
protected int count
| Constructor Detail |
public StackViaArray()
public StackViaArray(int size)
| Method Detail |
public boolean isEmpty()
|
S'=S Return S==() - maps to count == 0 |
isEmpty in interface Stackpublic java.lang.Object topOf()
|
S!=() - maps to checking if count != 0 S'=S Return an-1 - maps to actual[count-1], the position of the top of the stack |
topOf in interface StackAssertion - When stack is emptyprivate java.lang.Object[] doubler(java.lang.Object[] inArray)
inArray - The current array containing the stack.
public void push(java.lang.Object item)
| S'=(S, obj) - maps to count' == count+1, actual'[count'-1]==item, actual'[0..count-1]==actual[0..count-1] |
push in interface Stackitem - The new top of stackpublic java.lang.Object pop()
|
S!=() - maps to checking if count != 0 S = (S', an-1) - maps to count'=count-1, actual'[0..count'-1]==actual[0..count'-1] Return: an-1 - maps to returning actual[count-1] |
pop in interface StackAssertion - When stack is emptypublic Stack factory()
public java.lang.Object clone()
public java.lang.String toString()
Stack
toString in interface Stack
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||