Important programs by Professor, includes:
-StackException. Java = I believe it is a error thing for stacks.
-StackInterface.java = I believe this is the constructor class for the stack programs... maybe not the stacks.java program makes more sense but Ill include this way because you might understand it more than I do.
-Node.java = I believe this is the constructor class of Node programs
-Stack.java = this is a constructor program and implementation program * I believe in part 1 this is the Stack ADT program they were referring to...
-JCFStack.java = for part 2
//////////////////////////////////////////////////////////////////////
//StackException.java constructor program
public class StackException extends RuntimeException {
public StackException(String s) {
super(s);
} // end constructor
} // end StackException
/////////////////////////////////////////////////////////////
//StackInterface.java Constructor Program
public interface StackInterface {
public boolean isEmpty();
// Determines whether the stack is empty.
// Precondition: None.
// Postcondition: Returns true if the stack is empty;
// otherwise returns false.
public void popAll();
// Removes all the items from the stack.
// Precondition: None.
// PostCondition: Stack is empty.
public void push(Object newItem) throws StackException;
// Adds an item to the top of a stack.
// Precondition: newItem is the item to be added.
// Postcondition: If insertion is successful, newItem
// is on the top of the stack.
// Exception: Some implementations may throw
// StackException when newItem cannot be placed on
// the stack.
public Object pop() throws StackException;
// Removes the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty, the item
// that was added most recently is removed from the
// stack and returned.
// Exception: Throws StackException if the stack is
// empty.
public Object peek() throws StackException;
// Retrieves the top of a stack.
// Precondition: None.
// Postcondition: If the stack is not empty, the item
// that was added most recently is returned. The
// stack is unchanged.
// Exception: Throws StackException if the stack is
// empty.
} // end StackInterface
//////////////////////////////////////////////////////////////
//Node.java constructor program
public class Node {
private Object item;
private Node next;
public Node(Object newItem) {
item = newItem;
next = null;
} // end constructor
public Node(Object newItem, Node nextNode) {
item = newItem;
next = nextNode;
} // end constructor
public void setItem(Object newItem) {
item = newItem;
} // end setItem
public Object getItem() {
return item;
} // end getItem
public void setNext(Node nextNode) {
next = nextNode;
} // end setNext
public Node getNext() {
return next;
} // end getNext
} // end class Node
///////////////////////////////////////////////////////////////////////
//Stack.java
public class Stack {
private Node top;
public Stack() {
top = null;
} // end default constructor
public boolean isEmpty() {
return top == null;
} // end isEmpty
public void push(Object newItem) {
top = new Node(newItem, top);
} // end push
public Object pop() throws StackException {
if (!isEmpty()) {
Node temp = top;
top = top.getNext();
return temp.getItem();
}
else {
throw new StackException("StackException on " +
"pop: stack empty");
} // end if
} // end pop
public void popAll() {
top = null;
} // end popAll
public Object top() throws StackException {
if (!isEmpty()) {
return top.getItem();
}
else {
throw new StackException("StackException on " +
"peek: stack empty");
} // end if
} // end top
public Object clone() throws CloneNotSupportedException
{
Stack copy = new Stack();
Node curr = top, prev = null;
while (curr != null)
{
Node temp = new Node(curr.getItem());
if (prev == null)
copy.top = temp;
else
prev.setNext(temp);
prev = temp;
curr = curr.getNext();
}
return copy;
}
// Your findMax instance method goes HERE
} // end Stack
//////////////////////////////////////////////////////////////////////
// JCFStack.java program
public class JCFStack
{
public static void main(String[] args) throws CloneNotSupportedException
{
Stack stack1 = new Stack();
stack1.push(new Integer(27));
stack1.push(new Integer(0));
stack1.push(new Integer(-3));
stack1.push(new Integer(-18));
stack1.push(new Integer(99));
printStack (stack1);
}
private static void printStack (Stack s) throws CloneNotSupportedException
{
Stack tempStack = (Stack) (s.clone());
if (! tempStack.isEmpty())
System.out.println("*** Printing Out Stack: ");
while (! tempStack.isEmpty())
{
System.out.println(tempStack.peek());
tempStack.pop();
}
}
}