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
初学者指南:Herbert Schildt
Class FixedQueue:
类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:
接口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:
类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:
类QueueEmptyException:
// An exception for queue-empty errors.
public class QueueEmptyException extends Exception {
public String toString() {
return "\nQueue is empty.";
}
}
Class QueueFullException:
类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
运行:线程“main”java.lang中的异常。RuntimeException:无法编译的源代码——在dec15_javatu中得到()。FixedQueue不能实现dec15_javatu心目中的get()。ICharQ重写方法不会抛出dec15_javatu心目中的方法。QueueEmptyException正在获取下一个char:在dec15_javatual.fixedqueue .get(FixedQueue.java:28)位于dec15_javatutorial.QExcDemo.main(QExcDemo.java:33) Java结果:1
Thanks
谢谢
1 个解决方案
#1
3
I've copied all of your classes into a project and run it. It compiles and runs.
我已经将你所有的类复制到一个项目中并运行它。它编译和运行。
The output:
输出:
Getting next char:
Queue is empty.
My guess is either you've changed something in the FixedQueue and forgot to save or you imported something incorrectly.
我的猜测是,要么您在FixedQueue中更改了一些内容,忘记保存,要么您导入了一些错误的内容。
#1
3
I've copied all of your classes into a project and run it. It compiles and runs.
我已经将你所有的类复制到一个项目中并运行它。它编译和运行。
The output:
输出:
Getting next char:
Queue is empty.
My guess is either you've changed something in the FixedQueue and forgot to save or you imported something incorrectly.
我的猜测是,要么您在FixedQueue中更改了一些内容,忘记保存,要么您导入了一些错误的内容。