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

Java help please modify this code An updated PriorityCustomer class is provided

ID: 3567994 • Letter: J

Question

Java help

please modify this code

An updated PriorityCustomer class is provided for you (download from Moodle). 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. 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 will create the actual heap that will act as the priority queue. 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

Explanation / Answer

import java.util.Random;


class PriorityQueue{

PriorityCustomer[] heap;
private int size;
private int currentServiced;
private int pointer, parentIndex, leftIndex, rightIndex;
//PriorityCustomer first, last;

public void updateCustomerBeingServiced(int currentTime)
{
   if( currentServiced != 101)
   {
       if( currentTime == (heap[currentServiced].startTime + heap[currentServiced].getServiceTime()))
       {
           heap[currentServiced].serviced = true;
           while( heap[++currentServiced].serviced )
               break;
           heap[currentServiced].startTime = currentTime + 1;
       }
   }
   else
   {
       if(heap[pointer] != null){  
       heap[pointer].startTime = currentTime + 1;
       currentServiced = pointer;}
   }  
}

public void printHeap(int i)
{
System.out.println(heap[i].getPriority());
if( heap[2*i + 1] != null){
System.out.println("Left of "+ i);printHeap(2*i + 1);}

if( heap[2*i + 2] != null){
System.out.println("Right of "+i);printHeap(2*i + 2);}
}

PriorityQueue(){

heap = new PriorityCustomer[100];
size = 100; //size should not be incremented, it should be constant. I gave it 1-- because PriorityCustomers can be 100 maximum
pointer = 0;
currentServiced = 101; //initializing it to 101 i.e no customer is getting served.

}
public void Heapify(int i) // utility routine to percolate down from index i
{
int left, r, min=i; // declare variables

left = 2 * i + 1; // left child
r = 2 * i + 2; // right child
if(heap[left] != null){ //Null condition to not give nullPointerException
if(left < size && heap[left].getPriority() > heap[i].getPriority()) // find smallest child
min = left; // save index of smaller child
else
min = i;
}
if(heap[r] != null){ //Null condition to not give nullPointerException
if(r < size && heap[r].getPriority() > heap[min].getPriority())
min = r; // save index of smaller child
}
PriorityCustomer tmp;// = new PriorityCustomer();

//if that customer is serviced then only swap else don't swap.
if(min != i && i>=0 && heap[min].serviced) //It was not checking for i==0,so added that. // swap and percolate, if necessary
{
tmp = heap[i]; // exchange values at two indices
heap[i] = heap[min];
heap[min] = tmp;
Heapify((i-1)/2); // call Heapify

}// end if

}// end method Heapify
void addCustomer(){

PriorityCustomer c = new PriorityCustomer();
heap[pointer] = c;

if(!isEmpty()){

//pointer = parentIndex;
parentIndex = (pointer-1)/2; //changed the logic of parentIndex

Heapify(parentIndex); //addded function to heapify which takes care of all conditions

}
else{

heap[pointer] = c;


}
System.out.println("customer adding: Priority: " + heap[pointer].getPriority() + ", ServiceTime: " + heap[pointer].getServiceTime());


pointer++;


}
public boolean isEmpty(){
if(pointer == 0)
return true;
else
return false;

}
}
  

class PriorityCustomer {
  
private int serviceTime; // ServiceTime for this Customer
private int priority; // Priority for this Customer
public int startTime; //when customer started it's service time
public Boolean serviced;
/// Constructor
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
serviced = false;

}

public int getPriority(){
return priority;
}
  
/// Getter for ServiceTime
public int getServiceTime() {
return serviceTime;
}
  
/// Decrement ServiceTime by 1
public void decServiceTime() {
serviceTime--;
}
}

public class Driver{

public static void main(String[] args)
{
int sizeCounter = 0;
int customerCounter = 0;
PriorityQueue line = new PriorityQueue(); //creates new Priority queue list

for(int i = 1; i<=60; i++) //simulates 60 minutes in the store.
{

int num;
num = new Random().nextInt(4) + 1;
if(num ==1)

{

customerCounter++;

line.addCustomer();
}
line.updateCustomerBeingServiced(i);
}
System.out.println("HEAP");
line.printHeap(0);
}
}

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