-1

我创建了两个文件,一个文件的界面如下:

       public interface ICharQ {
       void put(char ch);
       char get();
       }

和实现类如下:

    //A fixed size Queue class for characters

    class FixedQueue implements ICharQ{
    private char q[]; //This queue holds the characters
    private int putloc, getloc; //the put and get indices

    public FixedQueue(int size){
    q = new char[size+1]; //allocate memory for the queue
    putloc = getloc = 0;
}

    //Put a character into the queue
    public void put(char ch){
    if(putloc == q.length - 1){
    System.out.println(" - Queue is full");
    return;
    }
    putloc++;
    q[putloc] = ch;
}

    //Get a character from the Queue
    public char get(){
    if(getloc == putloc){
    System.out.println(" - Queue is empty");
    return (char) 0;
    }
    getloc++;
    return q[getloc];
}
}

    //A circular Queue
    class CircularQueue implements ICharQ{
    private char q[]; //This queue holds the characters
    private int putloc, getloc; //the put and get indices

    public CircularQueue(int size){
    q = new char[size+1]; //allocate memory for the queue
    putloc = getloc = 0;
}

    //Put a character into the queue
    public void put(char ch){
    /*Queue is full if putloc is one less than getloc or if putloc is at the    end and getloc is at the beginning */
    if((putloc + 1 == getloc)|(putloc == q.length-1 & getloc == 0)){
    System.out.println(" - Queue is full");
    return;
    }
    putloc++;
    if(putloc == q.length) putloc = 0; //reset loop
    q[putloc] = ch;
}

    //Get character from the Queue
    public char get(){
    if(getloc == putloc){
    System.out.println(" - Queue is empty");
    return (char) 0;
    }
    getloc++;
    if(getloc == q.length) getloc = 0;
    return q[getloc];
}
}


    //A dynamic Queue
    class DynQueue 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 DynQueue(int size) {
    q = new char[size+1]; // allocate memory for queue
    putloc = getloc = 0;
}

    //put a character into the queue
    public void put(char ch){
    if(putloc == q.length-1){
    //increase queue size
    char t[] = new char[q.length * 2];

    //copy elements into the new queue
    for(int i=0; i<q.length; i++)
    t[i] = q[i];
    q = t;
    }
    putloc++;
    q[putloc] = ch;
}

// Get a character from the queue.
    public char get() {
    if(getloc == putloc) {
    System.out.println(" – Queue is empty.");
    return (char) 0;
    }
    getloc++;
    return q[getloc];
}
    }

    //Demonstrate the ICharQ interface
    class IQDemo{
    public static void main(String args[]){
    FixedQueue q1 = new FixedQueue(10);
    DynQueue q2 = new DynQueue(5);
    CircularQueue q3 = new CircularQueue(10);

    ICharQ iQ;
    char ch;
    int i;

    iQ = q1;
    //Put some characters into the fixed queue
    for(i = 0; i<10; i++){
    iQ.put((char)('A' + i));
    }
    //Show the queue
    for(i=0; i<10; i++){
    System.out.print("Contents of fixed queue: ");
    for(i=0; i < 10; i++) {
    ch = iQ.get();
    System.out.print(ch);
    }
    }
    System.out.println();
    iQ = q2;
    // Put some characters into dynamic queue.
    for(i=0; i < 10; i++)
    iQ.put((char) ('Z' - i));
    // Show the queue.
    System.out.print("Contents of dynamic queue: ");
    for(i=0; i < 10; i++) {
    ch = iQ.get();
    System.out.print(ch);
    }
    System.out.println();
    iQ = q3;
    // Put some characters into circular queue.
    for(i=0; i < 10; i++)
    iQ.put((char) ('A' + i));
    // Show the queue.
    System.out.print("Contents of circular queue: ");
    for(i=0; i < 10; i++) {
    ch = iQ.get();
    System.out.print(ch);
    }
    System.out.println();

    // Put more characters into circular queue.
    for(i=10; i < 20; i++)
    iQ.put((char) ('A' + i));
    // Show the queue.
    System.out.print("Contents of circular queue: ");
    for(i=0; i < 10; i++) {
    ch = iQ.get();
    System.out.print(ch);
    }
    System.out.println("\nStore and consume from" +
    " circular queue.");
    // Use and consume from circular queue.
    for(i=0; i < 20; i++) {
    iQ.put((char) ('A' + i));
    ch = iQ.get();
    System.out.print(ch);
    }
}
}

现在这些文件位于同一目录中。ICharQ.java 文件编译得很好。但是当我试图编译 IQDemo.java 时,它会给出错误,指出它找不到 ICharQ。

但是,当我从接口文件中删除公共访问器并将代码包含在 IQDemo 中时,它可以完美运行。但是,当我将其更改为公开时​​,它并没有编译。

代码有问题还是我错过了什么?请帮忙。

4

1 回答 1

0

这听起来像是路径和包的问题。检查您的类路径设置(cp命令行选项),确保在每个文件的顶部声明包,并且这些文件相应地放置在文件系统中。

您应该有类似package YourNick.YourProject;两个文件的第一行的内容。然后你使用它们编译它们javac -cp $CP $CP/YourNick/YourProject/{ICharQ,IQDemo}.java,生成的字节码文件(即ICharQ.classand IQDemo.class)应该在$CP/YourNick/YourProject. 然后你执行java命令为java -cp $CP YourNick.YourProject.IQDemo.

使用 IDE 时,项目的类路径设置和目录结构会以某种方式自动处理。您仍然必须选择一个包名称。

因此,这应该是结果(显示为 shell 会话):

$ cd /home/chatterjee/java-projects
$ ls Queue
ICharQ.java    IQDemo.java
$ cat Queue/ICharQ.java
package Queue;
public interface ICharQ {
    void put(char ch);
    char get();
}
$ cat Queue/IQDemo.java
package Queue;
… lots of code …
$ javac -cp . ./Queue/{ICharQ,IQDemo}.java
$ ls Queue
ICharQ.class    ICharQ.java    IQDemo.class    IQDemo.java
$ java -cp . Queue.IQDemo
… desired output …
于 2013-09-19T11:44:53.250 回答