Goals
- to practice class implementations
- to learn more about sequences
Overview
For this lab, you will implement the sequence data type as defined
and discussed in chapter 3 of Main's textbook starting on page 145.
I've added a toString method. Don't change it.
The ~rogers162/labs/Sequence
directory contains
the class (DoubleArraySeq.java)
and the JUnit tests (TestDoubleArraySeq.java) files.
There is also a PrintSequence.java printing example you can ignore
or play with.
You may build another class if you wish to play with the sequence,
or you may add some additional tests.
The DoubleArraySeq.html file is the JavaDoc documentation
for the class.
Focus your effort on implementing the methods of DoubleArraySeq.
What Your Program Must Do
The JavaDoc format specifications for the class are contained in the
DoubleArraySeq.java file as well as the in
DoubleArraySeq.html file.
You fill in the implementation of the methods.
This version implements the sequence using a partially filled array.
Pay careful attention to the meaning of addBefore, addAfter,
and removeCurrent and what happens when there is and is not a current item.
Looking at the picture above, think about what happens with the various member functions with 2.718 as the current item as shown. What happens if the current item is 3.14 at the beginning? How about if there is no current item? Draw a lot of pictures.
The comments include the class invariant near the top of the implementation file. As you build each method, carefully check that the method is doing what it is supposed to do, changing the instance variables appropriately, and ensuring that the invariant is true when execution leaves the method.
You should practice defensive programming by enforcing the preconditions by throwing IllegalStateException and IllegalArgumentException as noted in the specifications. The other exceptions will occur by themselves and are not tested. The JUnit tests do check for IllegalStateException.
How to do it
Understand how each instance variable represents part of the class.
Maintain the class invariant.
The book has suggestions for implementing some methods.
Write size, start, advance, isCurrent, and getCurrent. Some of those are trivial. Others need to check their precondition. Then add either addBefore or addAfter.
Realize that System.arraycopy can be very helpful. It can be used with the same array being both the source and destination. See p. 129. Be careful with the parameters. Draw pictures.
For ease of reference, the class invariant is provided below. It is also in the source code:
// Invariant of the DoubleArraySeq class: // 1. manyItems is the number of elements in the sequence. // 2. For an empty sequence (with no elements), we do not care what is // stored in any of data; for a non-empty sequence, the elements of the // sequence are stored in order in data[0] through data[manyItems-1], // and we don't care what's in the rest of data. // 3. If there is a current element, then it is in data[currentIndex]; // if there is no current element, then currentIndex equals manyItems. private double[ ] data; private int manyItems; private int currentIndex;
Submit
When you are satisfied that your program works as expected,
submit your directory as Sequence.
Only your DoubleArraySeq.java file will be submitted.