0

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

Raedwald
  • 46,613
  • 43
  • 151
  • 237
vvavepacket
  • 1,882
  • 4
  • 25
  • 38
  • 3
    I get no errors compiling or running this code. What exactly do you do that gives this runtime exception? – nem035 Dec 17 '14 at 07:39
  • Please explain how exactly you are building and running this code (as an edit to the question). Thanks! – reto Dec 17 '14 at 07:40
  • How are you building your code? For the runtime exception suggests that you are encountering this problem when running the code rather than compiling. I suspect you're running against old .class files – beny23 Dec 17 '14 at 07:40
  • Could you please save your all files and recheck it, Your code is working proprly in my system. – Vikas Singh Dec 17 '14 at 07:42
  • no errors running in eclipse. – Jared Dec 17 '14 at 07:42
  • Hello, is all that code in a single .java file? – Francesco Dec 17 '14 at 07:49
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2333285/java-lang-runtimeexception-uncompilable-source-code-what-can-cause-this – Raedwald Dec 17 '14 at 07:54
  • Program is ok. I have checked in netbeans too. You are missing something. Please tell me what is your jdk version and what tool you are using. – Manoj Sharma Dec 17 '14 at 07:56

1 Answers1

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.

Adrian B.
  • 1,592
  • 1
  • 20
  • 38