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:
Line 56 is the @Override notation at the get() methodAttempting 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 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; }