Implement a Queue using either a LinkedList or an ArrayList
Implement a Queue using either a LinkedList or an ArrayList as the underlying structure. You only need to write methods for enqueue, dequeue, hasItems, and likely a size method. You will also need a constructor which will be responsible for creating the list.
Add a Student class which has a String name, static int id, String courseName and an int waitTime. When a student is created, assign it a random wait time (between 1 and 10 seconds) and increment the static counter.
Next, you will implement the classic producer/consumer problem. Create a class which will act as a Producer for Student objects. The Producer class will extend the Thread class. It will share a Queue with the Consumer class, and it will add Students to the Queue at a random time interval (between 1 and 5 seconds). This class will run as a Thread like this:
Thread producerThread = new Thread(new Producer(sharedQueue), \"Producer\");
Then, implement a Consumer class which will simply pull the next Student out of the Queue and sleep for the dedicated waitTime. This will simulate the waiting time while that Student is being served at the registrar window. You can use Thread.sleep(n) for pausing a Thread. n is in milliseconds, so remember to multiply by 1000. If there are no Students in the Queue, you will want to pause your Thread for a few seconds and wait for the Producer to add more.
Solution
public class QueueImpl {
private int head,tail=-1;
private Object[] arr;
private int size;
private int capacity;
QueueImpl(int i){
arr=new Object[i];
capacity=arr.length;
}
public Object pop(){
Object value =null;
if(tail<=head){
throw new IndexOutOfBoundsException(\"underflow\");
}
else{
value=arr[head];
arr[head]=null;
head++;
size--;
}
return value;
}
public boolean add(Object o){
if(size==0){
head=tail=0;
arr[tail++]=o;
size++;
return true;
}
else if (tail+1>capacity){
throw new IndexOutOfBoundsException(\"overflow\");
}
else{
arr[tail++]=o;
size++;
return true;
}
}
public int getSize() {
return size;
}
}

