Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1.  
  2. /**
  3. * Stack
  4. * @version 1.01
  5. * @author Sam Harris
  6. */
  7. public class Stack {
  8.  
  9. static int stackSize = 5;
  10.  
  11. static int topOfStack = -1;
  12. static int[] stack = new int[stackSize];
  13. static boolean errorFree = true;
  14.  
  15. /**
  16. * returns true if the stack is empty and false if not
  17. * no side effects
  18. * referentially transparent
  19. * @return boolean, true if stack is empty
  20. **/
  21. public static boolean isEmpty () {
  22. return topOfStack == -1;
  23. }
  24. /**
  25. * returns true if the stack is full and false if not
  26. * no side effects
  27. * referentially transparent
  28. * @return boolean, true if stack full
  29. **/
  30. public static boolean isFull () {
  31. return topOfStack == stackSize - 1;
  32. }
  33.  
  34. /**
  35. * Moves the stack pointer to create an empty stack
  36. * side effect changes errorFree to true
  37. * referentially transparent
  38. **/
  39. public static void empty () {
  40. errorFree = true;
  41. topOfStack = -1;
  42. }
  43. /**
  44. * returns the value at the top of the stack
  45. * no side effects
  46. * referentially transparent
  47. * @return int, value of the top of the stack
  48. **/
  49. public static int top () {
  50. errorFree = ! (isEmpty ()) & errorFree;
  51. if (errorFree) {
  52. return stack[topOfStack];
  53. } else {
  54. return 0;
  55. }
  56. }
  57. /**
  58. * adds a value to the top of the stack
  59. * no side effects
  60. * based on
  61. * int value
  62. * referentially transparent
  63. * @param int, value to be added to stack
  64. **/
  65. public static void push (int value) {
  66. errorFree = ! (isFull ()) & errorFree;
  67. if (errorFree) {
  68. topOfStack = topOfStack + 1;
  69. stack[topOfStack] = value;
  70. }
  71. }
  72. /**
  73. * removes the value at the top of the stack by moving stack pointer
  74. * no side effects
  75. * referentially transparent
  76. **/
  77. public static void pop () {
  78. errorFree = ! (isEmpty ()) & errorFree;
  79. if (errorFree) {
  80. topOfStack = topOfStack - 1;
  81. }
  82. }
  83.  
  84. public static void main (String args[]) {
  85. int test;
  86.  
  87. System.out.println(" --- Begin Experiment 1 ---");
  88.  
  89.  
  90. System.out.println(" --- Empty ---");
  91. empty ();
  92.  
  93. System.out.println("Build up a stack of one entry:");
  94. push(1);
  95. System.out.println(" push 1");
  96.  
  97. System.out.println("Inspect the stack:");
  98. test = top();
  99. System.out.println(" top: " + test);
  100.  
  101. System.out.println("Make the stack empty:");
  102. pop();
  103. System.out.println(" pop");
  104.  
  105. System.out.println("Test how 'top' works on the empty stack:");
  106. test = top();
  107.  
  108. if (errorFree) {
  109. System.out.println(" top: " + test);
  110. } else {
  111. System.out.println(" An error has occured.");
  112. }
  113.  
  114. System.out.println(" --- End Experiment 1 ---");
  115.  
  116. System.out.println(" --- Begin Experiment 2 ---");
  117.  
  118. System.out.println(" --- Empty ---");
  119. empty ();
  120.  
  121. System.out.println("Build up a stack of five entries:");
  122.  
  123. push(1);
  124. System.out.println(" push 1");
  125. push(2);
  126. System.out.println(" push 2");
  127. push(3);
  128. System.out.println(" push 3");
  129. push(4);
  130. System.out.println(" push 4");
  131. push(5);
  132. System.out.println(" push 5");
  133.  
  134. System.out.println("Push another entry and check if 'out of memory'-protection works:");
  135.  
  136. push(6);
  137. if (errorFree) {
  138. System.out.println(" push 6");
  139. } else {
  140. System.out.println(" An error has occured.");
  141. }
  142.  
  143. System.out.println(" --- End Experiment 2 ---");
  144.  
  145.  
  146. System.out.println(" --- Begin Experiment 3 ---");
  147.  
  148. empty ();
  149. System.out.println(" --- Empty ---");
  150.  
  151. System.out.println("Build up a stack of three entries:");
  152.  
  153. push(1);
  154. System.out.println(" push 1");
  155. push(2);
  156. System.out.println(" push 2");
  157. push(3);
  158. System.out.println(" push 3");
  159.  
  160. System.out.println("Take these three entries away.");
  161. while (! isEmpty ()) {
  162. test = top ();
  163. System.out.println(" top: " + test);
  164. pop ();
  165. System.out.println(" pop");
  166. }
  167.  
  168. System.out.println(" --- End Experiment 3 ---");
  169.  
  170. System.out.println(" --- Begin Experiment 4 ---");
  171. System.out.println("Create empty stack");
  172. empty ();
  173. push(25);
  174. System.out.println(" push 25");
  175. push(31);
  176. System.out.println(" push 31");
  177. System.out.println(" pop");
  178. pop ();
  179. test = top ();
  180. System.out.println(" top: " + test);
  181.  
  182. System.out.println(" --- End Experiment 4 ---");
  183.  
  184. System.out.println(" --- Begin Experiment 5 ---");
  185.  
  186. empty ();
  187.  
  188. if (isEmpty()) {
  189. System.out.println("Empty");
  190. } else {
  191. System.out.println("not empty");
  192. }
  193. push(42);
  194. System.out.println(" push 42");
  195. if (isEmpty()) {
  196. System.out.println("Empty");
  197. } else {
  198. System.out.println("not empty");
  199. }
  200. System.out.println(" pop");
  201. pop ();
  202. if (isEmpty()) {
  203. System.out.println("Empty");
  204. } else {
  205. System.out.println("not empty");
  206. }
  207. System.out.println(" --- End Experiment 5 ---");
  208. }
  209.  
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement