Stuff you want to cover on Wednesday, September 20
Monday, September 18 pre-lecture 11 Q&A
Casting and Dynamic Method Selection (DMS)
When we press "play" on a Java program, two separate steps happen:
Compilation (all errors here are compiler errors):
Casting and Dynamic Method Selection (DMS)
When we press "play" on a Java program, two separate steps happen:
Compilation (all errors here are compiler errors):
Casting and Dynamic Method Selection (DMS)
When we press "play" on a Java program, two separate steps happen:
Runtime (all errors here are Runtime Errors)
Note: The hardest parts of DMS are considered out of scope for this class. Discussion teaches a single-pass approach with a flowchart, while we show here a two-pass approach (compiler, then runtime). In general, the single pass is wrong on a few edge cases, while the two-pass approach works always. I believe that the flowchart yields the same results as the two-pass approach on any example we consider in scope. But this is closer to what actually happens.
Example
A List of Lists
Two main types:
Linked List:
A List of Lists
Two main types:
Linked List:
Array List:
A List of Lists
List-like Interfaces (Not all in scope yet):
Example (+ How to approach Skeleton Code)
Higher Order Functions
Unlike Python, Java has no native support for HOFs (at least the version we teach; later versions introduce lambdas).
Instead, we create a "dummy" class whose sole purpose is to run a function.
Ex. We create a class UnaryFunction<K,V> that has one method: public V apply(K input)
To create the equivalent of Python's lambda x: x*x, we make a class SquareFunction implements UnaryFunction<Integer, Integer> with the apply function: public Integer apply(Integer x) {return x*x;}
Two main types of HOFs we talk about: Comparators and Iterators
Comparators
Designed to compare two values: either a < b, a = b, or a > b.
Two comparison-based interfaces:
Comparable<T> if you want to compare to a particular type
Comparator<T> if you want a machine that can compare two items of a given type
Comparators
In order for comparators to be considered valid, it must follow the following well-ordering principles:
Don't worry too much about this for now, but it'll be relevant once we get into sorts (if these aren't true, there's not necessarily a valid way to sort a set of values)
Iterators
Designed to iterate over a collection of values
Two iteration-based interfaces:
Iterable<T>
Iterator<T>
Example
https://drive.google.com/file/d/1-pPuHGuw0anqwnLh3nc_8z2Alqqcnpi5/view
Note: Iterators are sometimes covered after MT1 in past semesters of 61B.
So they tend to be in MT2, and then used in combination with other MT2 content.