Overview
csc207-01-grader@grinnell.eduTalk to me about the toString method in hashes.
for (int i = 0; i <= 255; i++) {
pen.println(String.format("%d: %x", i, i));
} // for
Why did Alice and Bob turn into Anna and Bob?
Foolish consistency is the hobgoblin of small minds. I’ll fix it.
What’s a nonce?
Something that’s hard to explain.
A bit like a password.
It’s a number that allows us to generate a valid hash.
How does mining relate to nonces?
Mining is a process in which we repeatedly generate candidate nonces until we find one that gives us a valid hash.
What makes a hash valid?
For this assignment, the first three bytes must be zero.
What’s a byte?
An integer between 0 and 255.
How much effort to mine?
In this case, it appears that only 1/(256^3) hashes are valid, so about 256^3 nonces must be generated.
If you finish early, take the time to sit quietly. Alternately, get a drink of water or use the restroom.
What is an iterator?
An object that allows you to visit each element of a collection one by one.
How does it do that?
Through two methods,
next()andhasNext().
Client repeatedly calls
next()to visit the elements. Knows to stop whenhasNext()returns false.
Why do we use them?
Allows the client to access the elements in the collection.
Lets us write more general code.
Provides encapsulation: Client does not need to know the innards of our collection.
What is an anonymous inner class?
A class that you can make within another class.
They don’t have a name, that’s why they’re anonymous.
Why do we use them?
Good for helper classes (just like anonymous functions were usuful as helpers in Scheme), particularly when we only want to use them once.
How do we write them?
Something like the following. Here’s a counter that increments by ten, starts at 5, and resets to 0.
new Counter() {
int i = 5;
public int get() { return i; }
public int increment() { i += 10; }
public int reset() { i = 0; }
}
foo(new Predicate<String>() { public boolean test(String str) return false; });
Writeup: Exercise 3
public ArrayBasedQueueIterator<T> implements Iterator<T> {
ArrayBasedQueue<T> abq;
int i;
public ArrayBasedQueueIterator(ArrayBasedQueue<T> abq) {
this.abq = abq;
}
// ... this.abq.values[pos] ...
}
public ArrayBasedQueue<T> implements Iterable<T> {
// ...
Iterator<T> iterator() {
return new ArrayBasedQueueIterator<T>(this);
}
// ...
}