Professional Documents
Culture Documents
CS1316: Representing
Structure and Behavior
Story
A Queue
Key ideas:
The current time is then that time, the time that that
event is supposed to occur.
Key:
Key:
Key Ideas
A Queue
List
I got here
second!
I got here
first!
This is the
front or head
of the queue
First-in-First-out
New
I got here
third!
I got here
second!
I got here
first!
This is the
front or head
of the queue
I got here
second!
Now, this is
the front or
head of the
queue
I got here
first! AND
NOW IM UP!
Served!
I got here
fourth!
Now, this is
the tail of the
queue
I got here
third!
I got here
second!
Now, this is
the front or
head of the
queue
Building a Queue
> Queue line = new Queue();
> line.push("Fred");
> line.push("Mary");
> line.push("Jose");
> line.size()
3
Accessing a Queue
> line.peek()
"Fred"
> line.pop()
"Fred"
> line.peek()
"Mary"
> line.pop()
"Mary"
> line.peek()
"Jose"
> line.pop()
"Jose"
> line.pop()
java.util.NoSuchElementException:
We dont really
want to peek()
or pop() an
empty queue,
so we should
probably check
its size first.
Building a
Queue
import java.util.*; // LinkedList representation
/**
* Implements a simple queue
**/
public class Queue {
/** Where we'll store our elements */
public LinkedList elements;
/// Constructor
public Queue(){
elements = new LinkedList();
}
Queue methods
/// Methods
/** Push an object onto the Queue */
public void push(Object element){
elements.addFirst(element);
}
/** Peek at, but don't remove, top of queue */
public Object peek(){
return elements.getLast();}
/** Pop an object from the Queue */
public Object pop(){
Object toReturn = this.peek();
elements.removeLast();
return toReturn;
}
/** Return the size of a queue */
public int size() { return elements.size();}
Array-oriented Queue
/**
* Implements a simple queue
**/
public class Queue2 {
private static int ARRAYSIZE = 20;
/** Where we'll store our elements */
private Object[] elements;
/** The indices of the head and tail */
private int head;
private int tail;
Queue2
methods
Same methods,
same behavior
But can only handle up
to 20 elements in the
queue! Less if pushing
and popping. Could shift
elements to always
allow 20.
Not as good an
implementation as the
linked list
implementation. (But
uses less memory.)
Welcome to DrJava.
> Queue2 line = new Queue2();
> line.push("Mary")
> line.push("Kim")
> line.push("Ron")
> line.peek()
"Mary"
> line.pop()
"Mary"
> line.peek()
"Kim"
> line.size()
2
> line.pop()
"Kim"
> line.pop()
"Ron"
Visualizing
a uniform
distribution
By writing out a
tab and the
integer, we dont
have to do the
string conversion.
}
}
A Uniform Distribution
A Normal
Distribution
// Fill it with 500 numbers between -1.0 and 1.0, normally distributed
for (int i=0; i < 500; i++){
try{
output.write("\t"+rng.nextGaussian());
output.newLine();
} catch (Exception ex) {
System.out.println("Couldn't write the data!");
System.out.println(ex.getMessage());
}
}
time
Exercising
an EventQueue
Implementing an EventQueue
import java.util.*;
/**
* EventQueue
* It's called an event "queue," but it's not really.
* Instead, it's a list (could be an array, could be a linked list)
* that always keeps its elements in time sorted order.
* When you get the nextEvent, you KNOW that it's the one
* with the lowest time in the EventQueue
**/
public class EventQueue {
private LinkedList elements;
/// Constructor
public EventQueue(){
elements = new LinkedList();
}
Were
Insertion
Sort
public void sort(){
// Perform an insertion sort
// For comparing to elements at smaller indices
SimEvent considered = null;
SimEvent compareEvent = null; // Just for use in
loop
// Smaller index we're comparing to
int compare;
// Start out assuming that position 0 is "sorted"
// When position==1, compare elements at
indices 0 and 1
// When position==2, compare at indices 0, 1,
and 2, etc.
for (int position=1; position < elements.size();
position++){
considered = (SimEvent) elements.get(position);
// Now, we look at "considered" versus the
elements
// less than "compare"
compare = position;
/PLDS210/sorting.html
http://www.cs.ubc.ca/spider/harrison/Jav
a/sorting-demo.html
http://www.cs.brockport.edu/cs/java/apps/
sorters/insertsort.html
Recommended
insertInOrder()
/**
* Put thisEvent into elements, assuming
* that it's already in order.
**/
public void insertInOrder(SimEvent
thisEvent){
SimEvent comparison = null;
// Have we inserted yet?
boolean inserted = false;
for (int i=0; i < elements.size(); i++){
comparison = (SimEvent)
elements.get(i);
Running a DESimulation
Welcome to DrJava.
> FactorySimulation fs = new
FactorySimulation();
> fs.openFrames("D:/temp/");
> fs.run(25.0)
Distributor: 0
Distributor: 0
Arrived at warehouse
is blocking
Distributor: 3
Distributor: 3
Arrived at warehouse
is blocking
Distributor: 4
Distributor: 4
Arrived at warehouse
is blocking
Distributor: 2
Distributor: 2
Arrived at warehouse
is blocking
Distributor: 1
Distributor: 1
Arrived at warehouse
is blocking
Notice that
time 2 never
occurs!
Truck: 2
Arrived at warehouse with load
13
Distributor: 0
unblocked!
Distributor: 0
Gathered product for orders of
11
Truck: 0
Arrived at warehouse with load
18
Distributor: 3
unblocked!
Distributor: 3
Gathered product for orders of
12
Distributor: 0
Arrived at market
Truck: 1
Arrived at warehouse with load
20
Distributor: 4
unblocked!
Distributor: 4
Gathered product for orders of
19
Subtract the time that they unblock from the time that
they block
At each time a distributor leaves, figure out how much
is left in the warehouse.
Try it!
Its a time, an
Agent, and an
integer that the
Agent will
understand as a
message
An Example: A Truck
/**
* Truck -- delivers product from Factory
* to Warehouse.
**/
public class Truck extends DEAgent {
/////// Constants for Messages
public static final int FACTORY_ARRIVE = 0;
public static final int WAREHOUSE_ARRIVE = 1;
////// Fields /////
/**
* Amount of product being carried
**/
public int load;
What Resources do
Setting up
the
Factory
Simulation
line.
EventQueues: For storing the events
scheduled to occur.
LinkedList: For storing all the agents.