Lab 5
OOP with Collections, Iterators, and Iterables
Last updated
Was this helpful?
OOP with Collections, Iterators, and Iterables
Last updated
Was this helpful?
Lab spec:
Welcome to lab 5! I won't be covering Object Oriented Programming or the hierarchical structure of the Java Standard Library in this guide. Instead, I'll be focusing on Iterators and the Iterable interface.
If you're wondering where lab 4 went, there was no lab 4 assignment! The section was converted to project office hours.
A lot of students get confused between Iterable
s and Iterator
s when first seeing them -- I know I was confused too. Let me use an analogy:
Here we have a vase and a hammer. From real world experience, we know that vases are quite fragile -- a property which I will call Breakable. Vases aren't the only Breakable objects, and plates, glass cups, and eggs could all be considered Breakable.
On the other hand, a hammer is an object that breaks Breakable objects. We could call such an object a Breaker. Breakers could also include sledgehammers or wrecking balls.
We observe the relationship between these two types as: Breaker objects break Breakable objects. Similarly, Iterator
objects iterate over Iterable
objects.
Before you can complete this section, it's crucially important that you understand how to read and understand skeleton code. The lab spec will help you greatly in figuring out what each method does -- it's your job to figure out which methods are relevant to whatever you're doing.
For the following filters that you're asked to complete, you'll find that they are all extremely similar. I would recommend starting out with the ColumnMatchFilter
, as it will teach you the most. At this point, I recommend you go and try to figure out the answer by yourself. I will provide hints in increasing level of specificity, but you will learn a lot more if you figure it out yourself. If you get stuck, feel free to come back and look at a tip or two before moving on.
Many students are confused about what exactly this JoinIterator
is doing. The JoinIterator
actually consists of two iterators, one for each table, and is constructing the joined table. You can see an example below.
For extra programming help, take a look at the JoinIterator
constructor.
Why did the skeleton code include these specific variables as instance variables? Why was _table2
included, but not _table1
? Is it possibly, to construct a new iterator when necessary? This then begs the question, when do we need to create a new iterator? As a final question, how do we know when we are done iterating through both tables?
Answering these questions will put you well on the path to completing this assignment. While doing so, remember that hasNext()
is concerned only with whether there is a next row to iterate over, or not.
JoinIterator
iterates through table B for each item in table A.