I have a LinkedList class, which serves as the base for a Queue class, which serves as the base for a PrintQueue class.
Here’s my PrintQueue class:
public class PrintQueue<T> {
private Queue<T> queue;
public PrintQueue() {
queue = new Queue<T>();
}
public void lpr(String owner, int jobID) {
queue.enqueue(new Job(owner, jobID));
}
}
The queue.enqueue(...) line, three from the bottom is resulting in an error:
The method enqueue(T) in the type Queue is not applicable for the arguments (Job)
PrintQueue is a queue of Job objects.
The enqueue method in my Queue class looks like this:
public void enqueue(T item) {
queue.addToEnd(item);
}
And the addToEnd method looks like this:
public void addToEnd(T item) {
Node<T> itemnode = new Node<T>(item, null);
if (isEmpty()) {
head = itemnode;
}
else {
Node<T> curr = head;
while (curr.getNext()!=null) {
curr= curr.getNext();
}
curr.setNext(itemnode);
}
count++;
}
Why isn’t this working? All three classes (PrintQueue, Queue and LinkedList) are generic classes .
You’re trying to create a
Queue<T>for some arbitrary typeT– but then you’re trying to enqueue aJob.What do you think it means to enqueue a
Jobon aQueue<String>for example?Do you really need
PrintQueueto be generic? I suspect you just want:(Additionally, it’s not clear why you’ve got your own
Queueclass, when there are various good queues in the built-in class library… ones that don’t have O(N) “add to end” behaviour, too…)