You are probably being recorded, perhaps even transcribed.
Approximate overview
Academic/Scholarly
Cultural
Peer
Wellness
Misc
Will more tokens become available?
I am revising the token system. Stay tuned.
Will there be a limit to the number of token activities we can submit?
Just physical limits. If I don’t set up enough slots, let me know and I’ll add more.
What happens if I don’t submit the mini-project by its late submit date?
You have to wait until the redo.
What happens if I don’t submit the redo by its late submit date?
You have to wait until the re-redo.
There are a number of situations in which we need “one use” things. For example, if I’m sorting students by some formula, it seems wastful to build a whole new function for that. In Scheme, we write a lambda.
(sort students (lambda (s1 s2) (or (better? (major s1) (major s2)) (> (donation-from-family s1) (donation-from-family s2)))))
We want to do the same thing in Java. Except Java is object oriented rather than functional, so we do “anonymous classes”.
Lab problem: Create a priority queue of strings that orders the strings by length (if different lengths) and alphabetically (if the same length).
import java.util.PriorityQueue;
import java.util.Comparator;
import java.io.PrintWriter;
/**
* A quick experiment with priority queues.
*
* @author CSC 207 2024Sp
*/
public class PQE {
public static void main(String[] args) {
PrintWriter pen = new PrintWriter(System.out, true);
PriorityQueue<String> pq =
new PriorityQueue<String>(new Comparator<String>() {
public int compare(String s, String t) {
int tmp = s.length() - t.length();
if (0 == tmp) {
return s.compareTo(t);
} else {
return tmp;
}
}
});
// Add all the value
pq.add("alpha");
pq.add("beta");
pq.add("bill");
pq.add("bart");
pq.add("c");
pq.add("aardvark");
pq.add("television");
pq.add("um");
// Print them out, clearing the queue
// while pq still has values
// remove the highest-priority value and print it
while (!pq.isEmpty()) {
pen.println(pq.poll());
}
pen.close();
} // main
} // class PQE
Iterators are used to allow us to visit all of the elements in a collection (ideally, any kind of collection: list, stack, queue, tree, graph, heap, hash table, set).
Most important methods: next()
- gets the next unvisited element,
and hasNext()
.
Conceptually, we think of the elements of the collection as being in a sequence (list).
H E L L O W O R L D
^
The iterator is between elements. next
means “go over the element and
return it”.
H E L L O W O R L D
^
Some iterators provide a remove()
method. remove()
removes the last
element returned by next()
(at least at the first level of accuracy).
Why do we have iterators?
remove
, it’s also a uniform and general way to remove elements.while students.hasNext() {
if (students.next().hasAdvisor()) {
students.remove():
}
}
How do we write iterators? Often, as anonymous inner classes.
Here’s an instance in which we don’t use an anonymous inner class.
class ArrayBasedQueueIterator<T> implements Iterator<T> {
// Notes
// Fields are front, size, values
// +--------+----------------------------------------------------------
// | Fields |
// +--------+
int i; // How many values I've seen.
ArrayBasedQueue<T> q;
// +--------------+----------------------------------------------------
// | Constructors |
// +--------------+
/**
* Create a new iterator.
*/
public ArrayBasedQueueIterator(ArrayBasedQueue<T> q) {
this.i = 0;
this.q = q;
} // ArrayBasedQueueIterator
// +---------+---------------------------------------------------------
// | Methods |
// +---------+
@Override
public T next() throws NoSuchElementException {
if (!this.hasNext()) {
throw new NoSuchElementException("no elements remain");
} // if no elements
return q.values[(q.front + i++) % q.values.length];
} // next()
@Override
public boolean hasNext() {
return this.i < this.q.size;
} // hasNext()
@Override
public void remove() throws UnsupportedOperationException {
throw new UnsupportedOperationException();
} // remove()
} // ArrayBasedQueueIterator<T>
Question: Where do we put that.
Answer: Often in the same file as the class. We still define this as a class.
Observation: If we make the fields of the primary class private, this won’t work, because there access issues.
Can we put one class inside another class to solve that? Yes.
Where does Iterator
come from?
java.util.Iterator
(there’s also ajava.util.ListIterator
)
Can we look at a use of the remove
method?
Iterator<String> sit = expt.iterator();
while (sit.hasNext()) {
String s = sit.next();
pen.println(s);
if (s.equals("B")) {
sit.remove();
}
}
for (String t : expt) {
pen.println(t);
}
You have to keep on learning as a full-time employee.
It’s useful that Grinnell makes you learn multiple languages.
Projects? AppDev. One of the random Sam one-credit courses.
“Commits on GitHub count.”