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

Write a program in JAVA that simulates a checkout line at a supermarket.The line

ID: 3835021 • Letter: W

Question

Write a program in JAVA that simulates a checkout line at a supermarket.The line is a queue object. Customers (i.e., customer objects)arrive in random integer intervals of 1–4 minutes. Also, eachcustomer is served in random integer intervals of 1–4minutes. Obviously, the rates need to be balanced. If the averagearrival rate is larger than the average service rate, the queuewill grow infinitely. Even with “balanced” rates,randomness can still cause long lines. Run the supermarketsimulation for a 12-hour day (720 minutes) using the followingalgorithm:

1) Choose a random integer between 1 and 4 to determine the minuteat which the first customer arrives.
2) At the first customer’s arrival time:
Determine customer’s service time (random integer from 1 to4);
Begin servicing the customer;
Schedule arrival time of next customer (random integer 1 to 4 addedto the current time).
3) For each minute of the day:
       If the next customerarrives,
               Sayso,
               Enqueuethe customer;
               Schedulethe arrival time of the next customer;
       If service was completedfor the last customer;
               Sayso
               Dequeuenext customer to be serviced
               Determinecustomer’s service completion time
                      (randominteger from 1 to 4 added to the current time).


Now run your simulation for 720 minutes, and answer each of thefollowing:

a) What is the maximum number of customers in thequeue at any time?
b) What is the longest wait any one customer experiences?
c) What happens if the arrival interval is changed from 1–4minutes to 1–3 minutes?

Explanation / Answer

#include #include #include struct queueNode { /* self-referential structure */ int data; struct queueNode *nextPtr; }; typedef struct queueNode QueueNode; typedef QueueNode *QueueNodePtr; /* function prototypes */ void printQueue( QueueNodePtr ); int isEmpty( QueueNodePtr ); char dequeue( QueueNodePtr *, QueueNodePtr * ); void enqueue( QueueNodePtr *, QueueNodePtr *, int ); int main() { int t_time = 0; //track global time in secs int t_arrival = 0; //track arrival time int t_depart = 0; //track departure and hence time needed to serve last customer int customer_n = 0; QueueNodePtr headptr = NULL,tailptr = NULL; srand((unsigned int)time(NULL)); while(t_time != 50) { if(t_time == t_arrival) { //customer arrives customer_n++; enqueue(&headptr,&tailptr,customer_n); //customer joins the Q t_arrival += rand()%4 + 1; printf(" time = %d,customer %d joins Q, next customer scheduled for %d ",t_time,customer_n,t_arrival); printQueue(headptr); getchar(); } if(t_time == t_depart) { if(!isEmpty(headptr)) { dequeue(&headptr,&tailptr); //customer gets serviced t_depart += rand()%4 + 1; //time customer leaves service printf("time = %d,customer %d ON, next service session at %d sec ",t_time,customer_n,t_depart); } else { //if Q empty schedule service at a later time t_depart += rand()%4 + 1; printf("time = %d,next service session at %d sec ",t_time,t_depart); } printQueue(headptr); getchar(); } t_time++; } printQueue(headptr); return 0; } void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr,int value ) { QueueNodePtr newPtr; newPtr = malloc( sizeof( QueueNode ) ); if ( newPtr != NULL ) { newPtr->data = value; newPtr->nextPtr = NULL; if ( isEmpty( *headPtr ) ) *headPtr = newPtr; else ( *tailPtr )->nextPtr = newPtr; *tailPtr = newPtr; } else printf( "%c not inserted. No memory available. ", value ); } char dequeue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr ) { char value; QueueNodePtr tempPtr; value = ( *headPtr )->data; tempPtr = *headPtr; *headPtr = ( *headPtr )->nextPtr; if ( *headPtr == NULL ) *tailPtr = NULL; free( tempPtr ); return value; } int isEmpty( QueueNodePtr headPtr ) { return headPtr == NULL; } void printQueue( QueueNodePtr currentPtr ) { if ( currentPtr == NULL ) printf( "Queue is empty. " ); else { printf( "The queue is: " ); while ( currentPtr != NULL ) { printf( "%d
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