Results 1 to 1 of 1
- 02-15-2008, 04:44 PM #1Member
- Join Date
- Feb 2008
- Rep Power
m trying to write a program that store a binary tree of possible events in an array. i need to be able to sort the the Events in the array based on the previous event that caused it by the time which they will occur. After the specific time has passed the event will be removed and all other events will be bumped up, all the while new events will be added to the end and sorted by their time.
heres wot i've got so far, i've got some test that will test whe the program is doin wot it should. i just dont know how to implement the add(event) and remove(event) methods
// Store a collection of events. The operations are to add an event to the
// collection, or to remove the earliest event from the collection.
// Internally, a priority heap is used to provide an efficient implementation.
// The fields of an EventQueue are...
Event eventqueue = new Event;
// The capacity() method returns the maximum size that the queue can grow
// to without resizing.
public int capicity()
// The size() method returns the number of events currently in the queue.
public int size(int count)
for (int i=0; i<eventqueue.length;i++)
if (eventqueue != null);
count = count+1;
// The add(event) method adds an event to the queue.
public void add(Event event)
// The take() method returns the earliest event in the queue.
// Test the queue.
public static void main(String args)
EventQueue q = new EventQueue();
Event e1 = new Event("One", 1);
Event e2 = new Event("Two", 2);
Event e3 = new Event("Three", 3);
test(q.size(), 3, "The queue should have size 3.");
test(q.capacity() >= 3, true, "The capacity should be >= size.");
test(q.take(), e1, "The earliest event was expected.");
test(q.take(), e2, "The second event was expected.");
test(q.take(), e3, "The third event was expected.");
// Test that an actual value matches an expected value, either of which can
// be null. Print the given message and throw an error if not.
private static void test(Object actual, Object expected, String message)
if (actual == null && expected == null) return;
if (actual != null && actual.equals(expected)) return;
System.err.print(": " + actual);
System.err.println(", not " + expected);
throw new Error("Testing failed.");
- By Java Tip in forum Java TipReplies: 0Last Post: 01-08-2008, 09:04 AM
- By Java Tip in forum Java TipReplies: 0Last Post: 12-30-2007, 12:21 PM
- By kavithas in forum New To JavaReplies: 7Last Post: 12-10-2007, 02:01 PM
- By adlb1300 in forum New To JavaReplies: 2Last Post: 11-19-2007, 01:15 AM
- By luisarca in forum Sun Java Wireless ToolkitReplies: 5Last Post: 05-07-2007, 06:05 PM