Representation Invariants and Abstraction Functions

The goal for this recitation is to provide practice in using (and then inventing) Rep Invariants and Abstraction Functions for collection classes.

Priority Queue questions

The first activities this week concern the PriorityQueue class file which was used in PS2. This code uses a complicated data structure and algorithm (it is called a "heap" and it is covered in 6.046). You do not need to understand the algorithm or the code for this recitation. However if you want to look the Java source is available.

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

The abstraction function is that

Note that this definition only makes sense for some representations, those which satisfy the first part of the rep invariant (otherwise several different i could be chosen for the same x, giving several different values for priority(x))

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

Card Sets

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.