Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: TestStackPT


Message #1 by sam-armen@e... on Fri, 12 Apr 2002 10:26:58
How do I change TestStackPT.java to test both the array and linked 
implementation in one executiion of the program?




import BreezyGUI.*; 

public class TestStackPT{ 
public static void main (String[] args){ 
StackPT s = new ArrayStackPT(10); 
//StackPT s = new LinkedStackPT(); 

try{ 
String str = (String)s.peek(); 
}catch (Exception e){ 
System.out.println("Expect error because peeking when empty:\n" + e); 
} 

for (int i = 0; i < 10; i++ ) 
s.push (new Integer(i)); 

System.out.println ("Expect 987 : "+ s.pop() + s.pop() + s.pop()); 

try{ 
for (int i = 0; i < 4; i ++) 
s.push (new Integer(i)); 
}catch (Exception e){ 
System.out.println("Expect full stack error with ArrayStackPT:\n" + e); 
} 

System.out.print ("Expect 2106543210 for ArrayStackPT,\n" + 
"Expect 32106543210 for LinkedStackPT : "); 

while (! s.isEmpty()) 
System.out.print(s.pop()); 

GBFrame.pause(); 
} 
} 






public interface StackPT { 

public boolean isEmpty(); 

public boolean isFull(); 

public Object peek(); 

public Object pop(); 

public void push(Object item); 

public int size(); 

} 







import java.io.Serializable; 

public class ArrayStackPT implements StackPT, Serializable { 

public final static int DEFAULT_CAPACITY = 100; 
private Object stack[]; // The array that holds the stack 
private int top; // Index of top item on the stack 

// Creates a stack with the default capacity 
public ArrayStackPT (){ 
this(DEFAULT_CAPACITY); 
} 

// Creates a stack with a user-specified capacity 
public ArrayStackPT (int capacity){ 
if (capacity < 1) 
throw new IllegalArgumentException ("Capacity must be > 0"); 
stack = new Object[capacity]; 
top = -1; 
} 

public boolean isEmpty(){ 
return top == -1; 
} 

public boolean isFull(){ 
return size() == stack.length; 
} 

public Object peek(){ 
if (isEmpty()) 
throw new IllegalStateException ("Stack is empty"); 
return stack[top]; 
} 

public Object pop(){ 
if (isEmpty()) 
throw new IllegalStateException ("Stack is empty"); 
Object topItem = stack[top]; 
stack[top] = null; 
top--; 
return topItem; 
} 

public void push(Object item){ 
if (item == null) 
throw new IllegalArgumentException ("Item is null"); 
if (isFull()) 
throw new IllegalStateException ("Stack is full"); 
top++; 
stack[top] = item; 
} 

public int size(){ 
return top + 1; 
} 
} 






import java.io.Serializable; 

public class LinkedStackPT implements StackPT, Serializable { 

private int count; // Number of items in the stack 
private Node top; // Top node in the stack 

public LinkedStackPT(){ 
top = null; 
count = 0; 
} 

public boolean isEmpty(){ 
return count == 0; 
} 

public boolean isFull(){ 
return false; 
} 

public Object peek(){ 
if (isEmpty()) 
throw new IllegalStateException ("Stack is empty"); 
return top.value; 
} 

public Object pop(){ 
if (isEmpty()) 
throw new IllegalStateException ("Stack is empty"); 
count--; 
Object item = top.value; 
top = top.next; 
return item; 
} 

public void push(Object item){ 
if (item == null) 
throw new IllegalArgumentException ("Item is null"); 
count++; 
Node n = new Node (item, top); 
top = n; 
} 

public int size(){ 
return count; 
} 

private class Node implements Serializable { 

private Object value; // Value stored in this node 
private Node next; // Reference to next node 

private Node(){ 
value = null; 
next = null; 
} 

private Node(Object value, Node next){ 
this.value = value; 
this.next =next; 
} 
} 
}



  Return to Index