Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Trouble adding exceptions to fixed queue class

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trouble adding exceptions to fixed queue class

    Hello,

    I am working through examples from a book from the library. From the original errors I saw that there were some overridden methods namely, put() and get(). I overlooked this originally because I didn't think this mattered with implemented classes. I added the @Override notation to the code below and I got this output:

    Attempting to store : A -- OK
    Attempting to store : B -- OK
    Attempting to store : C -- OK
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 
    get() in hsqexcdemo.FixedQueue cannot implement get() in hsqexcdemo.ICharQ
    Attempting to store : D -- OK
    Attempting to store : E -- OK
      overridden method does not throw hsqexcdemo.QueueEmptyException
    Attempting to store : F -- OK
    Attempting to store : G -- OK
    Attempting to store : H -- OK
    Attempting to store : I -- OK
    Attempting to store : J -- OK
    Attempting to store : K
    Queue is full. Maximum size is 10
     
    	at hsqexcdemo.FixedQueue.get(HSQExcDemo.java:56)
    	at hsqexcdemo.HSQExcDemo.main(HSQExcDemo.java:94)
    Getting next char: Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)
    Line 56 is the @Override notation at the get() method
    Line 94 is the line "ch = q.get();" within the try structure below main().
    Tell me if I am wrong but, this "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -
    get() in hsqexcdemo.FixedQueue cannot implement get() in hsqexcdemo.ICharQ" means
    it cannot use get() from interface class ICharQ. If this is true, how do I solve the problem?

    Without errors I should get this output:
    Attempting to store : A -- OK
    Attempting to store : B -- OK
    Attempting to store : C -- OK
    Attempting to store : D -- OK
    Attempting to store : E -- OK
    Attempting to store : F -- OK
    Attempting to store : G -- OK
    Attempting to store : H -- OK
    Attempting to store : I -- OK
    Attempting to store : J -- OK
    Attempting to store : K
    Queue is full. Maximum size is 10
     
    Getting next char: A
    Getting next char: B
    Getting next char: C
    Getting next char: D
    Getting next char: E
    Getting next char: F
    Getting next char: G
    Getting next char: H
    Getting next char: I
    Getting next char: J
    Getting next char: 
    Queue is empty.

    Here is the code:
    package hsqexcdemo;
    /**
     *   Add exception handling to the queue classes. 
    */
     
    // An exception for queue-full errors. 
    class QueueFullException extends Exception { 
      int size; 
     
      QueueFullException(int s) { size = s; } 
     
      public String toString() { 
       return "\nQueue is full. Maximum size is " + 
              size; 
      } 
    } 
     
    // An exception for queue-empty errors. 
    class QueueEmptyException extends Exception { 
     
      public String toString() { 
       return "\nQueue is empty."; 
      } 
    }
    // A fixed-size queue class for characters that uses exceptions. 
    class FixedQueue implements ICharQ {     
      private char q[]; // this array holds the queue     
      private int putloc, getloc; // the put and get indices     
     
      // Construct an empty queue given its size.    
      public FixedQueue(int size) {  
        q = new char[size+1]; // allocate memory for queue     
        putloc = getloc = 0;     
      }     
     
      // Put a characer into the queue.
      @Override
      public void put(char ch) 
        throws QueueFullException {     
     
        if(putloc==q.length-1)  
          throw new QueueFullException(q.length-1); 
     
        putloc++;     
        q[putloc] = ch;     
      }     
     
      // Get a character from the queue.
      @Override
      public char get() 
        throws QueueEmptyException {     
     
        if(getloc == putloc)  
          throw new QueueEmptyException(); 
     
        getloc++;     
        return q[getloc];     
      }     
    }
    // Demonstrate the queue exceptions.  
    public class HSQExcDemo {
        public static void main(String[] args) {
     
            FixedQueue q = new FixedQueue(10);     
            char ch;     
            int i;     
     
            try {  
                // overrun the queue 
                for(i=0; i < 11; i++) { 
                    System.out.print("Attempting to store : " + 
                                     (char) ('A' + i)); 
                    q.put((char) ('A' + i));     
                    System.out.println(" -- OK"); 
                } 
                System.out.println(); 
            } 
            catch (QueueFullException exc) { 
                System.out.println(exc); 
            } 
                System.out.println(); 
     
            try { 
                // over-empty the queue 
                for(i=0; i < 11; i++) {      
                    System.out.print("Getting next char: "); 
                    ch = q.get();     
                    System.out.println(ch);     
                } 
            } 
            catch (QueueEmptyException exc) { 
                System.out.println(exc); 
            }
        }
    }

    Here is the implemented class:
    package hsqexcdemo;
     
    // A character queue interface. 
    public interface ICharQ {    
      // Put a characer into the queue.    
      void put(char ch) throws QueueFullException; 
     
      // Get a character from the queue.   
      char get() throws QueueFullException; 
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble adding exceptions to fixed queue class

    The way I find bugs in code is by adding printlns to show the values of the variables as the code executes and to show execution flow. If you know how the program is supposed to execute, then looking at the printed output should show you where the code is going wrong.

    Does it make sense for a get method to throw a QueueFull exception.
    Try changing it to what the error message says it should be: Queue empty
    Last edited by Norm; December 14th, 2011 at 08:09 PM.

  3. #3
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Trouble adding exceptions to fixed queue class

    overridden method does not throw hsqexcdemo.QueueEmptyException
    Means the overridden method is not implemented to throw this kind of exception. Try throwing this exception in your overridden method in case of empty queue.

  4. #4
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Trouble adding exceptions to fixed queue class

    You can throw multiple exceptions from one method, like so:
    public void fooBar throws TooFooeyException, NotFooeyException
    {
     
    }
    which is what the compiler wants you to do, because the base class did it.

    However
    They should extend a base exception class QueueException... this will make many things more convenient especially throwing exceptions:

    QueueException extends Exception
    QueueFullException extends QueueException
    QueueEmptyException extends QueueException

    but that might just be me o.O

  5. #5
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trouble adding exceptions to fixed queue class

    Quote Originally Posted by Norm View Post
    The way I find bugs in code is by adding printlns to show the values of the variables as the code executes and to show execution flow. If you know how the program is supposed to execute, then looking at the printed output should show you where the code is going wrong.

    Does it make sense for a get method to throw a QueueFull exception.
    Try changing it to what the error message says it should be: Queue empty
    I have read all of the comments and plan to get to them, but well start with Norm's. I changed the QueueFullException to QueueEmptyException in implemented ICharQ class. This is a side note but, I also added some print statements following the put() and get() methods of the ICharQ class, the statements bring up an error, which reads as follows "illegal start of type".

    package hsqexcdemo;
    /**
     * A character queue interface.
     * @author Dan Storjohann
     */
     
    // A character queue interface. 
    public interface ICharQ {    
      // Put a characer into the queue.    
      void put(char ch) throws QueueFullException; 
            System.out.println("put()after interface");
      // Get a character from the queue.   
      char get() throws QueueEmptyException; 
            System.out.println("get() after interface");
    }
    I added a print statement after ch=q.get(); in the second try structure after main(). The statement "After get()" doesn't print in the output. I get this error first from the compiler "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - get() in hsqexcdemo.FixedQueue cannot implement get() in hsqexcdemo.ICharQ". This tells me that get() in the FixedQueue class can't implement get() in the ICharQ class, with that said I still don't know what this means in the grand scheme of things. This error also comes up between the letters "I" and "J" in the output with error (posted below). Does this have to do with where the error is occurring? I don't think the error is with put(), because it appears to me that the error is occuring after ch=q.get();, this calls the method get() from FixedQueue, which implements get() from ICharQ. On the way however, the error overridden method does not throw hsqexcdemo.QueueEmptyException
    at hsqexcdemo.FixedQueue.get(HSQExcDemo.java:53)
    at hsqexcdemo.HSQExcDemo.main(HSQExcDemo.java:91)" occurs.
    I do believe I understand this, but do I? If I do, I don't know why get() from ICharQ (the overridden method) does not throw QueueEmptyException. By the way, line 53 is @Override before get() and line 91 is ch=q.get();.

    Here is the majority of the code.
    package hsqexcdemo;
    /**
     *   Add exception handling to the queue classes. 
    */
     
    // An exception for queue-full errors. 
    class QueueFullException extends Exception { 
      int size; 
     
      QueueFullException(int s) { size = s; } 
     
      public String toString() { 
       return "\nQueue is full. Maximum size is " + 
              size; 
      } 
    } 
     
    // An exception for queue-empty errors. 
    class QueueEmptyException extends Exception { 
     
      public String toString() { 
       return "\nQueue is empty."; 
      } 
    }
    // A fixed-size queue class for characters that uses exceptions. 
    class FixedQueue implements ICharQ {     
      private char q[]; // this array holds the queue     
      private int putloc, getloc; // the put and get indices     
     
      // Construct an empty queue given its size.    
      public FixedQueue(int size) {  
        q = new char[size+1]; // allocate memory for queue     
        putloc = getloc = 0;     
      }     
     
      // Put a characer into the queue.
      @Override
      public void put(char ch) 
        throws QueueFullException {     
     
        if(putloc==q.length-1)  
          throw new QueueFullException(q.length-1); 
     
        putloc++;     
        q[putloc] = ch;     
      }     
     
      // Get a character from the queue.
      @Override
      public char get() 
        throws QueueEmptyException {     
     
        if(getloc == putloc)  
          throw new QueueEmptyException(); 
     
        getloc++;     
        return q[getloc];     
      }     
    }
    // Demonstrate the queue exceptions.  
    public class HSQExcDemo {
        public static void main(String[] args) {
     
            FixedQueue q = new FixedQueue(10);     
            char ch;     
            int i;     
     
            try {  
                // overrun the queue 
                for(i=0; i < 11; i++) { 
                    System.out.print("Attempting to store : " + 
                                     (char) ('A' + i)); 
                    q.put((char) ('A' + i));     
                    System.out.println(" -- OK"); 
                } 
                System.out.println(); 
            } 
            catch (QueueFullException exc) { 
                System.out.println(exc); 
            } 
                System.out.println(); 
     
            try { 
                // over-empty the queue 
                for(i=0; i < 11; i++) {      
                    System.out.print("Getting next char: "); 
                    ch = q.get();
                    System.out.println("After get()");
                    System.out.println(ch);     
                } 
            } 
            catch (QueueEmptyException exc) { 
                System.out.println(exc); 
            }
        }
    }

    I get these errors with the output.
    Attempting to store : A -- OK
    Attempting to store : B -- OK
    Attempting to store : C -- OK
    Attempting to store : D -- OK
    Attempting to store : E -- OK
    Attempting to store : F -- OK
    Attempting to store : G -- OK
    Attempting to store : H -- OK
    Attempting to store : I -- OK
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - get() in hsqexcdemo.FixedQueue cannot implement get() in hsqexcdemo.ICharQ
    Attempting to store : J -- OK
    Attempting to store : K
    Queue is full. Maximum size is 10
     
    Getting next char:   overridden method does not throw hsqexcdemo.QueueEmptyException
    	at hsqexcdemo.FixedQueue.get(HSQExcDemo.java:53)
    	at hsqexcdemo.HSQExcDemo.main(HSQExcDemo.java:91)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Trouble adding exceptions to fixed queue class

    error first from the compiler
    "Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - get() in hsqexcdemo.FixedQueue cannot implement get() in hsqexcdemo.ICharQ".
    A runtime exception does not come from the compiler.

    Take the calls to System.out.println() out of the interface definition.

    If you use a compiler with your code, you would get these error messages:
    ExceptionTests.java:16: <identifier> expected
            System.out.println("put()after interface");
                              ^
    ExceptionTests.java:16: illegal start of type
            System.out.println("put()after interface"); 
                               ^
    ExceptionTests.java:19: <identifier> expected
            System.out.println("get() after interface");
                              ^
    ExceptionTests.java:19: illegal start of type
            System.out.println("get() after interface");
                               ^
    4 errors
     
    4 error(s)

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. Adding a Panel from a different class
    By DudeJericho in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 25th, 2011, 07:28 AM
  3. Replies: 3
    Last Post: April 13th, 2011, 03:30 PM
  4. Adding fixed size picture and button to panel
    By Javabeginner in forum AWT / Java Swing
    Replies: 10
    Last Post: August 23rd, 2010, 06:07 PM
  5. I'm having trouble with date and calendar class
    By kiph in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 02:56 AM