Java Reference
In-Depth Information
/** Gets the size of this priority queue.
@return the number of entries currently in the priority queue */
public int getSize();
/** Removes all entries from this priority queue */
public void clear();
} // end PriorityQueueInterface
Question 4 After the following statements execute, what string is at the front of the prior-
ity queue and what string is at the back?
PriorityQueueInterface<String> myPriorityQueue =
new LinkedPriorityQueue<String>();
myPriorityQueue.add("Jane");
myPriorityQueue.add("Jim");
myPriorityQueue.add("Jill");
String name = myPriorityQueue.remove();
myPriorityQueue.add(name);
myPriorityQueue.add("Jess");
A Problem Solved: Tracking Your Assignments
Professors and bosses like to assign tasks for us to do by certain dates. Using a priority queue,
organize these assignments in the order in which we should complete them.
10.20
To keep our example simple, we will order the assignments by their due dates. A task with the earliest
due date will have the highest priority.
We can define a class Assignment of tasks that includes a data field date representing a task's due
date. Figure 10-12 shows a diagram of such a class. We assume that date is an instance of a Comparable
class such as java.sql.Date in the Java Class Library. Thus, date.compareTo(otherDate) is negative,
for example, if date occurs before otherDate . The compareTo method for Assignment is then
public int compareTo(Assignment other)
{
return -date.compareTo(other.date);
} // end compareTo
A more sophisticated version of Assignment could include other criteria in compareTo to assess priority.
FIGURE 10-12
A diagram of the class Assignment
Assignment
course —the course code
task —a description of the assignment
date —the due date
getCourseCode()
getTask()
getDueDate()
compareTo()
 
 
Search WWH ::




Custom Search