fork download
  1. class Stack {
  2. int[] stackElements; // the array object
  3. int currentSize = 0; // maximum size of the queue
  4.  
  5. Stack(int size) {
  6. // create the array object
  7. stackElements = new int [size] ;
  8. }
  9.  
  10. void push(int value) {
  11. // try to put a value on the top of the stack
  12. // if the stack is full, print an error message
  13. // if the push operation is successful, print a success message
  14. if (currentSize==stackElements.length-1){
  15. System.out.println("stack is full");
  16. }else
  17. stackElements[currentSize]=value;
  18. System.out.println("push sucesed :"+value);
  19.  
  20. }
  21.  
  22. int pop() {
  23. // try to retrieve a value from the top of the stack
  24. // if the stack is empty, print an error message and return zero
  25. // if the pop operation is successful, print a success message and
  26. // the value of the top element is returned
  27. if (currentSize==0){
  28. System.out.println("stack is vacancy");
  29. return 0;
  30. }else
  31. System.out.println("pop sucesed ");
  32. return (stackElements[currentSize--]);
  33. }
  34.  
  35. }
  36.  
  37.  
  38. public class StackApp {
  39.  
  40. public static void main(String[] args) {
  41. Stack aStack = new Stack(4);
  42.  
  43. aStack.push(9);
  44. aStack.push(28);
  45. aStack.push(47);
  46.  
  47. System.out.println(aStack.pop());
  48.  
  49. aStack.push(-56);
  50. aStack.push(742);
  51. aStack.push(-99);
  52.  
  53. System.out.println(aStack.pop());
  54. System.out.println(aStack.pop());
  55.  
  56. aStack.push(-8);
  57.  
  58. System.out.println(aStack.pop());
  59. System.out.println(aStack.pop());
  60. System.out.println(aStack.pop());
  61. System.out.println(aStack.pop());
  62.  
  63. aStack.push(44);
  64. aStack.push(81);
  65. aStack.push(-7);
  66. aStack.push(-106);
  67.  
  68. System.out.println(aStack.pop());
  69. System.out.println(aStack.pop());
  70. System.out.println(aStack.pop());
  71. System.out.println(aStack.pop());
  72. System.out.println(aStack.pop());
  73. }
  74.  
  75. }
  76.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:38: error: class StackApp is public, should be declared in a file named StackApp.java
public class StackApp {
       ^
1 error
stdout
Standard output is empty