Question
JAVA CODE: Need code for the execute method: Variables for the bank model: EList = OList of Event objects (time, type) (ordered time (primary) and
JAVA CODE: Need code for the execute method:
Variables for the bank model:
EList = OList of Event objects (time, type) (ordered time (primary) and type (secondary))
Qcust = Queue2 of Customer objects (a, s)
(a = arrival time, s = service time)
Tasks = stack of Task objects (arr, ser) (arr = arrival time, ser = service time)
Ci = customer at teller i (i = 1, 2, 3)
Bi = 1 if teller i is busy with a customer, 0 otherwise (i= 1, 2, 3)
Cust = next customer to arrive in bank (customer just read from CustArr file)
T = next task to be placed on the Tasks stack (task just read from TaskArr file)
CustArr = file of Customer
TaskArr = file of Task
TaskCurr = current task being worked on by teller 3
tt = throughtime for a customer
TT = total throughtime for all customers
Count (also called n in the example) = number of customers leaving the bank
CURR = current time
Events
- Exit from teller 1
- Exit from teller 2
- Exit from teller 3
- Ignore (Customer quits Qcust and exits bank- this is not implemented yet as an event)
- Task completion by teller 3
- Task arrival
- Customer arrival
The program begins by initializing the variables based on the given initial data.
The program then repeatedly executes the first event on the event list, until the event list is empty. To execute the next event, call the method Execute():
Execute() //method to execute the next event, at time = CURR
Event e = EList.del(EList.getData(1)) //delete and save the first event on event list
CURR = e.getTime()
Type = e.getType()
Now execute this type of event at time CURR, as follows:
To execute a type 1 exit event (customer C1 exits from teller 1 at time CURR):
- Print C1(a, s)
- Compute and print through-time tt = CURR a
- T = T + tt; Count = Count + 1;
- If Qcust is nonempty (Qcust.getSize() > 0), let C1 = Qcust.dequeue(), C1 =(a1, s1).
- Let Event next = Event(CURR + s1, 1) //prepare next exit 1 event
- EList.insert(next) //insert future type 1 exit event into the event list
- B1 = 1 (teller 1 is busy)
Type 2 exit events are done in the same way.
Type 3 exit events are a little different (customer C3 exits from teller 3 at time CURR):
Steps 1-3 are the same as for a type 1 exit event.
(*) If the Tasks stack is empty but Qcust is not empty, then continue as in step 4) above, but applied to C3 = (a3, s3), to prepare the next type 3 exit event. Set B3 = 1.
If the Tasks stack is nonempty, let TaskCurr = Tasks.pop(), TaskCurr = (arr, ser)
- Let Event next = Event(CURR + ser, 5) //prepare next task completion type 5 event
- EList.insert(next) //insert future type 5 event into the event list
- B3 = 1 (teller 3 is busy)
Type 5: Task completion event
- At time CURR, teller 3 completes TaskCurr = (arr, ser). Print the time, type of event, and TaskCurr.
- If the stack is nonempty, let TaskCurr = Tasks.pop(), and suppose TaskCurr = (arrNew, serNew). Prepare a future type 5 task completion event for time CURR + serNew, and insert this future event into the event list; set B3 = 1 (teller 3 is now bust with a new task).
- If the stack is empty and the customer queue is nonempty, then continue as in step (*) of the type 3 event.
Type 6: Task arrival
- At time CURR, task T = (arr, ser) arrives. Push this onto the stack with Tasks.push(T).
- If the stack was previously empty, then T is now the only task on the stack, so if teller 3 is free (B3 = 0), then teller 3 can begin service on this task. In this case, set TaskCurr = Tasks.pop(). Teller 3 will complete service for this task at CURR + ser, so prepare a future type 5 task completion event next = Event(Curr + ser, 5) and insert into the event list. Set B3 = 1 (teller 3 is busy with this new task.)
- If TaskArr (the task arrivals file) is not EOF, read the next task object into T,
T = (arrNew, serNew). Prepare a type 6 task arrival event for time arrNew,
Event next = Event(arrNew, 6), and insert into the event list.
Type 7 Customer arrival
The customer who is arriving is CUST(a, s), the customer last read from the file CustArr.
- Qcust.enqueue(CUST)
- If Qcust.getSize() == 1:
- If some teller is not busy, let i be the smallest number of a free teller.
- Let Ci = Qcust.dequeue(), Ci = (a, s)
- As in the type i exit event, prepare the next type i exit event for time CURR + s, and insert this event into the event list. Set Bi = 1 (busy).
- Read a new value for CUST(a, s) from CustArr (if the file is nonempty)
- Prepare the next Type 7 arrival event for time a and insert the event into the event list.
My Code:
Event.java
public class Event implements Comparable //, PrintValue { private int time, type;
public Event(int timeE, int typeE) { time = timeE; type = typeE; }
public int getTime() { return time; }
public int getType() { return type; }
public void setTime(int timeE) { time = timeE; }
public void setType(int typeE) { type = typeE; }
public String toString(){ return + time + "," + type; }
// public void printVal() // { // System.out.println("Event: " + "time: " + time + " type: " + type); // }
public int compareTo(Event e) { if( (time < e.getTime()) || ((time == e.getTime()) && (type < e.getType()))) { return -1; } else if((time == e.getTime()) && (type == e.getType())) { return 0; } else { return 1; } } }
Node2.java
//Node2.java
public class Node2
//constructor public Node2(T newData, Node2 linkValue) { data = newData; link = linkValue; }
public T getData() { return data; }
public Node2 getLink() { return link; }
public void setData(T y) { data = y; }
public void setLink(Node2 linkValue) { link = linkValue; }
}
Olist.java
//OList.java
//Class to implement the Ordered List ADT for generic objects of type T //The type T extends Comparable so that we can use the compareTo() method
public class OList
//constructor public OList() { start = null; size = 0; }
//copy constructor public OList(OList L) { Node2
public void insert(T y) { Node2
//search for the correct order position of data value y curr = start;//initialize the search at the beginning of the list while((curr != null) && (curr.getData().compareTo(y)<0)) { //if y comes after the current data, update the search save=curr; curr=curr.getLink(); } newnode = new Node2(y,curr);//new node to hold inserted value if(curr==start) { start = newnode; //insertion at the beginning of the list } else { save.setLink(newnode); //previous node points to the new value } size++; }
public void del(T y) //search for first occurance of value y, and remove { Node2
//search for first occurance of y curr = start; while((curr != null) && (curr.getData().compareTo(y) != 0)) { save = curr; curr = curr.getLink(); } if(curr == null) { System.out.println("no deletion: " + y + " not on list"); } else { size--; if(curr == start) { start = curr.getLink();//deletion at the beginning } else { save.setLink(curr.getLink());//delete the current node: //previous node points to following node } } }
public void printListBasic() { Node2
public void printList() { Node2
public int getSize() { return size; }
public Node2 getStart() { return start; }
public T getData(int i) { Node2 } Stack1gen.java //Stack1gen.java //array implementation of stack class public class Stack1gen private int top; private T[] stack; //array to hold the stack data //default constructor public Stack1gen() { top = MAX; //initialize an empty stack stack = (T[]) new Object[MAX]; } //copy constructor // public Stack1(Stack1 s) // { // top = s.top; // for(int i = top; i<=MAX-1; i++) // { // stack[i] = s.stack[i]; // } // } public void push(T y) //push data item y onto the stack { assert(top > 0); //check that there is room on the stack top = top -1; stack[top] = y; } public T pop() //pop the top item from the stack and return it { assert(top < MAX); //check that there is data on the stack T x = stack[top]; top = top +1; return x; } public int getSize() { return MAX-top; } public T getTop() { assert(top < MAX); return stack[top]; } // public void printStack() //print the contents of the stack, from // top to bottom, one item per line, without popping the stack items. }
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