class Data {}
public class BufferFaulty {
    private int cap, first, last;
    private Sema free, occupied;
    private Data[] a;
    public BufferFaulty(int n) {
        cap = n;
        first = last = 0;
        a = new Data[n];
        free = new Sema(n); 
        occupied = new Sema(0);
    }
// continued...
    public synchronized void produce(Data d) 
                        throws InterruptedException {
        free.down();
        a[last] = d; 
        last = (last+1) % cap;
        occupied.up();
    }
    public synchronized Data consume() 
                        throws InterruptedException {
        occupied.down();
        Data result = a[first]; 
        first = (first+1) % cap;
        free.up();
        return result;
    }
} // end of class BufferFaulty
