Goal:
Type Abstraction.
The point of this exercise is to show some
of the power of interfaces in Java.
In particular, the Iterable
interface allows for the writing of extremely
safe and compact for loops.
Build a version of the String
class called MyString
that implements the Java Iterable
interface.
The iterator()
method that you supply in MyString
should produce the characters in the string (as String
objects),
one at a time, from left to right.
For example, iterating over the string "cat" should
produce a "c", and then an "a", and then a "t".
The MyString
class doesn't actually have to have much additional functionality;
you may find a simple public String get()
method sufficent.
Provide a quality set of JUnit tests to test your implementation.
One of your tests should iterate over a MyString
and produce
the characters in reverse, then compare the reversed string with the original
string to check whether the string is a palindrome.