|
This assignment is based on Sections 6.3.1-6.3.3 (pages 242-245) of our textbook. You will write a "wrapper" class for arrays that turns an array into a collection that can be iterated in a somewhat weird way.
Create a (parameterized) class WeirdArrayWrapper<T> that implements the Iterable<T> interface. The WeirdArrayWrapper<T> class should contain a private reference to an array that would hold references to objects of type T . The WeirdArrayWrapper<T> class should have two constructors, one that takes a single integer n and creates an empty array of size n , and another constructor that takes a reference to an already created array (of references to T objects). The WeirdArrayWrapper<T> class should have a "get" method that returns a reference to the element of type T stored at index i in the private array, and a "set" method that puts a reference to a T object at index i in the private array. The WeirdArrayWrapper<T> class should have a getLength() method that returns the length of the private array. And, since WeirdArrayWrapper<T> implements the Iterable<T> interface, the WeirdArrayWrapper<T> class should have a iterator() method that returns a reference to a Iterator<T> object. This method should construct, and then return a reference to, a WeirdIterator<T> object. The WeirdIterator<T> class is described in the next paragraph.
Create a WeirdIterator<T> class that implements the Iterator<T> interface. Here is how your iterator should iterate through the array. If the array has an odd number of items, return the middle item first, then the item just before the middle item, then the item just after the middle, then the item two places before the middle, then the item two places after the middle, then the item three places before the middle, etc. If the array has an even number of items, first return the last item of the first half of the array, then the first item of the second half of the array, then the second to last item of the first half of the array, then the second item of the second half of the array, then the third to last item from the first half, etc. (Notice that in both cases, the iterator starts in the "middle" of the array and then works it way towards the "ends" of the array.)
Your WeirdIterator<T> class should have one constructor that takes a reference to a WeirdArrayWrapper<T> object (the object that it will iterate through). Since the WeirdIterator<T> class implements the Iterator<T> interface, the WeirdIterator<T> class has three methods, hasNext() , next() , and remove() . The remove() method does not have any meaning in this example, so you should give this method an empty method body. The hasNext() and next() methods are what actually implement the iterator as described above. Notice that in order to implement the iterator, your WeirdIterator<T> class can have any fields or extra methods that you might think are useful. You will definitely need some kind of field to remember the current "position" (or cursor) of the iterator as it iterates through the array. (Your iterator should operate directly on the array wrapper object, not on a "snapshot" of it which has been copied into another collection as mentioned on page 245 of the textbook.)
In the definition of your WeirdArrayWrapper<T> class, there is one small technicality that needs to be worked around. Java does not allow the construction of arrays whose type is given by a type parameter (like T ). In other words, you cannot have a line of code like this.
T[] a = new T[n];
when T is a type parameter. The declaration T[] a is not the problem, the problem is with the creation of the array (using new ). You get around this limitation by creating an array of type Object and then immediately casting it to an array of type T . The resulting code will work correctly, but unfortunately the compiler will complain about it by issuing warnings of "unchecked casts". There is no way to get rid of these warnings.
In the zip file along with this file there is a program TestIterator.java that tests your implementation of the WeirdArrayWrapper<T> class. Do not make any changes to the TestIterator.java file. When TestIterator.java runs, it should produce output like that contained in the file SampleOutput.txt .
Turn in a zip file called CS275Hw7Surname.zip containing your Java source files, WeirdArrayWrapper.java and WeirdIterator.java , and also the original files contained in h7.zip . This assignment is due Monday, November 12.
|
|