0

our prof gave us to do implementation of Queue. I wrote the code, but I stuck with the checks (in the CATCH) it's showing me an error:

Error:(80, 18) java: cannot find symbol symbol: class EmptyQueueException location: class TestQueue

Error:(82, 18) java: cannot find symbol symbol: class FullQueueException location: class TestQueue

The Prof gave us this part of code:

public class TestQueue {
public static void main(String[] args) {
    ArrayQueue Q = new ArrayQueue(3);
    try{
        Q.enqueue(1);
        Q.enqueue(2);
        Q.enqueue(3);
        // Q.enqueue(4); its full
        System.out.println(Q.dequeue());
        Q.enqueue(4);
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        Q.enqueue(5);
        System.out.println(Q.dequeue());
        Q.enqueue(6);
        Q.enqueue(7);
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        // System.out.println(Q.dequeue()); its empty
    }
    catch (EmptyQueueException e) {
        System.out.println("it's empty");
    }
    catch (FullQueueException f) {
        System.out.println("it's full");

I added my code:

public class TestQueue {

int sizeQueue;
int numberOfItems = 0;
int headQ = 0;//Front
int tailQ = 0;//Rear
int queuePost[];


TestQueue(int size) {
    sizeQueue = size;
    queuePost = new int[sizeQueue];
}

public boolean FullQueueException(){
    if(numberOfItems > sizeQueue) {
        return true;
    }
    return false;
}

public boolean EmptyQueueException() {
    if(numberOfItems == 0) {
        return true;
    }

    return false;
}



public void enqueue(int input) {
    if (numberOfItems + 1 <= sizeQueue) {
        queuePost[headQ] = input;
        headQ++;
        numberOfItems++;
    } else {
        System.out.println("full");
    }
}

public Integer dequeue() {
    if (numberOfItems > 0) {
        queuePost[headQ] = -1;
        headQ++;
        numberOfItems--;
    } else {
        System.out.println("empty");
    }
    return numberOfItems;
}

public static void main(String[] args) {
    TestQueue Q = new TestQueue(3);
    try {
        Q.enqueue(1);
        Q.enqueue(2);
        Q.enqueue(3);
        // Q.enqueue(4); is full!
        System.out.println(Q.dequeue());
        Q.enqueue(4);
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        Q.enqueue(5);
        System.out.println(Q.dequeue());
        Q.enqueue(6);
        Q.enqueue(7);
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        System.out.println(Q.dequeue());
        // System.out.println(Q.dequeue()); is empty!
    } catch (EmptyQueueException e) {
        System.out.println("it's empty");
    } catch (FullQueueException f) {
        System.out.println("it's full");
    }

}

}

how to resolve this, it's an exception I know, but why it's not working for me :(

Thank you guys.

Kukuriku
  • 13
  • 5
  • 1
    `public boolean FullQueueException()` is a method declaration and not how you create your own custom exceptions. – OH GOD SPIDERS Mar 20 '18 at 14:23
  • Replace `EmptyQueueException` and `FullQueueException` with an `Exception`. Till you learn how to custom create exceptions. – Skynet Mar 20 '18 at 14:24
  • Thank you guys, but i think my prof wants to make like this way , I mean by this he takes ours JAVA and run it on his Main (that actually test all the possibilities) – Kukuriku Mar 20 '18 at 15:03
  • @Kukuriku I really don't know what you mean with that last comment. You simply did not create any Exception and therefor cannot catch them. You need to create your custom Exceptions as own classes that extend the Exception class and then throw them at the apropriate place in the code. You just defined 2 methods and called them Exception. That's not how Exceptions work at all. And all this is completly unrelated to how you run your program. It would Probably be best if you would read a Tutorial about how java exceptions work before you continue with your programming assignment. – OH GOD SPIDERS Mar 20 '18 at 16:06

0 Answers0