PART 1. CREATE A PRIORITY QUEUE \"ITEM\" CLASS ---------------------------------
ID: 3768663 • Letter: P
Question
PART 1. CREATE A PRIORITY QUEUE "ITEM" CLASS -------------------------------------------- An item in a "priority queue" has two things: * the data * a priority value Create an Item class, which has two instance variables (properties): * info -- of type int (int for ease) * p -- priority, of type int. Priority values are 1-10, with lower numbers = higher priority. (For ease, you do NOT need to enforce the legal range, 1-10, you can assume that Prog1 testers will always use legal ranges.) The Item class has a compareTo routine, to make comparisons easier: * compareTo -- compares Item priorities. Specifically, obj1.compareTo(obj2) will return: * -1 if obj1 is "LESS THAN" obj2, as per Java's rules, but "less than" IN TERMS OF PRIORITY (so, its p has a HIGHER int value). * 0 if obj1 is "equal to" obj2, meaning their priority p's are equal. * 1 if obj1 is "GREATER THAN" obj2, as per Java's rules, but "greater than" IN TERMS OF PRIORITY (so, it's p has LOWER int value). NOTE: DO NOT USE ARRAYS.SORT FOR HW8 -- *YOU MUST CODE THE PROPER INSERTIONS! The compareTo is just to assist you with that task.
PART 2. PRIORITY QUEUE, USING AN ORDERED ARRAY ---------------------------------------------- Create a CustomPQ class. It has the usual enqueue and dequeue routines, but, as with all priority queues, a new item can cut to the front until it encounters an item with same or higher priority. To keep track of the items, use an array. Hint: Your CustomPQ objects each get an array of Items. (For ease, assume array size 100, and assume the user will never insert more than 100 items. Also, you will NOT need to use the "%" efficiency tricks from the "array queue" lectures.) The data-tracking array must be ordered, which means: the items in the array exactly match what the "line" looks like -- that is, the highest priority items are at the left/front of the array, the lowest priority items are at the right/back of the array. So, a dequeue simply takes out the front item (highest-priority item). But, the enqueue must calculate where each new item must get inserted into the array, then must shift the array's items to fit it there. When a Prog1 tester program uses the priority queue's enqueue, the Prog1 will specify the item to insert by using two int properties as parameters, NOT by creating an Item object: an int parameter for the info, and an int parameter for the p. See sample Prog1 below. When a Prog1 tester program uses the priority queue's dequeue, it will get the proper item's info, NOT the priority p. See sample Prog1 below.
NEED A CODE THAT WORKS FOR THIS IN JAVA PLEASE
Explanation / Answer
import java.util.Scanner;
class Task
{
String job;
int priority;
public Task(String job, int priority)
{
this.job = job;
this.priority = priority;
}
public String toString()
{
return "Job Name : "+ job +" Priority : "+ priority;
}
}
class PQueue
{
private Task[] heap;
private int heapSize, capacity;
public PQueue(int capacity)
{
this.capacity = capacity + 1;
heap = new Task[this.capacity];
heapSize = 0;
}
public void clear()
{
heap = new Task[capacity];
heapSize = 0;
}
public boolean isEmpty()
{
return heapSize == 0;
}
public boolean isFull()
{
return heapSize == capacity - 1;
}
{
return heapSize;
}
public void insert(String job, int priority)
{
Task newJob = new Task(job, priority);
heap[++heapSize] = newJob;
int pos = heapSize;
while (pos != 1 && newJob.priority > heap[pos/2].priority)
{
heap[pos] = heap[pos/2];
pos /=2;
}
heap[pos] = newJob;
}
public Task remove()
{
int parent, child;
Task item, temp;
if (isEmpty() )
{
System.out.println("Heap is empty");
return null;
}
item = heap[1];
temp = heap[heapSize--];
parent = 1;
child = 2;
while (child <= heapSize)
{
if (child < heapSize && heap[child].priority < heap[child + 1].priority)
child++;
if (temp.priority >= heap[child].priority)
break;
heap[parent] = heap[child];
parent = child;
child *= 2;
}
heap[parent] = temp;
return item;
}
}
public class PQueueTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Priority Queue Test ");
System.out.println("Enter size of priority queue ");
PQueue pq = new PQueue(scan.nextInt() );
char ch;
do
{
System.out.println(" Priority Queue Operations ");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. check empty");
System.out.println("4. check full");
System.out.println("5. clear");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter job name and priority");
pq.insert(scan.next(), scan.nextInt() );
break;
case 2 :
System.out.println(" Job removed "+ pq.remove());
break;
case 3 :
System.out.println(" Queue Status is Empty : "+ pq.isEmpty() );
break;
case 4 :
System.out.println(" Queue Status is full: "+ pq.isFull() );
break;
case 5 :
System.out.println(" Queue is Cleared");
pq.clear();
break;
case 6 :
System.out.println(" Size = "+ pq.size() );
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.println(" continue to this process (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.