The following is the specification for the PQ priority queue data abstraction

The following is the specification for the pq

This preview shows page 17 - 23 out of 23 pages.

The following is the specification for the PQ(priority queue) data abstraction that stores objects with their associated priority, and allows the objects to be retrieved in priority order. public class PQ { // overview: A priority queue is a collection of object/priority pairs {<o1, // p1>, ..., <on, pn>). public PQ( ) // effects: makes this be an empty priority queue. public void append(Object o, int p) throws BadPriorityException, NullPointerException // modifies: this // effects: if p < 0 or p > 9 throws BadPriorityException else if o = null throws //NullPointerException else adds <o, p> to this. public int highestPriority( ) throws NoElsException // effects: if this is empty throws //NoElsException else // returns the highest priority associated //with an element of this. (i) Identify each operation of the PQ class as a creator, mutator, producer, or observer(ii)Identify the PQ class as mutable or immutable. . 17
Image of page 17
Methods of Object: equals and clone. Two objects are equal if they are behaviorally equivalent Consistency: equality doesn’t change over time if a.equals(b) now then a.equals(b) later Mutable objects are equals only if they are the same objects (otherwise you can change one of them and prove they are not the same) IntSet s=new IntSet (); IntSet t=new IntSet (); If (s.equals(t)) ….; else …. 18
Image of page 18
19
Image of page 19
practical advice immutable objects override equals mutable objects don’t override equals inherit reference equality from Object 20
Image of page 20
21
Image of page 21
} 22
Image of page 22
23 Note that the implementation uses an additional constructor so that it can initialize the newly created object with the right vector (it solve the problem that the cloned object used the same vector); Mutable objects should implement their own cloning operation This constructor is private, it can be called only inside the class. i1 1 3 5 7 9 1 3 5 7 9 private IntSet (Vector v) { els = v; } public Object clone () { return new IntSet ((Vector) els.clone()); IntSet i1 = new IntSet(); IntSet i2 = (IntSet) i1.clone(); i2 }
Image of page 23

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture