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

a. Write the definition of the functions setWaitingTime, getArrivalTime, getTran

ID: 3673373 • Letter: A

Question

a. Write the definition of the functions setWaitingTime, getArrivalTime, getTransactionTime, and getCustomerNumber of the class customerType defined in the section, "Application of Queues: Simulation"

b. Write the definition of the functions getRemainingTransactionTime, setCurrentCustomer, getCurrentCustomer, getCurrentCustomerArrivalTime, getCurrentCustomerWaitingTime, and getCurrentCustomerTransactionTime of the class serverType defined in the section, "Appliction fo Queues: Simulation"

c. Write the definition of the function runSimulation to complete the design of the computer simulation program (see the section, "Application of Queues: Simulation"). Test run your program for a variety of data. Moreover, use a random number generator to decide whether a customer arrived at a given time unit.

Explanation / Answer


import java.io.IOException;
import java.util.Scanner;

public class CustomerServicingSimulation
{
   private static int simulationTime;
   private static int numberOfServers;
   private static int transactionTime;
   private static int timeBetweenCustomerArrival;
  
   public static void main(String[] args) throws IOException
   {
       setSimulationParameters();
       runSimulation();
   }
      
   public static void setSimulationParameters()
   {
       Scanner userChoice = new Scanner(System.in);
      
       //   Create a header when running program
       System.out.println(" ----------------------------------------------------------------------------------------------------------");
       System.out.println("                                 Customer Servicing Simulation Program");
       System.out.println("---------------------------------------------------------------------------------------------------------- ");
      
       //   ENTER SIMULATION TIME
       //   -----------------------------------------------------------------------------------------------------------------------------
       //   Read in and store the simulation time.
       System.out.print(" Please enter the Simulation Time: ");
      
       //   Validating input from user
       while (!userChoice.hasNext("[0-9]+"))
       {
           System.out.println(" Error! Enter only integers.");
           System.out.print(" Please enter the Simulation Time: ");
           userChoice.next();
       }
       simulationTime = Integer.parseInt(userChoice.next());
      
       //   ENTER NUMBER OF SERVERS  
       //   -----------------------------------------------------------------------------------------------------------------------------  
       //   Read in and store the number of servers.
       System.out.print(" Please enter the number of Servers: ");
      
       //   Validating input from user
       while (!userChoice.hasNext("[0-9]+"))
       {
           System.out.println(" Error! Enter only integers.");
           System.out.print(" Please enter the number of Servers: ");
           userChoice.next();
       }
       numberOfServers = Integer.parseInt(userChoice.next());
      
       //   ENTER TRANSACTION TIME  
       //   -----------------------------------------------------------------------------------------------------------------------------  
       //   Read in and store the transaction time.
       System.out.print(" Please enter the Transaction time: ");
      
       //   Validating input from user
       while (!userChoice.hasNext("[0-9]+"))
       {
           System.out.println(" Error! Enter only integers.");
           System.out.print(" Please enter the Transaction time: ");
           userChoice.next();
       }
       transactionTime = Integer.parseInt(userChoice.next());
      
       //   ENTER TIME BETWEEN CUSTOMER ARRIVALS  
       //   -----------------------------------------------------------------------------------------------------------------------------  
       //   Read in and store the time between customer arrivals.
       System.out.print(" Please enter the time between customer arrivals: ");
      
       //   Validating input from user
       while (!userChoice.hasNext("[0-9]+"))
       {
           System.out.println(" Error! Enter only integers.");
           System.out.print(" Please enter the time between customer arrivals: ");
           userChoice.next();
       }
       timeBetweenCustomerArrival = Integer.parseInt(userChoice.next());  
      
       //   Closing Scanner object
       userChoice.close();      
   }
      
   public static boolean isCustomerArrived(double arvTimeDiff)
   {
       double value;
       value = (double)(Math.random());
      
       return (value > Math.exp(- 1.0/arvTimeDiff));
   }
      
   public static void runSimulation()
   {
       ServerList listOfServers = new ServerList(numberOfServers);
       WaitingCustomerQueue queue = new WaitingCustomerQueue();
      
       int customerNumber = 1;
       int totalWaitingTime = 0;
       double averageWaitingTime = 0;
       int customersArrived = 0;
       int customersServed = 0;
       int customersLeftInQueue = 0;
       int customersLeftInServers = 0;
      
       //   Empty space before results
       System.out.println(" ---------------------------------------------------------------------------------------------------------- ");
      
       for (int clock = 1; clock <= simulationTime; clock++)
       {
           //   Update the server list to decrement the transaction time of each busy server by one time unit.
           listOfServers.updateServers();
          
           //   If the customers queue is nonempty, increment the waiting time of each customer by one time unit.
           if (!queue.isEmpty())
           {
               queue.updateWaitingQueue();
           }
          
           if (isCustomerArrived(timeBetweenCustomerArrival))
           {
               //   A customer just arrives, increment the number of customers by 1 and add the new
               //   customer to the queue.
               customersArrived++;
               queue.enqueue(new Customer(customerNumber, clock, 0, transactionTime));
              
               //   Output the following message to the screen:
               System.out.println("Customer number " + customerNumber + " arrived at time unit " + clock);
               customerNumber++;
           }
          
           //   If a server is free and the customers queue is nonempty then remove a customer from the
           //   front of the queue and send the customer to the free server.
           if (listOfServers.getFreeServerID() > -1 && queue.isEmpty() == false)
           {
               customersServed++;
               totalWaitingTime += (clock - ((Customer)queue.peekFront()).getArrivalTime());
               listOfServers.setServerBusy(listOfServers.getFreeServerID(), ((Customer)queue.peekFront()));
               queue.dequeue();
           }
       }
      
       customersLeftInServers = listOfServers.getNumberOfBusyServers();
       customersLeftInQueue = queue.getQueueElements();
       averageWaitingTime = ((double)totalWaitingTime / customersServed);
      
      
       System.out.println(" ----------------------------------------------------------------------------------------------------------");
       System.out.println("Simulation ran for " + simulationTime + " time units.");              
       System.out.println("Number of servers: " + numberOfServers);
       System.out.println("Average transaction time: " + ((customersArrived * transactionTime) / customersArrived));
       System.out.println("Average arrival time difference between customers: " + ((customersArrived * timeBetweenCustomerArrival) / customersArrived));
       System.out.println("Total wait time of all customers: " + totalWaitingTime);
       System.out.println("Number of customers who arrived: " + customersArrived);      
       System.out.println("Number of customers who completed a transaction: " + (customersServed - customersLeftInServers));
       System.out.println("Number of customers left in the servers: " + customersLeftInServers);
       System.out.println("Number of customers left in the queue: " + customersLeftInQueue);
       System.out.printf("Average wait time: %.2f", averageWaitingTime);
       System.out.println(" ----------------------------------------------------------------------------------------------------------");

       //   Create a footer when stopping program
       System.out.println(" ----------------------------------------------------------------------------------------------------------");
       System.out.println("                               Simulation terminated... Thank you!                               ");
       System.out.println("----------------------------------------------------------------------------------------------------------");
   }
}

Server.java


public class Server
{
   private Customer currentCustomer;
   // Value of 'status' is free or busy
   private String status;
   private int transactionTime;
  
   //   Default constructor
   public Server()
   {
       status = "free";
       currentCustomer = new Customer();
       transactionTime = 0;
   }
  
   //   Method to determine whether a server is free.
   public boolean isFree()
   {
       return (status == "free") ? true : false;
   }
  
   //   Method to set the status of a server to "busy".
   public void setBusy()
   {
       status = "busy";
   }
  
   //   Method to set the status of a server to "free".
   public void setFree()
   {
       status = "free";
   }
  
   //   Method to set the transaction time according to the parameter t.
   public void setTransactionTime(int t)
   {
       transactionTime = t;
   }
  
   //   Method to set the transaction time according to customers time.
   public void setTransactionTime()
   {
       transactionTime = currentCustomer.getTransactionTime();
   }
  
   //   Method to return the remaining transaction time.
   public int getRemainingTransactionTime()
   {
       return transactionTime;
   }
  
   //   Method to decrease the transaction time by 1.
   public void decreaseTransactionTime()
   {
       transactionTime--;
   }
  
   //   Method to set the current customer info according to cCustomer.
   public void setCurrentCustomer(Customer cCustomer)
   {
       currentCustomer.setCustomerInfo(cCustomer.getCustomerNumber(), cCustomer.getArrivalTime(), cCustomer.getWaitingTime(), cCustomer.getTransactionTime());
   }
  
   //   Method to return the customer number of the current customer.
   public int getCurrentCustomerNumber()
   {
       return currentCustomer.getCustomerNumber();
   }
  
   //   Method to return the arrival time of the current customer.
   public int getCurrentCustomerArrivalTime()
   {
       return currentCustomer.getArrivalTime();
   }
  
   //   Method to return the current waiting time of the current customer.
   public int getCurrentCustomerWaitingTime()
   {
       return currentCustomer.getWaitingTime();
   }
  
   //   Method to return the transaction time of the current customer.
   public int getCurrentCustomerTransactionTime()
   {
       return currentCustomer.getTransactionTime();
   }
}


Customer.java

public class Customer extends DataElement
{
   private int customerNumber;
   private int arrivalTime;
   private int waitingTime;
   private int transactionTime;
  
   //   Default constructor
   public Customer()
   {
       customerNumber = 0;
       arrivalTime = 0;
       waitingTime = 0;
       transactionTime = 0;
   }
  
   //   Constructor to initialize the data members
   public Customer(int custN, int aTime, int wTime, int tTime)
   {
       customerNumber = custN;
       arrivalTime = aTime;
       waitingTime = wTime;
       transactionTime = tTime;
   }
  
   //   Method to set the data members according to the parameters
   public void setCustomerInfo(int custN, int aTime, int wTime, int tTime)
   {
       customerNumber = custN;
       arrivalTime = aTime;
       waitingTime = wTime;
       transactionTime = tTime;
   }
  
   //   Method to return the waiting time of a customer.
   public int getWaitingTime()
   {
       return waitingTime;
   }
  
   //   Method to set the waiting time of a customer.
   public void setWaitingTime(int time)
   {
       waitingTime = time;
   }
  
   //   Method to increment the waiting time.
   public void incrementWaitingTime()
   {
       waitingTime++;
   }
  
   //   Method to return the arrival time of a customer.
   public int getArrivalTime()
   {
       return arrivalTime;
   }
  
   //   Method to return the transaction time of a customer.
   public int getTransactionTime()
   {
       return transactionTime;
   }
  
   //   Method to return the customer number.
   public int getCustomerNumber()
   {
       return customerNumber;
   }
  
   //   Implement equals method provided by DataElement class
   public boolean equals(DataElement otherElement)
   {
       if (!(otherElement instanceof Customer) || otherElement == null)
       {
           return false;
       }
      
       else if (this == ((Customer)otherElement))
       {
           return true;
       }
      
       else
       {
           return   customerNumber == (((Customer)otherElement).getCustomerNumber()) &&
                   arrivalTime == (((Customer)otherElement).getArrivalTime()) &&
                   waitingTime == (((Customer)otherElement).getWaitingTime()) &&
                   transactionTime == (((Customer)otherElement).getTransactionTime());
       }
   }
  
   //   Implement compareTo method provided by DataELement class
   public int compareTo(DataElement otherElement)
   {
       return (arrivalTime - ((Customer)otherElement).arrivalTime);
   }
  
   //   Copy Customer information from otherElement
   public void makeCopy(DataElement otherElement)
   {
       customerNumber = ((Customer)otherElement).customerNumber;
       arrivalTime = ((Customer)otherElement).arrivalTime;
       waitingTime = ((Customer)otherElement).waitingTime;
       transactionTime = ((Customer)otherElement).transactionTime;
   }
  
   //   Return a copy of Customer object
   public DataElement getCopy()
   {
       DataElement element = new Customer(customerNumber, arrivalTime, waitingTime, transactionTime);
      
       return element;
   }
}

WaitingCustomerQueue.java


public class WaitingCustomerQueue extends CirArrayQueue
{
   //   Default constructor: this will call CirArrayQueue no-args constructor
   public WaitingCustomerQueue() {}
  
   //   Constructor with parameter queue size
   public WaitingCustomerQueue(int size)
   {
       super(size);
   }
  
   //   Copy constructor
   public WaitingCustomerQueue(WaitingCustomerQueue otherQ)
   {
       super(otherQ);
   }
  
   //   Method to increment the waiting time of each customer in the queue by one time unit.
   //   Postcondition: The waiting time of each customer in the queue is incremented by one time unit.
   public void updateWaitingQueue()
   {
       super.incrementQueueWaitingTime();
   }
  
   //   Method to get number of elements in queue
   public int getQueueElements()
   {
       return super.getCount();
   }
}
CirArrayQueue.java

public class CirArrayQueue
{
   private int maxQueueSize;
   // Number of elements in the queue
   private int count;
   private int queueFront;
   private int queueRear;
   //   Array of references to the objects that store queue elements
   private DataElement[] list;

   //   Default constructor, creates a queue of default size 100
   public CirArrayQueue()
   {
       list = new DataElement[100];
       maxQueueSize = 100;
       queueFront = 0;
       queueRear = 0;
       count = 0;
   }
  
   //   Constructor with a parameter
   public CirArrayQueue(int queueSize)
   {
       list = new DataElement[queueSize];
       maxQueueSize = queueSize;
       queueFront = 0;
       queueRear = 0;
       count = 0;
   }
  
   //   Copy constructor
   public CirArrayQueue(CirArrayQueue otherQueue)
   {
       maxQueueSize = otherQueue.maxQueueSize;
       list = new DataElement[maxQueueSize];

       for (int i = 0; i < maxQueueSize; i++)
       {
           if (otherQueue.list[i] != null)
           {
               list[i] = otherQueue.list[i];
           }
       }
      
       queueFront = otherQueue.queueFront;
       queueRear = otherQueue.queueRear;
       count = otherQueue.count;
   }
  
   //   Method to initialize the queue to an empty state.
   public void initializeQueue()
   {
       for (int i = 0; i < list.length; i++)
       {
           list[i] = null;
       }

       queueFront = 0;
       queueRear = 0;
       count = 0;

   }
  
   //   Method to determine whether the queue is empty.
   public boolean isEmpty()
   {
       return (queueFront == queueRear || count == 0) ? true : false;
   }
  
   //   Method to determine whether the queue is full.
   public boolean isFull()
   {
       return ((queueRear - queueFront) == -1 || (queueRear - queueFront) == (maxQueueSize - 1) || count == maxQueueSize) ? true : false;
   }
  
   //   Method to return the first element of the queue.
   public DataElement peekFront()
   {
       if (!isEmpty() && list[queueFront] != null)
       {
           return list[queueFront];
       }
      
       else
       {
           System.out.println("***Sorry: the queue is actually empty!");
           return null;
       }
   }
  
   //   Method to return the last element of the queue.
   public DataElement peekRear()
   {
       if (!isEmpty() && list[queueRear] != null)
       {
           return list[queueRear];
       }
      
       else
       {
           System.out.println("***Sorry: the queue is actually empty!");
           return null;
       }
   }
  
   //   Method to add queueElement to the rear of the queue.
   public void enqueue(DataElement queueElement)
   {
       if (isFull())
       {
           System.out.println("***Sorry: the queue is actually full. Cannot add more customers!");
       }
      
       else
       {
           list[queueRear] = queueElement;
           count++;
          
           if (count != maxQueueSize)
           {
               queueRear = (queueRear + 1) % maxQueueSize;
           }
       }
   }
  
   //   Method to remove the first element of the queue.
   public void dequeue()
   {
       if (isEmpty())
       {
           System.out.println("***Sorry: the queue is actually empty!");
       }
      
       else
       {
           list[queueFront] = null;
           count--;
           queueFront = (queueFront + 1) % maxQueueSize;
       }  
   }
  
   //   Method to make a copy of otherQueue.
   public void copyQueue(CirArrayQueue otherQueue)
   {
       CirArrayQueue copy = new CirArrayQueue(otherQueue);
       System.out.println("This copied queue have " + copy.count + " customers.");
   }
  
   //   Method to increment waiting of elements in queue
   public void incrementQueueWaitingTime()
   {
       if (!isEmpty())
       {
           for (int i = 0; i < maxQueueSize; i++)
           {
               if (list[i] == null)
               {
                   continue;
               }
              
               else
               {
                   ((Customer)list[i]).incrementWaitingTime();
               }
           }
       }
   }
  
   //   Method to get number of elements in queue
   public int getCount()
   {
       return count;
   }
}

ServerList.java


public class ServerList
{
   private int numOfServers;
   private Server[] servers;
  
   //   Default constructor to initialize a list of servers
   public ServerList()
   {
       numOfServers = 1;
       servers = new Server[numOfServers];
   }
  
   //   Constructor to initialize a list of servers specified by num.
   public ServerList(int num)
   {
       numOfServers = num;
       servers = new Server[numOfServers];
      
       for (int i = 0; i < num; i++)
       {
           servers[i] = new Server();
       }
   }
  
  
   //   Method to search the list of servers for a free server, return the ID of a
   //   free server if found, else return -1.
   public int getFreeServerID()
   {
       int id = -1;
      
       for (int i = 0; i < servers.length; i++)
       {
           if (servers[i] == null)
           {
               continue;
           }
          
           else if (servers[i].isFree() == true)
           {
               id = i;
           }
       }

       return id;
   }
  
   //   Method to return the number of busy servers.
   public int getNumberOfBusyServers()
   {
       int busy = 0;
      
       for (int i = 0; i < servers.length; i++)
       {
           if (servers[i] == null)
           {
               continue;
           }
          
           else if (servers[i].isFree() == false)
           {
               busy++;
           }
       }
      
       return busy;
   }
  
  
   //   Method to set a server to "busy".
   public void setServerBusy(int serverID, Customer cCustomer, int tTime)
   {
       servers[serverID].setBusy();
       servers[serverID].setCurrentCustomer(cCustomer);
       servers[serverID].setTransactionTime(tTime);
   }
  
   public void setServerBusy(int serverID, Customer cCustomer)
   {
       servers[serverID].setBusy();
       servers[serverID].setCurrentCustomer(cCustomer);
       servers[serverID].setTransactionTime(cCustomer.getTransactionTime());
      
   }
  
  
   //   Method to update the transaction time of each busy server.
   public void updateServers()
   {
       for (int i = 0; i < servers.length; i++)
       {
           if (servers[i] == null)
           {
               continue;
           }
          
           else if (servers[i].isFree() == false && servers[i].getRemainingTransactionTime() > 0)
           {
               servers[i].decreaseTransactionTime();
              
               if (servers[i].getRemainingTransactionTime() == 0)
               {
                   servers[i].setFree();
                   System.out.println("Server number " + (i + 1) + " |--> Customer number " + servers[i].getCurrentCustomerNumber()
                           + " departed at clock unit " + (servers[i]. getCurrentCustomerArrivalTime() + servers[i].getCurrentCustomerTransactionTime() + servers[i].getCurrentCustomerWaitingTime()));
               }
           }
       }
   }
}

DataElement.java

public abstract class DataElement
{
   //   Method to determine whether two objects contain the same data.
   public abstract boolean equals(DataElement otherElement);
  
  
   //   Method to compare two objects.
   public abstract int compareTo(DataElement otherElement);
   //   Method to copy otherElement into this object.
   public abstract void makeCopy(DataElement otherElement);
  
   //   Method to return a copy of this object.
   public abstract DataElement getCopy();
}

output


Customer Servicing Simulation Program
Please enter the Simulation Time: 2                                                                                                                          
                                                                                                                                                             
Please enter the number of Servers: 2                                                                                                                        
                                                                                                                                                             
Please enter the Transaction time: 3                                                                                                                         
                                                                                                                                                             
Please enter the time between customer arrivals: 2                                                                                                           
                                                                                                                                                             
----------------------------------------------------------------------------------------------------------                                                   
                                                                                                                                                             
                                                                                                                                                             
----------------------------------------------------------------------------------------------------------                                                   
Simulation ran for 2 time units.                                                                                                                             
Number of servers: 2                                                                                                                                        

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote