I need some help with my assignment. I have most of it complete, but for some re
ID: 3597484 • Letter: I
Question
I need some help with my assignment. I have most of it complete, but for some reason it is not working. I have copied the code and description below
An updated PriorityCustomer class is provided for you (download from Canvas). 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 – only those that are also waiting in line. The store simulation with regards to the service 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 should represent 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 customers show up (need to be added to the queue) every minute. This does not mean you should add a customer 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 at the 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 the queue. 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 visually identify 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
import java.util.Random;
public 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--;
}
}
public class PriorityQueue {
private PriorityCustomer first,last;
private PriorityCustomer[] heap;
private int size;
private int TotalNumberOfCustomers = 0;
private int currentSize = 0;
public PriorityQueue() {
heap = new PriorityCustomer[50];
size = 0;
}
public void add(PriorityCustomer c) {
int index = size + 1;// index where we'll add the new value
heap[index] = c; // add value at that position
while (index > 1) { // while our value has parents
int parentIndex = index / 2; // get parent index by dividing by 2
if (heap[parentIndex].getPriority() < heap[index].getPriority()) { // if
// parent's
// value
// is
// lower
heap[index] = heap[parentIndex]; // perform swap
heap[parentIndex] = c;
index = parentIndex; // update index after swap
} else { // no swap needed
break;
}
}
size++; // increase size
}
public PriorityCustomer remove() {
PriorityCustomer rootValue = heap[1]; // store root value to return at
// the end
PriorityCustomer v = heap[size]; // store last value in heap in v
heap[1] = v; // take v and move to root
heap[size] = null; // delete node at last position (b/c we moved it to
// the root)
int index = 1;
while (index * 2 < size) { // is there at least one child at index
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) { // there is a right child
rightValue = heap[rightIndex];
}
PriorityCustomer maxChild;
int maxIndex; // find index of and value of larger child
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; // perform swap with larger of two
// children
heap[maxIndex] = v;
index = maxIndex;
} else {
break; // value is in a valid position -> stop
}
import java.util.Random;
public class Driver {
public static void main(String[] args) {
// Create a linked queue
PriorityQueue line = new PriorityQueue();
// Create a customer object
PriorityCustomer myCustomer = new PriorityCustomer();
Random randomNumber = new Random();
int maxLength = 0;
for (int i = 1; i <= 60; i++) {
int num = randomNumber.nextInt(4) + 1;
if (num == 1) {
//Add a new customer to the queue if newCustomer equals 1
line.add(new PriorityCustomer());
//Display a message to the user that a new customer has been added
System.out.println("New Customer added! Queue length is: " + line.getCurrentSize());
//If the max length is less than the current size, then set max length equal to the current size
if (maxLength < line.getCurrentSize()) {
maxLength = line.getCurrentSize();
}
}
if (!line.isEmpty()) {
//Decrease the service time if the customer isn't null
line.getFirst().decServiceTime();
if (line.getFirst().getServiceTime() == 0) {
//Dequeue the line if the service time is 0
line.remove();
//Display a message to the user that a customer was serviced and removed
System.out.println("Customer serviced and removed form the queue. Queue length is now: "
+ line.getCurrentSize());
}
}
System.out.println("-------------------------------------");
}
//Display the total number of customers
System.out.println("The total number of customers is: " + line.getTotalNumberOfCustomers());
//Display the maximum number of customer is the queue
System.out.println("The total Maximum number of customers in the queue is:" + maxLength);
}
}
}
size--; // update size
return rootValue; // return old root
}
public int getSize() { // gets the size of the line.
return size;
}
public PriorityCustomer findMax() { // gets the first customer in line.
PriorityCustomer customer = heap[1]; // first customer in line.
return customer; // returns the first customer in line.
}
public boolean isEmpty() {
return heap[1] == null;
}
// Return the total number of customers
public int getTotalNumberOfCustomers() {
return TotalNumberOfCustomers;
}
// Return the current size
public int getCurrentSize() {
return currentSize;
}
public PriorityCustomer getFirst() {
return first;
}
}
Explanation / Answer
import java.util.Random;
public 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--;
}
}
public class PriorityQueue {
private PriorityCustomer first,last;
private PriorityCustomer[] heap;
private int size;
private int TotalNumberOfCustomers = 0;
private int currentSize = 0;
public PriorityQueue() {
heap = new PriorityCustomer[50];
size = 0;
}
public void add(PriorityCustomer c) {
int index = size + 1;// index where we'll add the new value
heap[index] = c; // add value at that position
while (index > 1) { // while our value has parents
int parentIndex = index / 2; // get parent index by dividing by 2
if (heap[parentIndex].getPriority() < heap[index].getPriority()) { // if
// parent's
// value
// is
// lower
heap[index] = heap[parentIndex]; // perform swap
heap[parentIndex] = c;
index = parentIndex; // update index after swap
} else { // no swap needed
break;
}
}
size++; // increase size
}
public PriorityCustomer remove() {
PriorityCustomer rootValue = heap[1]; // store root value to return at
// the end
PriorityCustomer v = heap[size]; // store last value in heap in v
heap[1] = v; // take v and move to root
heap[size] = null; // delete node at last position (b/c we moved it to
// the root)
int index = 1;
while (index * 2 < size) { // is there at least one child at index
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) { // there is a right child
rightValue = heap[rightIndex];
}
PriorityCustomer maxChild;
int maxIndex; // find index of and value of larger child
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; // perform swap with larger of two
// children
heap[maxIndex] = v;
index = maxIndex;
} else {
break; // value is in a valid position -> stop
}
import java.util.Random;
public class Driver {
public static void main(String[] args) {
// Create a linked queue
PriorityQueue line = new PriorityQueue();
// Create a customer object
PriorityCustomer myCustomer = new PriorityCustomer();
Random randomNumber = new Random();
int maxLength = 0;
for (int i = 1; i <= 60; i++) {
int num = randomNumber.nextInt(4) + 1;
if (num == 1) {
//Add a new customer to the queue if newCustomer equals 1
line.add(new PriorityCustomer());
//Display a message to the user that a new customer has been added
System.out.println("New Customer added! Queue length is: " + line.getCurrentSize());
//If the max length is less than the current size, then set max length equal to the current size
if (maxLength < line.getCurrentSize()) {
maxLength = line.getCurrentSize();
}
}
if (!line.isEmpty()) {
//Decrease the service time if the customer isn't null
line.getFirst().decServiceTime();
if (line.getFirst().getServiceTime() == 0) {
//Dequeue the line if the service time is 0
line.remove();
//Display a message to the user that a customer was serviced and removed
System.out.println("Customer serviced and removed form the queue. Queue length is now: "
+ line.getCurrentSize());
}
}
System.out.println("-------------------------------------");
}
//Display the total number of customers
System.out.println("The total number of customers is: " + line.getTotalNumberOfCustomers());
//Display the maximum number of customer is the queue
System.out.println("The total Maximum number of customers in the queue is:" + maxLength);
}
}
}
size--; // update size
return rootValue; // return old root
}
public int getSize() { // gets the size of the line.
return size;
}
public PriorityCustomer findMax() { // gets the first customer in line.
PriorityCustomer customer = heap[1]; // first customer in line.
return customer; // returns the first customer in line.
}
public boolean isEmpty() {
return heap[1] == null;
}
// Return the total number of customers
public int getTotalNumberOfCustomers() {
return TotalNumberOfCustomers;
}
// Return the current size
public int getCurrentSize() {
return currentSize;
}
public PriorityCustomer getFirst() {
return first;
}
}