Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.util.Scanner;
- public class stackAttempt {
- Scanner sc = new Scanner(System.in);
- int stackSize = 6;
- int[] theStack = new int[stackSize];
- int topStack = 0;
- int pushNo = 0;
- boolean completed = false;
- String response = "";
- String ask() {
- System.out.println("Would you like to push, pop, or finish?");
- this.response = sc.nextLine();
- return this.response;
- // if (this.response.equals("push")) {
- // System.out.println("What value would you like to push?");
- // this.pushNo = sc.nextInt();
- // this.push(pushNo);
- // }
- // else if (this.response.equals("pop")) {
- // this.pop();
- // }
- // else if (this.response.equals("finish")) {
- // this.completed = true;
- // }
- // // else {
- // //
- // // }
- // else {
- // System.out.println("That didn't make sense mate. I'm gonna show you your stack and we'll try again, yeah?");
- // }
- }
- boolean completeCheck() {
- return this.completed;
- }
- void printStack() {
- System.out.println("The array is:");
- for (int i=0; i<this.topStack; i++) {
- System.out.printf("%s ", this.theStack[i]);
- }
- System.out.println();
- }
- int askPush() {
- System.out.println("What value would you like to push?");
- this.pushNo = sc.nextInt();
- return this.pushNo;
- }
- void push(int n) {
- if (this.topStack < this.stackSize) {
- this.theStack[topStack] = n;
- this.topStack++;
- }
- else {
- System.out.println("The stack is a bit too full at the moment. Thank you, come again");
- }
- }
- void pop(){
- if (this.topStack > 0) {
- this.theStack[topStack-1] = 0;
- this.topStack--;
- System.out.println("The height of the stack is " + this.topStack);
- }
- else {
- System.out.println("Sorry it seems there is nothing to pop off the stack. Thank you, come again");
- }
- }
- public static void main(String[] args) {
- stackAttempt test = new stackAttempt();
- System.out.println("The main is working");
- int pushNo = 0;
- // Scanner sc = new Scanner(System.in);
- boolean completed = false;
- do {
- // System.out.println("The while loop is working");
- // test.ask();
- //
- String action = test.ask();
- // System.out.println("The action requested is: " + action);
- //
- if (action.equals("push")) {
- // System.out.println("What value would you like to push?");
- pushNo = test.askPush();
- test.push(pushNo);
- }
- // else if (action.equals("pop")) {
- // test.pop();
- // }
- else if (action.equals("finish")) {
- completed = true;
- }
- // completed = test.completeCheck();
- test.printStack();
- // completed = true;
- } while (completed == false);
- }
- }
Add Comment
Please, Sign In to add comment