1

I'm learning java, I'm supposed to add an exception handler to a class for a fixed queue. It seems the interface needs to be changed but I'm not sure how.

Code:

//ICharQ.java
package qpack;

public interface ICharQ {
    void put(char ch);

    char get();

    void reset();
}

//QExcDemo.java
package qpack;

class QueueFullException extends Exception {
    int size;

    QueueFullException(int s) { size = s; }

    public String toString() {
        return "\nQueue is full. Max size is " + size;
    }
}

class QueueEmptyException extends Exception {
    public String toString() {
        return "\nQueue is empty.";
    }
}


//Excerpt from IQClasses.java
package qpack;

class FixedQueue implements ICharQ {
        private char q[];
        private int putloc, getloc;

        public FixedQueue(int size) {
                q = new char[size+1];
                putloc = getloc = 0;
        }

        public void put(char ch)
         throws QueueFullException {
                if (putloc == q.length-1)
                        throw new QueueFullException(q.length-1);


                putloc++;
                q[putloc] = ch;
        }

        public char get()
         throws QueueEmptyException {
                if (getloc == putloc)
                        throw new QueueEmptyException();

                getloc++;
                return q[getloc];
        }

        public void reset() {
                putloc = getloc = 0;
        }
}

Compiler output...

qpack/IQClasses.java:22: error: get() in FixedQueue cannot implement get() in ICharQ
public char get() 
            ^
   overridden method does not throw QueueEmptyException
qpack/IQClasses.java:12: error: put(char) in FixedQueue cannot implement put(char) in ICharQ
public void put(char ch) 
            ^
   overridden method does not throw QueueFullException

2 errors

2 Answers 2

3

In FixedQueue you have the checked exceptions.

    public void put(char ch)
     throws QueueFullException {

    public char get()
     throws QueueEmptyException {

This means your interface for these method has to have the same "throws".

BTW I would make QueueFullException and QueueEmptyException extend IllegalStateException which is not a check exception, but I would still add it to the throws clause in your interface.

For comparison, you could look at the Queue and I would follow the naming and exceptions it throws as much as possible.

I would consider turning your FixedBuffer into a Ring Buffer also known as a Circular Buffer This way your queue won't run out of space just because it reaches the end.


This is how I would set out the interface by basing it on Queue.

public interface CharQueue {

    /**
     * Inserts the specified element into this queue if it is possible to do so
     * immediately without violating capacity restrictions, returning
     * <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
     * if no space is currently available.
     *
     * @param ch the char to add
     * @return <tt>true</tt> (as specified by {@link Collection#add})
     * @throws IllegalStateException if the element cannot be added at this
     *         time due to capacity restrictions
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean add(char ch) throws IllegalStateException;

    /**
     * Inserts the specified element into this queue if it is possible to do
     * so immediately without violating capacity restrictions.
     * When using a capacity-restricted queue, this method is generally
     * preferable to {@link #add}, which can fail to insert an element only
     * by throwing an exception.
     *
     * @return <tt>true</tt> if the element was added to this queue, else
     *         <tt>false</tt>
     * @throws IllegalArgumentException if some property of this element
     *         prevents it from being added to this queue
     */
    boolean offer(char ch);

    /**
     * Retrieves and removes the head of this queue.  This method throws an exception if this
     * queue is empty.
     *
     * @return the head of this queue
     * @throws NoSuchElementException if this queue is empty
     */
    char remove() throws NoSuchElementException;

    /**
     * Removes all of the elements from this collection.
     * The collection will be empty after this method returns.
     */
    void clear();
}

This way you can mentally, if not in code, replace Queue<Character> with CharQueue As the documentation notes, offer is preferable to add and you might want to choose one of these depending on your requirements.

Sign up to request clarification or add additional context in comments.

4 Comments

@artbristol I prefer checked buffers, but I am more in favour of following convention, as it means there is less things to remember / gotchas for anyone using the class and Queue.remove() throws an unchecked NoSuchElementException
Well thanks, but I'm doing self-test exercises out of the book, while your improvements are worthy, I'm really just trying to get through the book. But, you've answered my question and now I know how to fix the code. Thanks a heap.
@JohnTate By giving a longer, more in depth answer I assumed you are looking to learn rather than answer one question. ;)
Also that was just an excerpt, there is a circular and dynamic queue class as well I had to implement exceptions into. I got stuck on the first fixed queue.
2

In your FixedQueue class, your method put throws QueueFullException, but that's not specified in your interface ICharQ. Same with get and QueueEmptyException.

You can either:

  • Specify these exceptions in the interface as well
  • OR make both of these exceptions extend RuntimeException rather than Exception

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.