Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Stack
- * @version 1.01
- * @author Sam Harris
- */
- public class Stack {
- static int stackSize = 5;
- static int topOfStack = -1;
- static int[] stack = new int[stackSize];
- static boolean errorFree = true;
- /**
- * returns true if the stack is empty and false if not
- * no side effects
- * referentially transparent
- * @return boolean, true if stack is empty
- **/
- public static boolean isEmpty () {
- return topOfStack == -1;
- }
- /**
- * returns true if the stack is full and false if not
- * no side effects
- * referentially transparent
- * @return boolean, true if stack full
- **/
- public static boolean isFull () {
- return topOfStack == stackSize - 1;
- }
- /**
- * Moves the stack pointer to create an empty stack
- * side effect changes errorFree to true
- * referentially transparent
- **/
- public static void empty () {
- errorFree = true;
- topOfStack = -1;
- }
- /**
- * returns the value at the top of the stack
- * no side effects
- * referentially transparent
- * @return int, value of the top of the stack
- **/
- public static int top () {
- errorFree = ! (isEmpty ()) & errorFree;
- if (errorFree) {
- return stack[topOfStack];
- } else {
- return 0;
- }
- }
- /**
- * adds a value to the top of the stack
- * no side effects
- * based on
- * int value
- * referentially transparent
- * @param int, value to be added to stack
- **/
- public static void push (int value) {
- errorFree = ! (isFull ()) & errorFree;
- if (errorFree) {
- topOfStack = topOfStack + 1;
- stack[topOfStack] = value;
- }
- }
- /**
- * removes the value at the top of the stack by moving stack pointer
- * no side effects
- * referentially transparent
- **/
- public static void pop () {
- errorFree = ! (isEmpty ()) & errorFree;
- if (errorFree) {
- topOfStack = topOfStack - 1;
- }
- }
- public static void main (String args[]) {
- int test;
- System.out.println(" --- Begin Experiment 1 ---");
- System.out.println(" --- Empty ---");
- empty ();
- System.out.println("Build up a stack of one entry:");
- push(1);
- System.out.println(" push 1");
- System.out.println("Inspect the stack:");
- test = top();
- System.out.println(" top: " + test);
- System.out.println("Make the stack empty:");
- pop();
- System.out.println(" pop");
- System.out.println("Test how 'top' works on the empty stack:");
- test = top();
- if (errorFree) {
- System.out.println(" top: " + test);
- } else {
- System.out.println(" An error has occured.");
- }
- System.out.println(" --- End Experiment 1 ---");
- System.out.println(" --- Begin Experiment 2 ---");
- System.out.println(" --- Empty ---");
- empty ();
- System.out.println("Build up a stack of five entries:");
- push(1);
- System.out.println(" push 1");
- push(2);
- System.out.println(" push 2");
- push(3);
- System.out.println(" push 3");
- push(4);
- System.out.println(" push 4");
- push(5);
- System.out.println(" push 5");
- System.out.println("Push another entry and check if 'out of memory'-protection works:");
- push(6);
- if (errorFree) {
- System.out.println(" push 6");
- } else {
- System.out.println(" An error has occured.");
- }
- System.out.println(" --- End Experiment 2 ---");
- System.out.println(" --- Begin Experiment 3 ---");
- empty ();
- System.out.println(" --- Empty ---");
- System.out.println("Build up a stack of three entries:");
- push(1);
- System.out.println(" push 1");
- push(2);
- System.out.println(" push 2");
- push(3);
- System.out.println(" push 3");
- System.out.println("Take these three entries away.");
- while (! isEmpty ()) {
- test = top ();
- System.out.println(" top: " + test);
- pop ();
- System.out.println(" pop");
- }
- System.out.println(" --- End Experiment 3 ---");
- System.out.println(" --- Begin Experiment 4 ---");
- System.out.println("Create empty stack");
- empty ();
- push(25);
- System.out.println(" push 25");
- push(31);
- System.out.println(" push 31");
- System.out.println(" pop");
- pop ();
- test = top ();
- System.out.println(" top: " + test);
- System.out.println(" --- End Experiment 4 ---");
- System.out.println(" --- Begin Experiment 5 ---");
- empty ();
- if (isEmpty()) {
- System.out.println("Empty");
- } else {
- System.out.println("not empty");
- }
- push(42);
- System.out.println(" push 42");
- if (isEmpty()) {
- System.out.println("Empty");
- } else {
- System.out.println("not empty");
- }
- System.out.println(" pop");
- pop ();
- if (isEmpty()) {
- System.out.println("Empty");
- } else {
- System.out.println("not empty");
- }
- System.out.println(" --- End Experiment 5 ---");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement