Here is the full code, this program is supposed to work since I copied it from the book:
Java: A Beginner's Guide: Herbert Schildt
Class FixedQueue:
// 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]; // allocate memory for queue
putloc = getloc = 0;
}
// Put a character into the queue.
public void put(char ch) throws QueueFullException {
if(putloc==q.length)
throw new QueueFullException(q.length);
q[putloc++] = ch;
}
// Get a character from the queue.
public char get() throws QueueEmptyException {
if(getloc == putloc)
throw new QueueEmptyException();
return q[getloc++];
}
}
Interface ICharQ:
// A character queue interface.
public interface ICharQ {
// Put a character into the queue.
void put (char ch) throws QueueFullException;
// Get a character from the queue.
char get() throws QueueEmptyException;
}
Class QExcDemo:
// Demonstrate the queue exceptions.
class QExcDemo {
public static void main(String args[]) {
FixedQueue q = new FixedQueue(10);
char ch;
int i;
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);
}
}
}
Class QueueEmptyException:
// An exception for queue-empty errors.
public class QueueEmptyException extends Exception {
public String toString() {
return "\nQueue is empty.";
}
}
Class QueueFullException:
// An exception for queue-full errors.
public class QueueFullException extends Exception {
int size;
QueueFullException(int s) { size = s; }
public String toString() {
return "\nQueue is full. Maximum size is " + size;
}
}
When I build the program, why is that there is an error about exceptions?
run: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - get() in dec15_javatutorial.FixedQueue cannot implement get() in dec15_javatutorial.ICharQ overridden method does not throw dec15_javatutorial.QueueEmptyException Getting next char: at dec15_javatutorial.FixedQueue.get(FixedQueue.java:28) at dec15_javatutorial.QExcDemo.main(QExcDemo.java:33) Java Result: 1
Thanks