Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8786347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:35:58+00:00 2026-06-13T21:35:58+00:00

I’m learning java, I’m supposed to add an exception handler to a class for

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T21:35:59+00:00Added an answer on June 13, 2026 at 9:35 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Seemingly simple, but I cannot find anything relevant on the web. What is the
I want to construct a data frame in an Rcpp function, but when I
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.