Using the Java code provided ONLY, answer the following question: Modify your implementation so that each teller has a separate queue. When customers arrive, they enter the shortest queue (and stay in that one, never switching queues). Run both versions of the algorithm for
Computer Science
Using the Java code provided ONLY, answer the following question:
Modify your implementation so that each teller has a separate queue. When customers arrive, they enter the shortest queue (and stay in that one, never switching queues). Run both versions of the
ONLY modify the following code provided:
import java.util.Random;
public class Queue {
public int head;
public int tail;
public int size;
public int[] Q;
public Queue(int size) {
this.head = 1;
this.tail = 1;
this.Q = new int[size];
}
public boolean isEmpty() {
if(this.tail == this.head)
return true;
return false;
}
public boolean isFull() {
if(this.head == this.tail+1)
return true;
return false;
}
public void enqueue(int x) {
if(isFull()) {
System.out.println("Queue Overflow");
}
else {
this.Q[this.tail] = x;
if(this.tail == this.size)
this.tail = 1;
else
this.tail = this.tail+1;
}
}
public int dequeue() {
if(isEmpty()) {
System.out.println("Underflow");
return -1000;
}
else {
int x = this.Q[this.head];
if(this.head == this.size) {
this.head = 1;
}
else {
this.head = this.head+1;
}
return x;
}
}
public void display() {
int i;
for(i=this.head; i System.out.println(this.Q[i]);
if(i == this.size) {
i = 0;
}
}
}
public static void main(String[] args) {
Queue q = new Queue(101); //100 cumtomers
int sum =0; //total time taken by all 100 cutomers
Random rand = new Random();
for(int i=1;i<=100;i++){
int tellers = rand.nextInt(3-1) +1; //this is used for selecting random available teller (1-3)
switch(tellers){
case 1:
q.enqueue(i); //customer enter in a queue
int entryTime1 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
int processTime1 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
q.dequeue(); //customer leaves the queue
sum = sum + (entryTime1 + processTime1);
break;
case 2:
q.enqueue(i); //customer enter in a queue
int entryTime2 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
int processTime2 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
q.dequeue(); //customer leaves the queue
sum = sum + (entryTime2 + processTime2);
break;
case 3:
q.enqueue(i); //customer enter in a queue
int entryTime3 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
int processTime3 = rand.nextInt(5-1) + 1; //random integer between 1 to 5
q.dequeue(); //customer leaves the queue
sum = sum + (entryTime3 + processTime3);
}
}
System.out.println(sum/100); //avarage waiting time
}
}
Step by step
Solved in 2 steps