Create an application that uses a priorityqueue to perform


Create an application that uses a "PriorityQueue" to perform the following

Uses the constructor that receives a "Comparator" object as an argument
Stores 5 "Time1" objects using the "Time1" class shown in Fig. 8.1 on page 317. The class must be modified to implement the "Comparator" interface

Displays the "Universal Time" in priority order
Note: To determine the ordering when implementing the "Comparator" interface, convert the time into seconds (i.e., hours * 3600 + minutes * 60 + seconds), smaller values infer higher priority)

Here is the PriorityQueue class:

import java.util.Comparator;
import java.util.PriorityQueue;

public class PriorityQueueTest
{
public static void main( String[] args )
{
// queue of capacity 5 and a new Comparator
PriorityQueue< Double > queue =
new PriorityQueue< Double >( 5, new DoubleComparator() )

// insert elements to queue
queue.offer( 3.2 );
queue.offer( 9.8 );
queue.offer( 5.4 );

System.out.print( "Polling from queue: " );

// display elements in queue
while ( queue.size() > 0 )
{
System.out.printf( "%.1f ", queue.peek() ); // view top element
queue.poll(); // remove top element
} // end while
} // end main

private static class DoubleComparator implements Comparator< Double >
{
public int compare( Double first, Double second )
{
return -Double.compare( first, second );
} // end method compare
} // end class TimeComparator
} // end class PriorityQueueTest

Here is the Time class in page 317

public class Time1
{
private int hour;
private int minute;
private int second;

public void setTime(int hour, int minute, int second)
{
if (hour < 0 || hour >= 24 || minute < 0 || minute >=60 ||
second < 0 || second >=60)
{
throw new IllegalArgumentException("out of range");
}
this.hour = hour;
this.minute = minute;
this.second = second;
}
public String.format("%02d:%02d:%02d", hour, minute, second);
}
public String to String()
{
return String.format("%d:%02d:%02d %s",((hour == 0 || hour == 12) ? 12 : hour % 12),
minute, second, (hour < 23 ? "AM" : "PM"));

}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Create an application that uses a priorityqueue to perform
Reference No:- TGS02387029

Now Priced at $15 (50% Discount)

Recommended (97%)

Rated (4.9/5)