Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. /**
  2. * @Title Week 7 Discussion
  3. * @author Adam Hall
  4. */
  5. package week7discussion;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. /**
  11. *
  12. * @author adamchall
  13. */
  14. //Stack is first in last out
  15. class ObjectStack {
  16.  
  17. private Object[] myStack;
  18. private int top;
  19.  
  20. public ObjectStack(int maxSize) {
  21. myStack = new Object[maxSize];
  22. top = -1;
  23. }
  24.  
  25. void Push(Object input) {
  26. myStack[++top] = input;
  27. }
  28.  
  29. Object Pop() {
  30. return myStack[top--];
  31. }
  32.  
  33. boolean isEmpty() {
  34. return (top == -1);
  35. }
  36.  
  37. }
  38.  
  39. class GenericStack<T> {
  40.  
  41. private List<T> myStack;
  42.  
  43. public GenericStack() {
  44. myStack = new ArrayList<>();
  45. }
  46.  
  47. void Push(T input) {
  48. myStack.add(input);
  49. }
  50.  
  51. T Pop() {
  52. T returnValue;
  53.  
  54. if (myStack.isEmpty() == true) {
  55. return null;
  56. }
  57.  
  58. returnValue = myStack.get(myStack.size() - 1);
  59. myStack.remove(myStack.size() - 1);
  60. return returnValue;
  61.  
  62. }
  63.  
  64. T Peek() {
  65. return myStack.get(myStack.size() - 1);
  66.  
  67. }
  68.  
  69. boolean isEmpty() {
  70. return myStack.isEmpty();
  71. }
  72.  
  73. }
  74.  
  75. public class Week7Discussion {
  76.  
  77. /**
  78. * @param args the command line arguments
  79. */
  80. public static void main(String[] args) {
  81. System.out.println("About to try Generic Stack");
  82.  
  83. GenericStack<Double> myDoubleStack = new GenericStack<>();
  84.  
  85. myDoubleStack.Push(42.0);
  86.  
  87. // Comment this next line out in order to compile, notice it does not compile
  88. // Because the top is spotted as being not valid at compile time
  89. // myDoubleStack.Push("Here is a string!"); // This error is caught at compile time
  90. myDoubleStack.Push(888.8);
  91.  
  92. while (!myDoubleStack.isEmpty()) {
  93. Object objectValue;
  94. objectValue = myDoubleStack.Pop();
  95. System.out.println((double) objectValue); // This is not flagged
  96. }
  97.  
  98. System.out.println("About to try ObjectStack");
  99.  
  100. ObjectStack myObjectStack = new ObjectStack(10);
  101.  
  102. myObjectStack.Push(42.0);
  103. // Comment this line out to remove RUNTIME error
  104. myObjectStack.Push("Here is a string!");
  105. myObjectStack.Push(888.8);
  106.  
  107. try {
  108. while (!myObjectStack.isEmpty()) {
  109. Object objectValue;
  110. objectValue = myObjectStack.Pop();
  111. System.out.println((double) objectValue); // This is not flagged
  112. }
  113.  
  114. } catch (ClassCastException exception) {
  115. System.out.println(exception);
  116. System.out.println("This exception points to a programming error that does not show until runtime!");
  117. }
  118.  
  119. }
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement