The goal for this recitation is to provide practice in using (and then inventing) Rep Invariants and Abstraction Functions for collection classes.
The specification fields are elements (a set) and priority (a function from objects to doubles). The representation fields are a Vector heap and a HashSet elts. Each component of heap (except the first) is of type Entry, with fields key and value.
The representation invariant is
for all i and j with 1<=i<j<heap.size():Note: this does not cover i=0 or j=0
heap.elems[i].value != heap.elems[j].value
for every x, x is in elts if and only if there exists i with 1 <= i < heap.size() and x = heap.elems[i].value
heap.size() >= 1
The abstraction function is that
In writing out descriptions of this data structure, we will use a textual notation. Here we write the entry y as a pair (y.key, y.value), we write the Vector as an array [e1, e2, e3...], and we use distinct symbols o1, o2 , o3 for distinct objects
heap = [o1, (3.0, o2), (6.0, o3), (4.0, o4), (8.0, o5), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o2, o3, o4, o5, o6, o7, o8},
heap = [o1, (3.0, o2), (6.0, o3), (4.0, o4), (8.0, o5), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o3, o4, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (4.0, o4), (8.0, o5), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o3, o4, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (4.0, o4), (8.0, o5), (10.0, o6), (7.0, o7)]
elts = {o3, o4, o5, o6, o7, o8},
heap = [o1, (4.0, o4), (6.0, o3), (5.0, o8), (8.0, o5), (10.0, o6), (7.0, o7)]
elts = {o3, o4, o5, o6, o7, o8},
heap = [o1, (4.0, o4), (6.0, o3), (5.0, o8), (8.0, o5), (10.0, o6), (7.0, o7)]
elts = {o3, o4, o5, o6, o7, o8},
* @returns elt such that elt is in this.elements
* && for all elt2 in this.elements, priority(elt) <= priority(elt2)
* @effects removes elt from this.elements and from the domain of priority
* @throws NoSuchElementException if this.elements is empty
heap = [o1, (4.0, o4), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o2, o3, o4, o5, o6, o7, o8},
heap = [o1, (4.0, o4), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o2, o3, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7), (5.0, o8)],
elts = {o2, o3, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7)],
elts = {o2, o3, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7)],
elts = {o2, o3, o5, o6, o7, o8},
heap = [o1, (5.0, o8), (6.0, o3), (8.0, o5), (3.0, o2), (10.0, o6), (7.0, o7)],
elts = {o2, o3, o5, o6, o7, o8},
The next activities are concerned with a class Hand, representing a set elements of Cards; recall that a set has no duplicates. Each Card c has c.rank which is a number (we treat Jack as 11, Queen as 12, King as 13 and Ace as 14) and c.suit which is a character (C for Clubs, D for Diamonds, H for Hearts, S for Spades). For simplicity we have a textual description of each object of type Card by a String such as "S12" the Queen of Spades, or "H6" for the six of Hearts.
Consider the method extractHighestRank(char c) which is specified abstractly by
* @returns Card elt such that elt is in this.elements,
* && elt.suit==c
* && for all elt2 in this.elements,
* if elt2.suit==c
* then elt.rank >= elt2.rank
* @effects removes elt from this.elements and from the domain of priority
* @throws NoSuchElementException if
* for all elt2 in this.elements
* elt2.suit != c
Write code to perform this method.