0

Hi there I'm trying to implement the stack methods push(x) and pop() with two queue's. I was able to test out my methods using print statements in the methods themselves so I know the methods are working properly, but I know that's bad coding practice. I've looked all over about issues regarding the strange output (@15db9742) when I try to print the stack during the for loop in main. All of the solutions I've seen are directed at classes that use strings, so they just say to override the toString() method to print out what you need, but my push(x) method doesn't return anything, and if I choose to return int it obviously doesn't work. The other solutions I've seen are trying Arrays.toString(), but these aren't arrays either. The last solution I've looked into is implementing Iterator, but I can't seem to extrapolate the way the examples implementation is used to my own.

public class Assignment1Question1B {

    Queue q1 = new LinkedList();
    Queue q2 = new LinkedList();
    int front;
    int n;

    public class Node {
        int x;
        Node next;
    }

    public void push(int x) {
        q1.add(x);
        front = x;
        n++;
    }

    public void pop() {
        if (q1.size() == 0){
            System.out.println("Stack is already empty.");
        } else {
            while (q1.size() != 1) {
                q2.add(q1.remove());
            }
            q1.remove();
            Queue empty = q1;
            q1 = q2;
            q2 = empty;
            n--;
        }
    }

    public static void main(String[] args) {
        Assignment1Question1B stack = new Assignment1Question1B();
        for (int i = 0; i < 50; i++) {
            stack.push(i);
            System.out.println(stack);
        }
    }
}
MaxV
  • 3
  • 5
  • You need to write `toString()` for `Assignment1Question1B` class. You don't have that. – clinomaniac Apr 06 '18 at 17:49
  • @clinomaniac Yes I figured as much, but from the examples I have seen, none of the toString() method overrides that people have discussed seem to really help me. They are super short and simple but I can't seem to figure out how to apply them. – MaxV Apr 06 '18 at 18:06
  • Try this: `@Override public String toString() { return q1.toString(); }` – clinomaniac Apr 06 '18 at 18:08
  • @clinomaniac Wow that worked perfectly. So correct me because I want to try and understand how this process works. When I instantiate the Assignment1Question1B class, anytime the object q1 appears, the program automatically checks the toString() method and changes whatever the input is to String form? – MaxV Apr 06 '18 at 18:17
  • Read the answer to the question marked as a duplicate. That has a very detailed explanation. – clinomaniac Apr 06 '18 at 18:18
  • @clinomaniac Ah got it, anytime an object is printed the toString() method is called. Thanks a lot for the help, seeing it applied to my specific scenario still helps a lot. I'd upvote your comment but there's no way to do that haha! – MaxV Apr 06 '18 at 18:26

0 Answers0