Question
Revise the Average Waiting Time program to do the following: a) Also output the largest number of customers who were on a queue at the
Revise the Average Waiting Time program to do the following:
a) Also output the largest number of customers who were on a queue at the same time.
b) Choose the queue for a customer to enter based on shortest finish time, rather than shortest size. The user should have the ability to choose which approach to use for any simulation run.
//---------------------------------------------------------------------
// Simulation.java by Dale/Joyce/Weems Chapter 5
//
// Models a sequence of customers being serviced
// by a number of queues.
//---------------------------------------------------------------------
import support.*; // Customer, CustomerGenerator
import ch05.queues.*;
public class Simulation
{
final int MAXTIME = Integer.MAX_VALUE;
CustomerGenerator custGen; // a customer generator
float avgWaitTime = 0.0f; // average wait time for most recent simulation
public Simulation(int minIAT, int maxIAT, int minST, int maxST)
{
custGen = new CustomerGenerator(minIAT, maxIAT, minST, maxST);
}
public float getAvgWaitTime()
{
return avgWaitTime;
}
public void simulate(int numQueues, int numCustomers)
// Preconditions: numQueues > 0
// numCustomers > 0
// No time generated during simulation is > MAXTIME
//
// Simulates numCustomers customers entering and leaving the
// a queuing system with numQueues queues
{
// the queues
GlassQueue
Customer nextCust; // next customer from generator
Customer cust; // holds customer for temporary use
int totWaitTime = 0; // total wait time
int custInCount = 0; // count of customers started so far
int custOutCount = 0; // count of customers finished so far
int nextArrTime; // next arrival time
int nextDepTime; // next departure time
int nextQueue; // index of queue for next departure
int shortest; // index of shortest queue
int shortestSize; // size of shortest queue
Customer rearCust; // customer at rear of shortest queue
int finishTime; // calculated finish time for customer being enqueued
// instantiate the queues
for (int i = 0; i < numQueues; i++)
queues[i] = new GlassQueue
// set customer generator and get first customer
custGen.reset();
nextCust = custGen.nextCustomer();
while (custOutCount < numCustomers) // while still more customers to handle
{
// get next arrival time
if (custInCount != numCustomers)
nextArrTime = nextCust.getArrivalTime();
else
nextArrTime = MAXTIME;
// get next departure time and set nextQueue
nextDepTime = MAXTIME;
nextQueue = -1;
for (int i = 0; i < numQueues; i++)
if (queues[i].size() != 0)
{
cust = queues[i].peekFront();
if (cust.getFinishTime() < nextDepTime)
{
nextDepTime = cust.getFinishTime();
nextQueue = i;
}
}
if (nextArrTime < nextDepTime)
// handle customer arriving
{
// determine shortest queue
shortest = 0;
shortestSize = queues[0].size();
for (int i = 1; i < numQueues; i++)
{
if (queues[i].size() < shortestSize)
{
shortest = i;
shortestSize = queues[i].size();
}
}
// determine the finish time
if (shortestSize == 0)
finishTime = nextCust.getArrivalTime() + nextCust.getServiceTime();
else
{
rearCust = queues[shortest].peekRear();
finishTime = rearCust.getFinishTime() + nextCust.getServiceTime();
}
// set finish time and enqueue customer
nextCust.setFinishTime(finishTime);
queues[shortest].enqueue(nextCust);
custInCount = custInCount + 1;
// if needed, get next customer to enqueue
if (custInCount < numCustomers)
nextCust = custGen.nextCustomer();
}
else
// handle customer leaving
{
cust = queues[nextQueue].dequeue();
totWaitTime = totWaitTime + cust.getWaitTime();
custOutCount = custOutCount + 1;
}
} // end while
avgWaitTime = totWaitTime/(float)numCustomers;
}
}
//----------------------------------------------------------------------
// SimGUI.java by Dale/Joyce/Weems Chapter 5
//
// Simulates customers waiting in queues. Customers always enter
// the shortest queue.
//
// Input consists of simulation instance information:
// Minimum and maximum customer inter-arrival time.
// Minimum and maximum customer service time.
// Number of queues and customers.
//
// Assumes a friendly user, i.e., inputs are valid.
//
// Output is the average waiting time for a customer.
// Uses a graphical user interface.
//----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import support.*;
public class SimGUI
{
// input text fields
private static JTextField minIATText;
private static JTextField maxIATText;
private static JTextField minSTText;
private static JTextField maxSTText;
private static JTextField numQueuesText;
private static JTextField numCustText;
// status Label
private static JLabel resultLabel; // label for status/result info
// Define a button listener.
private static class ActionHandler implements ActionListener
{
public void actionPerformed(ActionEvent event)
// listener for the button events
{
if (event.getActionCommand().equals("Simulate"))
{ // Handles Evaluate event.
float result;
Simulation sim = new Simulation
(Integer.parseInt(minIATText.getText()),
Integer.parseInt(maxIATText.getText()),
Integer.parseInt(minSTText.getText()),
Integer.parseInt(maxSTText.getText()));
sim.simulate(Integer.parseInt(numQueuesText.getText()),
Integer.parseInt(numCustText.getText()));
result = sim.getAvgWaitTime();
resultLabel.setText(" Average Wait Time: " + result);
}
else
if (event.getActionCommand().equals("Clear"))
{ // Handles Clear event.
resultLabel.setText(" RESULT ");
minIATText.setText("");
maxIATText.setText("");
minSTText.setText("");
maxSTText.setText("");
numQueuesText.setText("");
numCustText.setText("");
}
}
}
public static void main(String args[]) throws IOException
{
// Declare/instantiate/initialize display frame.
JFrame displayFrame = new JFrame();
displayFrame.setTitle("Queue Simulation Program");
displayFrame.setSize(350,300);
displayFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// text boxes for input
minIATText = new JTextField("", 5);
maxIATText = new JTextField("", 5);
minSTText = new JTextField("", 5);
maxSTText = new JTextField("", 5);
numQueuesText = new JTextField("", 5);
numCustText = new JTextField("", 5);
// input/result labels
JLabel InArgLabel = new JLabel(" Input Arguments:", JLabel.LEFT);
JLabel minIATLabel = new JLabel(" Minimum Inter-Arrival Time", JLabel.CENTER);
JLabel maxIATLabel = new JLabel(" Maximum Inter-Arrival Time", JLabel.CENTER);
JLabel minSTLabel = new JLabel(" Minimum Service Time", JLabel.CENTER);
JLabel maxSTLabel = new JLabel(" Maximum Service Time", JLabel.CENTER);
JLabel numQueuesLabel = new JLabel(" Number of Queues", JLabel.CENTER);
JLabel numCustLabel = new JLabel(" Number of Customers", JLabel.CENTER);
resultLabel = new JLabel(" RESULT ", JLabel.CENTER);
// input/result panels
JPanel minIATPanel = new JPanel();
JPanel maxIATPanel = new JPanel();
JPanel minSTPanel = new JPanel();
JPanel maxSTPanel = new JPanel();
JPanel numQueuesPanel = new JPanel();
JPanel numCustPanel = new JPanel();
JPanel resultPanel = new JPanel();
// set input/result panel layouts
minIATPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
maxIATPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
minSTPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
maxSTPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
numQueuesPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
numCustPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
resultPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// add items to input/result panels
minIATPanel.add(minIATLabel);
minIATPanel.add(minIATText);
maxIATPanel.add(maxIATLabel);
maxIATPanel.add(maxIATText);
minSTPanel.add(minSTLabel);
minSTPanel.add(minSTText);
maxSTPanel.add(maxSTLabel);
maxSTPanel.add(maxSTText);
numQueuesPanel.add(numQueuesLabel);
numQueuesPanel.add(numQueuesText);
numCustPanel.add(numCustLabel);
numCustPanel.add(numCustText);
resultPanel.add(resultLabel);
// Simulate and Clear buttons
JButton simulate = new JButton("Simulate");
JButton clear = new JButton("Clear");
// Button event listener
ActionHandler action = new ActionHandler();
// Register button listeners.
simulate.addActionListener(action);
clear.addActionListener(action);
// Instantiate content pane and information panels.
Container contentPane = displayFrame.getContentPane();
JPanel mainPanel = new JPanel();
JPanel buttonPanel = new JPanel();
// Initialize main panel.
mainPanel.setLayout(new GridLayout(7,1));
mainPanel.add(InArgLabel);
mainPanel.add(minIATPanel);
mainPanel.add(maxIATPanel);
mainPanel.add(minSTPanel);
mainPanel.add(maxSTPanel);
mainPanel.add(numQueuesPanel);
mainPanel.add(numCustPanel);
// Initialize button panel.
buttonPanel.setLayout(new GridLayout(1,2));
buttonPanel.add(simulate);
buttonPanel.add(clear);
// Set up and show the frame.
contentPane.add(mainPanel, "North");
contentPane.add(buttonPanel, "Center");
contentPane.add(resultPanel, "South");
displayFrame.pack();
displayFrame.setVisible(true);
}
}
//---------------------------------------------------------------------
// SimulationApp.java by Dale/Joyce/Weems Chapter 5
//
// Simulates customers waiting in queues. Customers always enter
// the shortest queue.
//
// Input consists of customer information:
// Minimum and maximum customer inter-arrival time.
// Minimum and maximum customer service time.
// Followed by a sequence of simulation instance information:
// Number of queues and customers.
//
// Output includes, for each simulation instance:
// The average waiting time for a customer.
//----------------------------------------------------------------------
import java.util.Scanner;
public class SimulationApp
{
public static void main(String[] args)
{
Scanner conIn = new Scanner(System.in);
int minIAT; // minimum inter-arrival time
int maxIAT; // maximum inter-arrival time
int minST; // minimum service time
int maxST; // maximum service time
int numQueues; // number of queues
int numCust; // number of customers
String skip; // skip end of line after reading an integer
String more = null; // used to stop or continue processing
// Get customer information
System.out.print("Enter minimum inter-arrival time: ");
minIAT = conIn.nextInt();
System.out.print("Enter maximum inter-arrival time: ");
maxIAT = conIn.nextInt();
System.out.print("Enter minimum service time: ");
minST = conIn.nextInt();
System.out.print("Enter maximum service time: ");
maxST = conIn.nextInt();
// create object to perform simulation
Simulation sim = new Simulation(minIAT, maxIAT, minST, maxST);
do
{
// Get next simulation instance to be processed.
System.out.print("Enter number of queues: ");
numQueues = conIn.nextInt();
System.out.print("Enter number of customers: ");
numCust = conIn.nextInt();
skip = conIn.nextLine(); // skip end of line
// run simulation and output average waiting time
sim.simulate(numQueues, numCust);
System.out.println("Average waiting time is " + sim.getAvgWaitTime());
// Determine if there is another simulation instance to process
System.out.println();
System.out.print("Evaluate another simulation instance? (Y=Yes): ");
more = conIn.nextLine();
System.out.println();
}
while (more.equalsIgnoreCase("y"));
System.out.println("Program completed.");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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