Warning This class is being recorded (and transcribed) (assuming Teams succeeds).
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
How should we test our code for MP9?
Pick a variety of types of legal JSON and feed them in to the system and then verify that it gives the correct object structure.
Feed in legal JSON, convert back to a JSON string (with
writeJSONor something like that ), compare.
For writeJSON, do you care what method we use to generate output?
Nope.
For JSONHash, why are we building an iterator?
It’s good practice to build iterators (or iterables) for each collection class you write.
For a hash table, the most natural iterator is one that iterates over the key/value pairs.
We often also want to write iterators for just the keys and just the values.
“Get all the elements of this collection” is a standard request from any collection. In 151, that was probably a list. In 161, it was probably an array. In 207, it’s an Iterator (or Iterable). The last is nice because it requires less extra memory.
How do I compute the hashCode of a hash table?
It’s up to you. “Sum of hashCode of individual pairs” is one approach, but not great.
You could also use the underlying
hashCodeof the underlying array/ArrayList.
You could use the ideas from PM’s chapter.
Do we have to check for null in our constructors and/or in the
individual methods we write for the various JSON types?
No
You provided JSONConstant. Why did you do something weird in the
equals method like compare the contents field to another object?
For constants, it made sense to do so.
So should we do the same thing for strings and such?
It’s up to you.
Are there complexities in dealing with hashes?
Yes, they may not have the same capacities but still have the same content; you should probably check for that situation.
How do we write an iterator for hash tables?
TPS.
Option one: For probed hash tables
Sam would probably have an integer that keeps track of the index of the next cell we might look at.
hasNextwill see if there’s a value in that cell. If so, return true. If not, advance until you (a) find a non-empty cell or (b) run off the end of the array. In case (a), you return true. In case (b), you return false.
nextdoes something similar, except that it throws an exception in the case that it runs off the end of the array. It also adds one to the position for the next go round.
public KVPair<K,V> next() { if (this.hasNext()) { return JSONHash.this.values[pos++]; } else { throw new NoSuchElementException("boom!"); } // if/else } // next()
Option two: For chained hash tables
Similar to the probed one. However, when you reach a bucket, you’ll need to loop through the bucket/array/ArrayList at the given index.
We could use a second cursor.
We could also grab an iterator for the bucket.
General structure
``` public Iterator<KVPair<JSONString,JSONValue» iterator() { return new Iterator<KVPair<JSONString,JSONValue»() { /** The position in the underlying array. */ int pos = 0;
public boolean hasNext() {
...
} // hasNext()
public boolean next() {
...
} // next()
}; } // iterator() ```
Could you tell the infamous “Grading in SamR’s first Data Structures and Algorithms in Java” story?
Sure.
Some sample problems:
Abstraction
A graph is a collection of vertices and edges that connect them. CLRS write this as $(V,E)$.
A directed weighted graph $G$, is a pair of sets, $(V,E)$ in which
Yay! It’s time for PUM!
Focus on the basics; the things like cons, car, and cdr that
we’d need to implement the more complex algorithms we’re going to
design.
Note: The plural is VERTICIES, the singular is VERTEX. “VERTISEE” is an abomination.
public Graph(String initialVertex).public Graph().void addEdge(String from, String to, double weight). This should also add the vertices if they are not already in the graph. Throw an error if’s already there. Or replace it if it’s already there (obviating the need for changeWeight).Whoops. We didn’t name edges.
renameVertex(String old, String new). (Mostly for
UI issues.)removeEdge(String from, String to). Throws
an exception if the edge does not already exist.changeWeight(String from, String to).hasEdge(String from, String to).
edgeWeight(String from, String to) throws ExceptionisEmpty().
numVerticies() and numEdges()int numEdgesFrom(String vertex)
Iterator<Edge> edgesFrom(String vertex)Iterable<Edge> edgesFrom(String vertex)
for (Edge e: g.edgesFrom("start"))Edge class, compareTo(Edge other).Yay! It’s time for LIA! (Layout, Implementation, Analysis)
TPS: How would you store a graph?