Help JAVA Using ArrayList to write Queue.java class When a new integer number is
ID: 3661874 • Letter: H
Question
Help JAVA Using ArrayList to write Queue.java class When a new integer number is added to the queue, it is added to the end of the queue and the first integer in the queue is deleted. 1. public Queue() creates a queue (the constructor Method) 2. public void addToEndOfQueue(int val) adds the integer "val" to the end of the queue. 3. public int getNext() removes the next integer from the front of the queue and returns it. 4. public int size() returns the numbeof elements (numbers) that are currently in the queue. 5. public Stringt toString() prints all the integer numbers in the queue seperated by spaces. Printed output looking like {10 5 18}, where 10, 5 and 18 are the integer numbers stored in the queue and 10 is the first in the queue and 18 is the end of the queue.
Explanation / Answer
public class URLQueue {
private ArrayList<URL> urls;
public URLQueue() {
urls = new ArrayList<URL>();
}
public synchronized URL remove() throws InterruptedException {
while (urls.isEmpty())
wait();
URL r = urls.remove(0);
notifyAll();
return r;
}
public synchronized void add(URL newURL) throws InterruptedException {
urls.add(newURL);
notifyAll();
}
public int getSize() {
return urls.size();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.