Assignment 2
Due Monday, April 2



This assignment contains questions for you to answer and Java code to write.

Part 1
java.util.Iterator is Java's basic interface for implementing the Iterator Design Pattern. It is poorly designed. In one to two sentences, explain why.

Part 2
Click here to view a Stack class in Java. It includes a method push() to add a value to the stack. push() is always called for its side-effect, yet instead of being declared void it returns a Stack. Why is this a good idea? Restrict your answer to one or two sentences.

Part 3
Java collections are supposed to be "fail fast". If you iterate over the elements of a collection and, while iterating, also modify the collection through another method (i.e., not the iterator's remove() method), the next iteration should signal a java.util.ConcurrentModificationException. In the example Stack, if you call push() or pop() while iterating, the next invocation of next() should signal a concurrent modification. Modify Stack to support this behavior and add code to main() that demonstrates that your code works. Save your modified version of Stack in Stack.java. Be sure that it compiles and runs correctly.

Part 4
The provided code uses several more advanced Java features, notably, generics, (anonymous) inner classes, the enhanced for loop, and automatic boxing/unboxing. The Java virtual machines does not understand any of these features. In Java, produce an alternative version of the provided code that uses none of the listed features but still has the exact same behavior. Call your new class AltStack and save it in a file AltStack.java. Be sure your code compiles and runs correctly.

Hint: Remember that the Java compiler produces class files containing bytecode, the instruction set executed by the Java virtual machine. If you want to read the bytecode generated by the compiler, the javap tool disassembles class files.

Part 5
Besides being much more verbose than the original Stack class, your AltStack class suffers from a significant short-coming. What is that, in one sentence?

Questions regarding this assignment should be directed to Prof. Grimm (rgrimm@cs.nyu.edu).

To submit the assignment, please follow these instructions:

As with all assignments, you must do this assignment on your own.