Java Reference
In-Depth Information
public
void
addProject(String courseCode, String task, Date dueDate)
{
Assignment newAssignment =
new
Assignment(courseCode, task,
dueDate);
addProject(newAssignment);
}
// end addProject
public
Assignment getNextProject()
{
return
log.peek();
}
// end getNextProject
public
Assignment removeNextProject()
{
return
log.remove();
}
// end removeNextProject
}
// end AssignmentLog
10.22
The following statements could appear in a client of
AssignmentLog
:
AssignmentLog myHomework =
new
AssignmentLog();
myHomework.addProject("CSC211", "Pg 50, Ex 2",
Date.valueOf("2012-10-21"));
Assignment pg75Ex8 =
new
Assignment("CSC215", "Pg 75, Ex 8",
Date.valueOf("2012-10-14"));
myHomework.addProject(pg75Ex8);
. . .
System.out.println("The following assignment is due next:");
System.out.println(myHomework.getNextProject());
The assignment with the earliest due date is displayed but is not removed from the assignment log.
The standard package
java.util
in the Java Class Library contains the class
PriorityQueue
. This class
implements the interface
Queue
that we described earlier in this chapter. An instance of
PriorityQueue
behaves like a priority queue, not a queue, in that its entries are ordered, with the entry having the smallest
value, and therefore the highest priority, at the front of the priority queue. Since
PriorityQueue
uses the
method
compareTo
to order its entries, the entries must belong to a class that implements the interface
Comparable
. In addition, the entries cannot be
null
.
Here are the basic constructors and methods of the class
PriorityQueue
:
public
PriorityQueue()
Creates an empty priority queue whose initial capacity is 11 entries.
public
PriorityQueue(
int
initialCapacity)
Creates an empty priority queue having a given initial capacity.
public boolean
add(T newEntry)
Adds a new entry to this priority queue, returning true if successful and throwing an excep-
tion if not.
public boolean
offer(T newEntry)
Adds a new entry to this priority queue, returning true or false according to the success of
the operation.