An updated PriorityCustomer class is provided for you (see below).You must use t
ID: 3568417 • Letter: A
Question
An updated PriorityCustomer class is provided for you (see below).You must use that class, without alternations, for the creation of your PriorityCustomer objects. You must analyze the class and use the provided methods to achieve the desired functionality of the program. The Driver class that will represent the data structure for holding your Customer objects. In the Driver class, you can import priorityqueue and comparable You must use an array representation of a heap. No ArrayLists or linked structures!
Customers with a priority value higher than other existing customers should be placed in front of them. This is simulated by utilizing a Max Heap to implement your priority queue. The only exception to this is for the customer in the front of the line (the one currently being serviced). If a new customer is added to the line with a higher priority than the front customer, the new customer should not be put in front of the customer being serviced
Explanation / Answer
Java Program:
import java.util.Random;
class PriorityCustomer {
private int serviceTime; // ServiceTime for this Customer
private int priority; // Priority for this Customer
// / Constructor
public PriorityCustomer()
{
serviceTime = new Random().nextInt(5) + 1; // Randomly assign required
// service time 1-5
priority = new Random().nextInt(5) + 1; // Randomly assign priority 1-5
}
public int getPriority()
{
return priority;
}
// / Getter for ServiceTime
public int getServiceTime()
{
return serviceTime;
}
// / Decrement ServiceTime by 1
public void decServiceTime()
{
serviceTime--;
}
}
class CustormerQueue
{
private PriorityCustomer[] Heap;
private int size;
private int maxsize;
private static final int FRONT = 1;
public CustormerQueue(int maxsize)
{
this.maxsize = maxsize;
this.size = 0;
Heap = new PriorityCustomer[this.maxsize + 1];
}
private int parent(int pos)
{
return pos / 2;
}
private int leftChild(int pos)
{
return (2 * pos);
}
private int rightChild(int pos)
{
return (2 * pos) + 1;
}
private boolean isLeaf(int pos)
{
if (pos >= (size / 2) && pos <= size) {
return true;
}
return false;
}
private void swap(int fpos, int spos)
{
PriorityCustomer tmp;
tmp = Heap[fpos];
Heap[fpos] = Heap[spos];
Heap[spos] = tmp;
}
private void maxHeapify(int pos)
{
if (!isLeaf(pos))
{
if (Heap[pos].getPriority() < Heap[leftChild(pos)].getPriority()
|| Heap[pos].getPriority() < Heap[rightChild(pos)].getPriority())
{
if (Heap[leftChild(pos)].getPriority() > Heap[rightChild(pos)].getPriority())
{
swap(pos, leftChild(pos));
maxHeapify(leftChild(pos));
}
else
{
swap(pos, rightChild(pos));
maxHeapify(rightChild(pos));
}
}
}
}
public void insert(PriorityCustomer element)
{
Heap[++size] = element;
int current = size;
while (Heap[current].getPriority() > Heap[parent(current)].getPriority())
{
swap(current, parent(current));
current = parent(current);
}
System.out.println("New customer added! Queue length is now "+Heap.length);
}
public void maxHeap()
{
for (int pos = (size / 2); pos >= 1; pos--)
{
maxHeapify(pos);
}
}
public PriorityCustomer remove()
{
PriorityCustomer popped = Heap[FRONT];
Heap[FRONT] = Heap[size--];
maxHeapify(FRONT);
System.out.println("Customer serviced and removed from the queue. Quest length is now "+Heap.length);
return popped;
}
public void print()
{
System.out.println("Now the Queue Length"+Heap.length);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.