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:
Code Java:
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:
Code Java:
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:
Code Java:
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:
Code Java:
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;
}
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
Re: Trouble adding exceptions to fixed queue class
Quote:
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.
Re: Trouble adding exceptions to fixed queue class
You can throw multiple exceptions from one method, like so:
Code Java:
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
Re: Trouble adding exceptions to fixed queue class
Quote:
Originally Posted by
Norm
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".
Code Java:
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.
Code Java:
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.
Code Java:
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)
Re: Trouble adding exceptions to fixed queue class
Quote:
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:
Code :
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)