Question: In this lab you will write a program that will simulate the arrival and processing of tasks in a queue over time. The task description
In this lab you will write a program that will simulate the arrival and processing of tasks in a queue over time. The task description will contain the id, arrival time, and transaction time for the task. These will be read in from a le called sim.in. You will have to nd out the average wait times for the tasks from the time they arrive to the time their processing starts. Copy sim.in, Lab.java, TaskQueue.java, Task.java, Node.java, QueueReferenceBased.java, QueueException.java, and QueueInterface.java. In the main function rst a queue is created which is used to store a series of task descriptions from the le sim.in. An instance of a TaskQueue is created which simulates the processing of tasks in a queue. Each task is then inserted into the TaskQueue when that tasks arrival time is reached in the simulation. Each call to the process method of the TaskQueue object simulates one time unit of processing of the task queue and increments the Time variable which is initialized to 0 in the constructor. A new task is enqueued to the task queue when its arrivalTime equals Time. When a task arrives at the front of the queue, i.e., when a task is inserted into an empty queue or the task before it nishes processing at the front of the queue, its startTime is set to Time and the dequeueTime for the task queue is set to the time when this task will be completed (startTime + transactionTime of this task). The wait time for the task is the dierence between its startTime and arrivalTime. When Time equals dequeueTime, the task at the front of the queue is removed.
For each time tick, print out which task, if any, is being processed, started, nished, enqueued. At the end of the simulation, print out the number of tasks processed and the average wait times for all tasks processed. You need to add code only at the places marked with * TO COMPLETE * in the TaskQueue.java le.
NOTE: We believe that it will greatly help your understanding of the lab if you used pen and paper to simulate the arrival and processing of the tasks in sim.in on a timeline, i.e., draw the state of the task queue for each time tick. Increase the number of queues in the TaskQueue class from 1 to 4 and list the average wait time in each case. Use an array of queues and a corresponding array of dequeueTimes. Note that there will still only be a single Time variable, a single tasks variable, and totalWaitTime variable. When there are multiple queues, a newly arriving task should be inserted to one of the queues with minimum number of tasks in it.
Lab.java: Simulating dynamic task arrival and processing in a queue
import java.io.IOException; import java.io.File; import java.util.Scanner;
public class Lab8 { static TaskQueue processingQ; static QueueInterface
scanInput.close(); } catch(IOException e) { System.out.println("Caught IOException: "+ e.getMessage()); } processingQ = new TaskQueue(); while (!Q.isEmpty()){//while more tasks to be added while (!Q.isEmpty() // while more tasks to be added now && processingQ.getTime() == Q.peek().arrivalTime) { processingQ.add(Q.dequeue()); } processingQ.process(); }// all task from the auxiliary queue have been sent to the taskQueue, but they may not have been performed while (!processingQ.isComplete()) // while not all tasks completed processingQ.process(); System.out.println("Number of tasks processed : " + processingQ.getNumTasks() +" " + "Average wait time : " + processingQ.getAvgWaitTime()); } }// end class Lab
Task.java
public class Task { public int id; public int arrivalTime; public int transactionTime; public int startTime; } // end class Task
TaskQueue.java
public class TaskQueue { /** queue of tasks, the task in front of the queue is being processed */ private QueueInterface
/** Constructor */ public TaskQueue(){ Time = 0; dequeueTime =-1; tasks = 0; totalWaitTime =0; Q = new QueueReferenceBased
/** get the current time */ public int getTime(){ return Time; }
/** add a task to the queue @param newTask new task to be handled by the queue. */ public void add(Task newTask){ tasks++; System.out.println(Time+ ": Task " + newTask.id + " enqueued (transaction time=" + newTask.transactionTime+")"); // * TO COMPLETE * // enqueue the newTask // if the new task can start immediately, update the dequeue // time and print out that the task is starting } /** get the number of tasks that have been present in the queue @return number of tasks that entered the queue */ public int getNumTasks(){ return tasks;} /** get the current average wait time, i.e. the average time that a task waited between the moment it entered the queue and the moment when it starts to be processed @return the average waiting time */ public double getAvgWaitTime(){ return (double) totalWaitTime/tasks; }
/** tells whether the queue completed all the current task @return return true when all the current tasks have been performed (i.e. the queue is empty; false otherwise (i.e. the queue is not empty) */ public boolean isComplete(){ return Q.isEmpty(); } /** manage the queue. When needed
sim.in 1 2 2 2 2 1 3 7 1 4 8 3 5 8 2 6 8 5 7 9 1 8 9 4 9 10 2 10 11 3
Node.java
public class Node
public Node(E newItem) { item = newItem; next = null;} // end constructor
public Node(E newItem, Node
public void setItem(E newItem) { item = newItem;} // end setItem
public E getItem() { return item;} // end getItem
public void setNext(Node
public Node
QueueInterface.java
public interface QueueInterface
public boolean isEmpty(); public void enqueue(E newItem) throws QueueException;
public E dequeue() throws QueueException;
public void dequeueAll(); public E peek() throws QueueException; } // end QueueInterface
QueueReferenceBased.java
public class QueueReferenceBased
private Node
/** Determines whether a queue is empty. @return Returns true if the queue is empty; otherwise returns false. */ public boolean isEmpty() { return lastNode == null; } // end isEmpty
/** Removes all items of a queue.
/** Adds an item at the back of a queue.
/** Retrieves and removes the front of a queue. @return If the queue is not empty, the item that was added to the queue earliest is returned and the item is removed. If the queue is empty, the operation is impossible and QueueException is thrown. */ public E dequeue() throws QueueException { if (!isEmpty()) { // queue is not empty; remove front Node
QueueException.java
public class QueueException extends RuntimeException {
public QueueException(String s) { super(s); } // end constructor } // end QueueException
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
