我正在学习java,我应该为一个固定队列的类添加一个异常处理程序。似乎需要更改界面,但我不确定如何更改。
代码:
//ICharQ.java
package qpack;
public interface ICharQ {
void put(char ch);
char get();
void reset();
}
//QExcDemo.java
package qpack;
class QueueFullException extends Exception {
int size;
QueueFullException(int s) { size = s; }
public String toString() {
return "\nQueue is full. Max size is " + size;
}
}
class QueueEmptyException extends Exception {
public String toString() {
return "\nQueue is empty.";
}
}
//Excerpt from IQClasses.java
package qpack;
class FixedQueue implements ICharQ {
private char q[];
private int putloc, getloc;
public FixedQueue(int size) {
q = new char[size+1];
putloc = getloc = 0;
}
public void put(char ch)
throws QueueFullException {
if (putloc == q.length-1)
throw new QueueFullException(q.length-1);
putloc++;
q[putloc] = ch;
}
public char get()
throws QueueEmptyException {
if (getloc == putloc)
throw new QueueEmptyException();
getloc++;
return q[getloc];
}
public void reset() {
putloc = getloc = 0;
}
}
编译器输出...
qpack/IQClasses.java:22: error: get() in FixedQueue cannot implement get() in ICharQ
public char get()
^
overridden method does not throw QueueEmptyException
qpack/IQClasses.java:12: error: put(char) in FixedQueue cannot implement put(char) in ICharQ
public void put(char ch)
^
overridden method does not throw QueueFullException
2 个错误