-1

While studying how to use synchronized buffers, I needed to use the Buffer interface in order to complete the assignment. I'm pretty sure my entire code is correct with the exception of how I implemented the buffer. I got an error code on my public class saying "Interface expected here." The hints are telling me to extend Buffer instead of implement it. Does anyone have any ideas as to what I am missing? (This is 1 out of 4 classes).

package synchronizedbuffer;

public class SynchronizedBuffer implements Buffer {


private int buffer = -1;
private boolean occupied = false;

public synchronized void put(int value) throws InterruptedException {

    while(occupied) {
        System.out.println("Producer tries to write.");
        displayState("Buffer full. Producer waits");
        wait();
    }

    buffer = value;
    occupied = true;
    displayState("Producer writes " + buffer);
    notifyAll();

}

public synchronized int get() throws InterruptedException { 

    while(!occupied) {
        System.out.println("Consumer tries to read");
        displayState("Buffer empty. Consumer waits");
        wait();
    }

    occupied = false;
    displayState("Consumer reads" + buffer);
    notifyAll();
    return buffer;
}

private synchronized void displayState(String operation) {
    System.out.printf("%-40s%d\t\t%b%n%n", operation, buffer, occupied);
    }  
}

EDIT:: I editted the code to remove the import java.nio.Buffer;

Cigaro
  • 51
  • 1
  • 7
  • 1
    Eh? `java.nio.Buffer` is a class, not an interface. Why do you think you need to write your own? – user207421 Nov 13 '15 at 19:05
  • 1
    @EJP And the `Buffer` constructor is package-private, so you **can't** implement your own. OP must be misinterpreting some part of the homework assignment. – Andreas Nov 13 '15 at 19:12
  • I feel like I am. I used Java.nio.Buffer; because the IDE hinted that it was what I needed to fix the original error. I Originally got the error " cannot find symbol symbol: class Buffer missing javadoc" I added the java.nio.Buffer and got the error I explained above. – Cigaro Nov 13 '15 at 19:25
  • I suspect a Buffer interface was provided with the assignment. Otherwise you need to seek clarification. – user207421 Nov 13 '15 at 22:49
  • After your edit, what is your question now? – user207421 Nov 15 '15 at 00:19
  • The question remains: how do I debug the line where I declare the class and implement the buffer. The error message: Cannot find Symbol; Symbol: Class Buffer. I have emailed the professor and I will post a resolution as soon as I find one. – Cigaro Nov 16 '15 at 12:48

1 Answers1

0

I now understand the issue. The last time I implemented an interface, the interface itself had been imported from another file. I didnt know that I needed to actually write the interface out. I thought it might have been included in Java frameworks like Lists, Sets, Maps, etc...

Anywho I needed to create a Buffer.java:

public interface Buffer {
    public void put(int value) throws InterruptedException;

    public int get() throws InterruptedException; }

@EJP and Andres. Thanks for posting comments.

Cigaro
  • 51
  • 1
  • 7