Question
Create, compile, and run a program that performs a simulation. Using the code in the book, simulate a grocery checkout checkout counter with one line.
Create, compile, and run a program that performs a simulation. Using the code in the book, simulate a grocery checkout checkout counter with one line. People will enter the line. The only thing a person can do in a line is buy groceries. As the person enters, note (ask) how many groceries the person wishes to purchase. A person's time in line is the same as that number. In total, you should add 6 people to each queue and run for 25 time units. Stop the program when the queue is empty or the 25 time units have expired. At the end, print summary statistics, including:
- which customer (if any) is still being served, even though the simulation has ended, and
- how many customers were actually served, and
- the total time that customers spent in line waiting, and
- the average time a customer waited in line, and
- how many customers were left in line when the simulation ended, and
- a list of those customers who were still in line when the simulation ended.
Can delete the third parameter (the maximum number of time units required to process a customer) from the simulate method.
Also, please submit the results of a sample run along with your code.
/** Simulates a waiting line. @author Frank M. Carrano @author Timothy M. Henry @Modified on May 16, 2019 */ public class WaitLine { private QueueInterface line; private int numberOfArrivals; private int numberServed; private int totalTimeWaited; private Customer currentCustomer = null; public WaitLine() { line = new LinkedQueue<>(); reset(); } // end default constructor /** Simulates a waiting line with one serving agent. @param duration The number of minutes in the simulation @param arrivalProbability A real number between 0 and 1, representing the probability that a customer arrives at a given time @param maxTransactionTime the longest transaction time allowed for a customer */ public void simulate(int duration, double arrivalProbability, int maxTransactionTime) { int transactionTimeLeft = 0; boolean printed = false; currentCustomer = null; for (int clock = 0; clock < duration; clock++) { if (Math.random() < arrivalProbability) { // Add new customer to queue numberOfArrivals++; int transactionTime = (int)(Math.random() * maxTransactionTime + 1); Customer nextArrival = new Customer(clock, transactionTime); line.enqueue(nextArrival); System.out.println("Customer " + nextArrival.getCustomerNumber() + " enters line at time " + clock + ". Transaction time required is " + transactionTime + "."); } // end if if (transactionTimeLeft > 0) transactionTimeLeft--; else { if (!printed) { if (currentCustomer != null) { System.out.println("Customer " + currentCustomer.getCustomerNumber()
+ " exits queue at time " + clock + "."); printed = true; } } if (!line.isEmpty()) { // Process next customer in queue currentCustomer = line.dequeue(); printed = false; transactionTimeLeft = currentCustomer.getTransactionTime() - 1; int timeWaited = clock - currentCustomer.getArrivalTime(); totalTimeWaited += timeWaited; numberServed++; System.out.println("Customer " + currentCustomer.getCustomerNumber() + " begins service at time " + clock + ". Time waited is " + timeWaited + "."); } } // end if } // end for if (transactionTimeLeft > 0) System.out.println("Customer " + currentCustomer.getCustomerNumber() + " is still being served but simulation has ended."); numberServed--; } // end simulate /** Displays summary results of the simulation. */ public void displayResults() { System.out.println(); System.out.println("Number served = " + numberServed); System.out.println("Total time waited = " + totalTimeWaited); double averageTimeWaited = ((double)totalTimeWaited) / numberServed; System.out.format("Average time waited = %5.2f ", averageTimeWaited); int leftInLine = numberOfArrivals - numberServed; System.out.println("Number left in line = " + leftInLine); if (!line.isEmpty()) { System.out.println ("Customers left in line:"); System.out.println (" " + currentCustomer.getCustomerNumber()); while (!line.isEmpty()) { Customer tempCustomer = line.dequeue(); System.out.println (" " + tempCustomer.getCustomerNumber()); } } } // end DisplayResults /** Initializes the simulation. */ public final void reset() { line.clear(); numberOfArrivals = 0; numberServed = 0; totalTimeWaited = 0; } // end reset
} // end WaitLine
public class TestWaitLine { public static void main (String [] args) { WaitLine customerLine = new WaitLine (); // Simulate a waiting line with 20 minutes, 50% arrival probability, // and 5 minute maximum transactop time customerLine.simulate(20, 0.5, 5); customerLine.displayResults(); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Heres the modified code with the requested changes import javautilRandom class Customer private static int customerNumberCounter 0 private int customerNumber private int arrivalTime private int transa...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started