In this exercise, you will get some practice with interfaces:
- Start by examining the
StrangeDataStructure.java
interface. You can also look at the
Javadoc version.
- Notice that the interface has a number of methods.
- You are to write an object called
StrangeDataStructureImpl.java
that will implement the methods.
Start by using this template.
- The code for testing is in
TestStrangeDataStructure.java.
- The test code shows that:
- Some integers will be fed into the data structure,
using the addElement() method.
- The sum of these integers is requested from the
the data structure, using the
getSumOfElements() method.
- There are two enumerations, that are invoked using
the methods oddElements() and evenElements().
- Your code in StrangeDataStructureImpl.java will
implement the interface:
- This means you will have to implement every method in
the interface.
- You will need to store the integers fed to you.
You can use whatever internal data structure you like, including
library data structures, but
should be prepared for a large (and unknown) size, even
if the test in TestStrangeDataStructure is small.
- The addElement() and getSumOfElement() methods
seem straightforward, so let's consider oddElements:
- Notice that the return type is an Enumeration,
which is itself an interface.
- Thus, you will need to return an object that implements
the Enumeration interface.
- Recall that the Enumeration interface has two methods:
public interface Enumeration {
public boolean hasMoreElements ();
public Object nextElement ();
}
Your enumeration object will walk through the data structure
in such a way that every call to nextElement()
will return one of the odd integers stored.
- By successively calling nextElement() it should
be possible to "enumerate" all the odd integers stored.
- The evenElements() method is analogous.
- Suggestion: start by getting the first two methods working, then do the
odd-elements enumeration, and then the even one. You can comment out
appropriate portions of the test code for this purpose.
Deliverables and submission:
- The source code for your program. Please write all your code in the
same file, StrangeDataStructureImpl.java
- NOTE: Your code will be graded on both correctness and documentation (comments).
- Follow the submission instructions carefully.
- For this exercise, you will name your jar-file (see the
submission instructions) with your username followed by
5.jar
- Draw a complete memory picture just before the start of
the first while-loop. You do not need to show all the elements
in the data structure, just the first few. But you do need
to show the different kinds of objects. You can either
draw something electronically and include that in the
jar file, or submit a neat hand-drawn picture on paper
in my mailbox by the due date.