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

Use array and loop structure, each iteration represents 1 minute (8hrs = 480mins

ID: 3906158 • Letter: U

Question

Use array and loop structure, each iteration represents 1 minute (8hrs = 480mins)

No hashmap please.

Enqueue to serve, Dequeue after 3 iterations

GIVEN__________________________-

public class NumberQueue{

private int[] data;
private int front, rear, numberOfItems, capacity;
public NumberQueue(int cap){
front=rear=numberOfItems=0;
data=new int[cap];
capacity=cap;
}
public boolean isEmpty(){
return numberOfItems==0;
}
public boolean isFull(){
return numberOfItems==capacity;
}

public int size(){
return numberOfItems;
}
public void enqueue(int val){
if(!isFull()){
data[rear]=val;
rear= ++rear % capacity;
numberOfItems++;
}else{
System.out.println("it is full, find another restaurant!");
}
}
public int dequeue(){
if(!isEmpty()){
int val=data[front];
front = ++front % capacity;
numberOfItems--;
return val;
}else{
return -1000;
}
}
}

Explanation / Answer

solution:

import java.util.HashMap;
public class QueueSimulation {
NumberQueue numberQueue;
public QueueSimulation() {
this.numberQueue = new NumberQueue(10);
}
void run() {
int totalMinServe = 8 * 60;
int person = 1;
int maxSize = 0;
HashMap<Integer, Integer> personComeAt = new HashMap<>();
int servedPerson = 1;
for (int min = 0; min <= totalMinServe; min += 1) {
if (min % 5 == 0) {
if (numberQueue.isFull()) {
System.out.printf("Person %s ", person);
}
numberQueue.enqueue(person);
personComeAt.put(person, min);
}
if (personComeAt.containsKey(servedPerson) && min ==
personComeAt.get(servedPerson) + 3) {
numberQueue.dequeue();
servedPerson += 1;
}
person += 1;
maxSize = maxSize > numberQueue.size() ? maxSize :
numberQueue.size();
}
System.out.println("Max Queue Size :: " + maxSize);
}
public static void main(String[] args) {
QueueSimulation queueSimulation = new QueueSimulation();
queueSimulation.run();
}
}
class NumberQueue {
private int[] data;
private int front, rear, numberOfItems, capacity;
public NumberQueue(int cap) {
front = rear = numberOfItems = 0;
data = new int[cap];
capacity = cap;
}
public boolean isEmpty() {
return numberOfItems == 0;
}
public boolean isFull() {
return numberOfItems == capacity;
}
public int size() {
return numberOfItems;
}
public void enqueue(int val) {
if (!isFull()) {
data[rear] = val;
rear = ++rear % capacity;
numberOfItems++;
} else {
System.out.println("it is full, find another restaurant!");
}
}
public int dequeue() {
if (!isEmpty()) {
int val = data[front];
front = ++front % capacity;
numberOfItems--;
return val;
} else {
return -1000;
}
}
}

Thankyou, i hope it helps you please like it.