1Using Queue Ticket Counter Simulation 2 points Our simulate

(1)Using Queue: Ticket Counter Simulation (2 points)

Our simulated ticket counter will use the following assumptions:

. There is only one line and it is first come, first served

. Ticket buyers arrive on average every 15 seconds

. If there is a cashier available, processing begins immediately upon arrival

. Processing a ticket buyer request takes on average two minutes (120

seconds) from the ticket buyer reaches a cashier.

Download TicketBuyer.java .

Use java.util.Queue to write a java program to create a queue of ticket buyers and then see how long it takes to process those ticket buyers if there is only one cashier. Then you will process the same queue of ticket buyers with two cashiers. You continue this process for up to ten cashiers. Display the average times that it takes to process a ticket buyer with different number of cashier.

Code for ticket buyer:

/**
* Customer represents a waiting customer.
*
*/
public class TicketBuyer
{
private int arrivalTime, departureTime;

/**
* Creates a new customer with the specified arrival time.
* @param arrives the arrival time
*/
public TicketBuyer(int arrives)
{
arrivalTime = arrives;
departureTime = 0;
}

/**
* Returns the arrival time of this customer.
* @return the arrival time
*/
public int getArrivalTime()
{
return arrivalTime;
}

/**
* Sets the departure time for this customer.
* @param departs the departure time
**/
public void setDepartureTime(int departs)
{
departureTime = departs;
}

/**
* Returns the departure time of this customer.
* @return the departure time
*/
public int getDepartureTime()
{
return departureTime;
}

/**
* Computes and returns the total time spent by this customer.
* @return the total customer time
*/
public int totalTime()
{
return departureTime - arrivalTime;
}
}

//*******************************************************************
// Line.java   
//
//*******************************************************************

import java.util.LinkedList;

public class Line
{
LinkedList queue;

//----------------------------------------------------------------
// Creates a new line based on a queue.
//----------------------------------------------------------------
public Line()
{
queue = new LinkedList();
}

//----------------------------------------------------------------
// Removes a customer from the head of the queue.
//----------------------------------------------------------------
public Customer nextCustomer()
{
return (Customer)queue.removeFirst();
}

//----------------------------------------------------------------
// Adds a customer to the tail of the queue.
//----------------------------------------------------------------
public void addCustomer(Customer person)
{
queue.addLast(person);
}

//----------------------------------------------------------------
// Indicates whether the queue is empty.
//----------------------------------------------------------------
public boolean isEmpty()
{
return queue.isEmpty();
}

//----------------------------------------------------------------
// Returns the number of customers in the queue.
//----------------------------------------------------------------
public int size()
{
return queue.size();
}
}

//*******************************************************************
// Customer.java
//*******************************************************************

public class Customer
{
private int id;

//----------------------------------------------------------------
// Creates a customer with the specified id number.
//----------------------------------------------------------------
public Customer (int number)
{
id = number;
}

//----------------------------------------------------------------
// Returns a string description of this customer.
//----------------------------------------------------------------
public String toString()
{
return \"Customer \" + id;
}
}

Solution

import jss2.*;

public class TicketCounter
{
final static int PROCESS = 120;
final static int MAX_CASHIERS = 10;
final static int NUM_CUSTOMERS = 100;

public static void main ( String[] args)
{
Customer customer;
LinkedQueue<Customer> customerQueue = new LinkedQueue<Customer>();
int[] cashierTime = new int[MAX_CASHIERS];  
int totalTime, averageTime, departs;

// process the simulation for various number of cashiers
for (int cashiers=0; cashiers < MAX_CASHIERS; cashiers++)
{
// set each cashiers time to zero initially
for (int count=0; count < cashiers; count++)
cashierTime[count] = 0;

// load customer queue
for (int count=1; count <= NUM_CUSTOMERS; count++)
customerQueue.enqueue(new Customer(count*15));

totalTime = 0;

// process all customers in the queue
while (!(customerQueue.isEmpty()))
{
for (int count=0; count <= cashiers; count++)
{
if (!(customerQueue.isEmpty()))
{
customer = customerQueue.dequeue();
if (customer.getArrivalTime() > cashierTime[count])
departs = customer.getArrivalTime() + PROCESS;
else
departs = cashierTime[count] + PROCESS;
customer.setDepartureTime (departs);
cashierTime[count] = departs;
totalTime += customer.totalTime();
}
}
}

// output results for this simulation
averageTime = totalTime / NUM_CUSTOMERS;
System.out.println (\"Number of cashiers: \" + (cashiers+1));
System.out.println (\"Average time: \" + averageTime + \"\ \");
}
}
}

(1)Using Queue: Ticket Counter Simulation (2 points) Our simulated ticket counter will use the following assumptions: . There is only one line and it is first c
(1)Using Queue: Ticket Counter Simulation (2 points) Our simulated ticket counter will use the following assumptions: . There is only one line and it is first c
(1)Using Queue: Ticket Counter Simulation (2 points) Our simulated ticket counter will use the following assumptions: . There is only one line and it is first c
(1)Using Queue: Ticket Counter Simulation (2 points) Our simulated ticket counter will use the following assumptions: . There is only one line and it is first c

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site