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

Please I have a class called \"ArrayQueue\" which I want to implement all the me

ID: 3623750 • Letter: P

Question

Please I have a class called "ArrayQueue" which I want to implement all the methods of the interface "Queue". Those methods are used by the class Bank to perform the necessary operations. You can add more methods if you find it necessary. Below is little brief about the code below it. Please help, thanks

In this program I would like to simulate a bank operation with customers being served by the tellers of the bank. The customers are arriving at random time points and they are put in a line, which in our case will be implemented as a fixed capacity queue. They wait there until a teller is available to serve them.

The simulation will decide how many customers were accepted, how many customers were served, how many customers were turned away because the queue was full, what was the average queue size during the operation hours and what was the average waiting time for the customers.

Execution example:
Input:
>java Bank 30 3 40

Output:
Customers accepted: 122
Customers served: 92
Customers waiting: 30
Turnaways: 2
Average queue size: 15.511111
Average wait time: 22.554348 minutes






package tryproj;

public class Bank
{
/**
* The main method of the class.
* @param args Command line arguments.
*/
public static void main(String [] args)
{

//maximum size of queue
int qCapacity = Integer.parseInt(args[0]);

//number of simulation hours
int simHours = Integer.parseInt(args[1]);

//average number of customers per hour
int custPerHour = Integer.parseInt(args[2]);

//Run simulation
simulation(qCapacity, simHours, custPerHour);
}

/**
* This methods performs a simulation of a Bank operation using a queue and
* prints the results.
* @param qCapacity The fixed capacity of the queue to be used.
* @param simHours The number of hours that the simulation should run.
* @param custPerHour The expected number of customers to arrive per hour.
*/
private static void simulation(int qCapacity, int simHours, int custPerHour)
{
//Constant
final int MIN_PER_HR = 60;

//A queue that will hold and manage objects of type Customer.
Queue<Customer> line = new ArrayQueue<Customer>(qCapacity);

//For how many cycles should the simulation run. We assume that each
//cycle takes one minute.
int cycleLimit = MIN_PER_HR*simHours;

//The average number of minutes that will pass until the next customer
//arrives.
float minPerCust = ((float)MIN_PER_HR)/custPerHour;

//The number of customers that were turned away because the line (queue)
//was full at the time they arrived.
int turnAways = 0;

//Number of customers that arrived.
int customers = 0;

//Number of customers that were served.
int served = 0;

//Total number of customers that entered the line (queue).
int sumLine = 0;

//Waiting time until the next customer is served.
int waitTime = 0;

//Total time that all the customers waited in the line.
int lineWait = 0;

//Simulation
for (int cycle = 0; cycle < cycleLimit; cycle++)
{
if (newCustomer(minPerCust))
{
if (line.isFull())
{
turnAways++;
}
else
{
customers++;
Customer customer = new Customer();
customer.set(cycle);
line.enqueue(customer);
}
}

if (waitTime <= 0 && !line.isEmpty())
{
Customer customer = (Customer) line.dequeue();
waitTime = customer.procTime();
lineWait += cycle - customer.when();
served++;
}

if (waitTime > 0) {
waitTime--;
}

sumLine += line.size();
}

//Print the simulation results.
if (customers > 0) {
System.out.println(" Customers accepted: " + customers);
System.out.println(" Customers served: " + served);
System.out.println(" Customers waiting: " + line.size());
System.out.println(" Turnaways: " + turnAways);
System.out.println("Average queue size: " + (float) sumLine / cycleLimit);
System.out.println(" Average wait time: " + (float) lineWait / served + " minutes");
}
else
{
System.out.println("No customers!");
}
}

/**
* This method decides if a new customer has arrived at each time based on
* the customer arrival rate.
* @param minPerCust Number of minutes between two customer arrivals.
* @return true if a new customer has arrived, otherwise false.
*/
private static boolean newCustomer(float minPerCust)
{
return (Math.random() * minPerCust < 1);
}
}





class Customer
{
int arrive; //Time point that the customer arrived.
int processTime; //Time duration that the customer will need to be served.

/**
* Default constructor
*/
public Customer()
{
arrive = processTime = 0;
}

/**
* Set the arrival time point of the customer.
* @param when Time point
*/
public void set(int when)
{
arrive = when;

//We set the processing time as a random integer between 1 and 3.
processTime = (int)(Math.random()*3+1);
}

/**
* @return the arrival time point of the customer.
*/
public int when()
{
return arrive;
}

/**
* @return the processing time of the customer.
*/
public int procTime()
{
return processTime;
}
}



interface Queue<Item>
{

/** Append item to the queue.
* @param item. An item to add at the end of the queue.
* @return true if the operation was successful and false if the operation
* was unsuccessful, e.g. if the queue is full.
*/
public boolean enqueue(Item item);

/** Remove and return the front element of the queue.
* @return the front element of the queue.
*/
public Item dequeue();

/** Return the number of elements in queue. */
public int size();

/**
* Checks if the queue is full.
* @return true if the queue is full, i.e. if the number of elements in the
* queue has reached its capacity.
*/
public boolean isFull();

/**
* Checks if the queue is empty.
* @return true if the queue is empty, i.e. if the number of elements in the
* queue is 0.
*/
public boolean isEmpty();
}


// Please "ArrayQueue is the class I need help at to make my code output the sample //above. Thanks

class ArrayQueue<Item> implements Queue<Item>
{

public ArrayQueue(int qCapacity)
{

}

public boolean enqueue(Item item)
{

}

public Item dequeue()
{

}

public int size()
{

}

public boolean isFull()
{

}

public boolean isEmpty()
{

}

}

Explanation / Answer

public class ArrayQueue implements Queue {

// data holds the objects in the queue.
// It has to be an array of Objects since Java doesn't permit an array of
// a generic type (in this case Item).
// The items are in order with lower indexes being closer to the front
// EXCEPT that the items can 'wrap around' in the array
// i.e. when the end of the array is reached, if there is space at the beginning
// of the array (due to items having been removed)
// then new items will be inserted at the beginning.
// For instance if qCapacity is 6 and the queue currently holds five items
// I1 I2 I3 I4 and I5 with I1 first and I5 last,
// and I1 is at index 3 in the array,
// the array would look like this: {I4,I5,null,I1,I2,I3}
private Object[] data;
// queueHead is the index of the front item in the queue.
private int queueHead;
// queueTail is the index where the next item in the queue will be inserted
// i.e. queueTail is one position after the current last item.
// queueTail will be a higher number than queueHead UNLESS
// the queue has wrapped around.
private int queueTail;
private int queueSize;

public ArrayQueue(int qCapacity)
{
data = new Object[qCapacity];
queueHead = 0;
queueTail = 0;
}

public boolean enqueue(Item item)
{
if (queueSize == data.length) {
return false;
}
data[queueTail]=item;
queueTail += 1;

// wrap around if necessary
queueTail = queueTail % data.length;

queueSize += 1;
return true;
}

public Item dequeue()
{
if (queueSize == 0) return null;
@SuppressWarnings("unchecked")
Item frontItem = (Item) data[queueHead];

// The next item will be at the next position in the array.
queueHead += 1;

// If the head was at the last item in the array
// then due to "wrap around"
// the next item will be at the beginning of the array,
queueHead = queueHead % data.length;

queueSize -= 1;
return frontItem;
}

public int size()
{
return queueSize;
}

public boolean isFull()
{
return queueSize == data.length;
}

public boolean isEmpty()
{
return queueSize == 0;
}

}