Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Customers with a priority value higher than other existing customers should be p

ID: 3805071 • Letter: C

Question

Customers with a priority value higher than other existing customers should be placed in front of them. This issimulated by utilizing a Max Heap to implement your priority queue. The only exception to this is for thecustomer in the front of the line (the one currently being serviced). If a new customer is added to theline with a higher priority than the front customer, the new customer should not be put in front of thecustomer being serviced – only those that are also waiting in line. The store simulation with regards to theservice time of customers, probability that new customers show up, and program output will be the same as it

was for the first program.
The program (driver) should simulate 60 minutes of activity at the store. Each iteration of your program shouldrepresent one minute. At each iteration (minute), your program should do the following:
• Check to see if new customers are added to the queue. There is a 25% chance that new customersshow up (need to be added to the queue) every minute. This does not mean you should add acustomer every four iterations, but rather each iteration should have its own 25% chance.
• Update the customer object currently being serviced (if one exists). This will be the customer object atthe front of the queue. If the customer has been completely serviced, remove them from the queue.
During execution, your program should output the following information:• When a new customer is added to the queue, output, “New customer added! Queue length is now X”

where X is the size of the queue after the new customer has been added.
• When a customer has been completely serviced, output, “Customer serviced and removed from thequeue. Quest length is now X” where X is the size of the queue after the customer has been removed.
• At the end of each iteration (minute), output, “---------------------------------------------------“  to visuallyidentify the passing of time.
When your simulation ends, your program should also output the following information:• Total number of customers serviced• Maximum line length during the simulation

Explanation / Answer

This program simulates a line at a grocery store. For this program you must store Customer objects in a priority queue. You must use the priority customer class coded below, 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. You will also need to create two additional classes. The first will be a PriorityQueue class that will represent the data structure for holding your Customer objects. In your PriorityQueue class. You must use an array representation of a heap. No ArrayLists or linked structures! The second class you will need to create is a driver where your store simulation will take place.
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 – only those that are also waiting in line.
import java.util.Random;   
public class PriorityCustomer {
private int serviceTime;   
private int priority;   
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-
}
public int getPriority(){
return priority;
}
public int getServiceTime() {
return serviceTime;
}
public void decServiceTime() {   
serviceTime--;
}
}


pgm2:
public class PriorityQueue{
private PriorityCustomer[] heap;
private int size;
public PriorityQueue(){
heap = new PriorityCustomer[50];
size = 0;
}
public void add (PriorityCustomer c){
int index = size + 1;
heap[index] = c;   
while ( index > 1 ) {   
int parentIndex = index / 2; if(heap[parentIndex].getPriority() < heap[index].getPriority()) {
heap[index] = heap[parentIndex];   
heap[parentIndex] = c
index = parentIndex;
}else {   
break;
}
}
size ++;
}
public PriorityCustomer remove () {
PriorityCustomer rootValue = heap[1];
PriorityCustomer v = heap[size];   
heap[1] = v;
heap[size] = null; int index = 1;
while(index * 2 < size){
int leftIndex = index * 2;
int rightIndex = leftIndex + 1;
PriorityCustomer leftValue = heap[leftIndex];
PriorityCustomer rightValue = null; //assign a temp value to right child so a swap will not occur.
if(rightIndex < size){
rightValue = heap[rightIndex];
}
PriorityCustomer maxChild;
int maxIndex;
if(leftValue.getPriority() >= rightValue.getPriority()){ //put in '=' so you get FIFO (swap with left child if values are the same
maxChild = leftValue;
maxIndex = leftIndex;
}else{
maxChild = rightValue;
maxIndex = rightIndex;
}
if(v.getPriority() < maxChild.getPriority()){ //value is less than the larger child -> swap
heap[index] = maxChild;
heap[maxIndex] = v;
index = maxIndex;
}else{
break;   
}
}
size --;
return rootValue;   
}
public int getSize(){
return size;
}
public PriorityCustomer findMax() {   
PriorityCustomer customer = heap[1];
return customer;   
}
public boolean isEmpty(){
return heap[1] == null;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote