Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the class WaitLine ! Driver.java public class Driver { public static void main(String[] args) { WaitLine customerLine = new WaitLine(); customerLine.simulate(20, 0.5, 5); customerLine.displayResults();

Complete the class WaitLine !

Driver.java public class Driver { public static void main(String[] args) { WaitLine customerLine = new WaitLine(); customerLine.simulate(20, 0.5, 5); customerLine.displayResults(); System.out.println(" Done."); } // end main } // end Driver /* Customer 1 enters line at time 1. Transaction time is 5 Customer 1 begins service at time 1. Time waited is 0 Customer 2 enters line at time 3. Transaction time is 2 Customer 3 enters line at time 4. Transaction time is 1 Customer 4 enters line at time 6. Transaction time is 1 Customer 2 begins service at time 6. Time waited is 3 Customer 5 enters line at time 7. Transaction time is 5 Customer 3 begins service at time 8. Time waited is 4 Customer 6 enters line at time 9. Transaction time is 3 Customer 4 begins service at time 9. Time waited is 3 Customer 5 begins service at time 10. Time waited is 3 Customer 7 enters line at time 12. Transaction time is 4 Customer 8 enters line at time 13. Transaction time is 1 Customer 6 begins service at time 15. Time waited is 6 Customer 9 enters line at time 16. Transaction time is 1 Customer 7 begins service at time 18. Time waited is 6 Number served = 7 Total time waited = 25 Average time waited = 3.5714285714285716 Number left in line = 2 Done. */

WaitLine.java

public class WaitLine { private QueueInterface line; private int numberOfArrivals; private int numberServed; private int totalTimeWaited; public WaitLine() { line = new LinkedQueue(); reset(); } // end default constructor /** Simulates a waiting line with one serving agent. @param duration the number of simulated minutes @param arrivalProbability a real number between 0 and 1, and the probability that a customer arrives at a given time @param maxTransactionTime the longest transaction time for a customer */ public void simulate(int duration, double arrivalProbability, int maxTransactionTime) { int transactionTimeLeft = 0; for (int clock = 0; clock < duration; clock++) { if (Math.random() < arrivalProbability) { // Add a new customer numberOfArrivals = //////////////// ADD CODE HERE /////////////// int transactionTime = (int)(Math.random() * maxTransactionTime + 1); Customer nextArrival = new Customer(clock, transactionTime, numberOfArrivals); // add nextArrival to the line //////////////// ADD CODE HERE /////////////// System.out.println("Customer " + numberOfArrivals + " enters line at time " + clock + ". Transaction time is " + transactionTime); } // end if if (transactionTimeLeft > 0) { // still processing the current customer, decrease transaction time left transactionTimeLeft //////////////// ADD CODE HERE /////////////// } else if (!line.isEmpty()) { // the customer has been processed // take the next customer from the line Customer nextCustomer = //////////////// ADD CODE HERE /////////////// transactionTimeLeft = nextCustomer.getTransactionTime() - 1; // update the total time waited by customers int timeWaited = clock - nextCustomer.getArrivalTime(); totalTimeWaited = //////////////// ADD CODE HERE ////////////// // update the number of served customers numberServed //////////////// ADD CODE HERE /////////////// System.out.println("Customer " + nextCustomer.getCustomerNumber() + " begins service at time " + clock + ". Time waited is " + timeWaited); } // end if } // end for } // 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.println("Average time waited = " + averageTimeWaited); int leftInLine = numberOfArrivals - numberServed; System.out.println("Number left in line = " + leftInLine); } // end displayResults /** Initializes the simulation. */ public final void reset() { line.clear(); numberOfArrivals = 0; numberServed = 0; totalTimeWaited = 0; } // end reset } // end WaitLine

Customer.java

public class Customer { private int arrivalTime; private int transactionTime; private int customerNumber; public Customer(int arrivalTime, int transactionTime, int customerNumber) { this.arrivalTime = arrivalTime; this.transactionTime = transactionTime; this.customerNumber = customerNumber; } // end constructor public int getArrivalTime() { return arrivalTime; } // end getArrivalTime public int getTransactionTime() { return transactionTime; } // end getTransactionTime public int getCustomerNumber() { return customerNumber; } // end getCustomerNumber } // end Customer

LinkedQueue.java

public class LinkedQueue implements QueueInterface { private Node firstNode; // references node at front of queue private Node lastNode; // references node at back of queue public LinkedQueue() { firstNode = null; lastNode = null; } // end default constructor public void enqueue(T newEntry) { Node newNode = new Node(newEntry, null); if (isEmpty()) firstNode = newNode; else lastNode.setNextNode(newNode); lastNode = newNode; } // end enqueue public T getFront() { T front = null; if (!isEmpty()) front = firstNode.getData(); return front; } // end getFront public T dequeue() { T front = null; if (!isEmpty()) { front = firstNode.getData(); firstNode = firstNode.getNextNode(); if (firstNode == null) lastNode = null; } // end if return front; } // end dequeue public boolean isEmpty() { return (firstNode == null) && (lastNode == null); } // end isEmpty public void clear() { firstNode = null; lastNode = null; } // end clear private class Node { private T data; // entry in queue private Node next; // link to next node private Node(T dataPortion) { data = dataPortion; next = null; } // end constructor private Node(T dataPortion, Node linkPortion) { data = dataPortion; next = linkPortion; } // end constructor private T getData() { return data; } // end getData private void setData(T newData) { data = newData; } // end setData private Node getNextNode() { return next; } // end getNextNode private void setNextNode(Node nextNode) { next = nextNode; } // end setNextNode } // end Node } // end Linkedqueue

QueueInterface.java

public interface QueueInterface { /* Adds a new entry to the back of this queue. @param newEntry an object to be added */ public void enqueue(T newEntry); /* Removes and returns the entry at the front of this queue. @return either the object at the front of the queue or, if the queue is empty before the operation, null */ public T dequeue(); /* Retrieves the entry at the front of this queue. @return either the object at the front of the queue or, if the queue is empty, null */ public T getFront(); /* Detects whether this queue is empty. @return true if the queue is empty, or false otherwise */ public boolean isEmpty(); /* Removes all entries from this queue. */ public void clear(); } // end QueueInterface

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions