Guest User

Untitled

a guest
Dec 12th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.74 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.Scanner;
  3.  
  4. public class stackAttempt {
  5.  
  6. Scanner sc = new Scanner(System.in);
  7. int stackSize = 6;
  8. int[] theStack = new int[stackSize];
  9. int topStack = 0;
  10. int pushNo = 0;
  11. boolean completed = false;
  12. String response = "";
  13.  
  14.  
  15. String ask() {
  16. System.out.println("Would you like to push, pop, or finish?");
  17. this.response = sc.nextLine();
  18. return this.response;
  19. // if (this.response.equals("push")) {
  20. // System.out.println("What value would you like to push?");
  21. // this.pushNo = sc.nextInt();
  22. // this.push(pushNo);
  23. // }
  24. // else if (this.response.equals("pop")) {
  25. // this.pop();
  26. // }
  27. // else if (this.response.equals("finish")) {
  28. // this.completed = true;
  29. // }
  30. // // else {
  31. // //
  32. // // }
  33. // else {
  34. // System.out.println("That didn't make sense mate. I'm gonna show you your stack and we'll try again, yeah?");
  35. // }
  36. }
  37.  
  38.  
  39. boolean completeCheck() {
  40. return this.completed;
  41. }
  42.  
  43.  
  44. void printStack() {
  45. System.out.println("The array is:");
  46. for (int i=0; i<this.topStack; i++) {
  47. System.out.printf("%s ", this.theStack[i]);
  48. }
  49. System.out.println();
  50. }
  51.  
  52.  
  53. int askPush() {
  54. System.out.println("What value would you like to push?");
  55. this.pushNo = sc.nextInt();
  56. return this.pushNo;
  57. }
  58.  
  59.  
  60. void push(int n) {
  61. if (this.topStack < this.stackSize) {
  62. this.theStack[topStack] = n;
  63. this.topStack++;
  64. }
  65. else {
  66. System.out.println("The stack is a bit too full at the moment. Thank you, come again");
  67. }
  68. }
  69.  
  70.  
  71. void pop(){
  72. if (this.topStack > 0) {
  73. this.theStack[topStack-1] = 0;
  74. this.topStack--;
  75. System.out.println("The height of the stack is " + this.topStack);
  76. }
  77. else {
  78. System.out.println("Sorry it seems there is nothing to pop off the stack. Thank you, come again");
  79. }
  80. }
  81.  
  82.  
  83. public static void main(String[] args) {
  84. stackAttempt test = new stackAttempt();
  85. System.out.println("The main is working");
  86. int pushNo = 0;
  87. // Scanner sc = new Scanner(System.in);
  88. boolean completed = false;
  89.  
  90. do {
  91.  
  92. // System.out.println("The while loop is working");
  93.  
  94. // test.ask();
  95. //
  96. String action = test.ask();
  97.  
  98. // System.out.println("The action requested is: " + action);
  99. //
  100. if (action.equals("push")) {
  101. // System.out.println("What value would you like to push?");
  102. pushNo = test.askPush();
  103. test.push(pushNo);
  104. }
  105. // else if (action.equals("pop")) {
  106. // test.pop();
  107. // }
  108. else if (action.equals("finish")) {
  109. completed = true;
  110. }
  111.  
  112. // completed = test.completeCheck();
  113.  
  114.  
  115. test.printStack();
  116. // completed = true;
  117.  
  118.  
  119. } while (completed == false);
  120.  
  121. }
  122.  
  123. }
Add Comment
Please, Sign In to add comment