1
|
 |
package com.instantiations.example.stack;
|
2
|
|
|
3
|
|
import java.util.EmptyStackException;
|
4
|
|
|
5
|
 |
/**
|
6
|
|
* The class <code>SimpleStack</code> is a simple implementation of a stack
|
7
|
|
* that does not contain any design-by-contract (DBC) information. Other than
|
8
|
|
* the absence of the DBC tags, this class is identical to the class
|
9
|
|
* {@link DBCStack} so that the results of generating test classes can be
|
10
|
|
* compared.
|
11
|
|
*
|
12
|
|
* @author Donna Devon
|
13
|
|
*/
|
14
|
  |
public class SimpleStack
|
15
|
|
{
|
16
|
 |
private Object[] elements;
|
17
|
|
|
18
|
 |
private int top;
|
19
|
|
|
20
|
 |
public SimpleStack(int initialCapacity)
|
21
|
|
{
|
22
|
|
elements = new Object[initialCapacity];
|
23
|
|
}
|
24
|
|
|
25
|
 |
public boolean isEmpty()
|
26
|
|
{
|
27
|
 |
return top == 0;
|
28
|
|
}
|
29
|
|
|
30
|
 |
public void push(Object object)
|
31
|
|
{
|
32
|
|
if (top == elements.length) {
|
33
|
|
grow();
|
34
|
|
}
|
35
|
 |
elements[top++] = object;
|
36
|
|
}
|
37
|
|
|
38
|
 |
public Object peek()
|
39
|
|
{
|
40
|
 |
if (top == 0) {
|
41
|
|
throw new EmptyStackException();
|
42
|
|
}
|
43
|
|
return elements[top - 1];
|
44
|
|
}
|
45
|
|
|
46
|
 |
public Object pop()
|
47
|
|
{
|
48
|
 |
if (top == 0) {
|
49
|
|
throw new EmptyStackException();
|
50
|
|
}
|
51
|
|
return elements[--top];
|
52
|
|
}
|
53
|
|
|
54
|
 |
private void grow()
|
55
|
|
{
|
56
|
  |
int elementCount;
|
57
|
  |
Object[] newElements;
|
58
|
|
|
59
|
|
elementCount = elements.length;
|
60
|
 |
newElements = new Object[elementCount + 10];
|
61
|
|
System.arraycopy(elements, 0, newElements, 0, elementCount);
|
62
|
|
elements = newElements;
|
63
|
|
}
|
64
|
|
}
|