CS 210 Intermediate Programming with Data Structures
Programming Assignment 5
Fall 2004

Due Monday, November 15 at midnight

 

Purpose

This assignment aims to help you:

·         Work with the details of linked lists.

·         Try out “extreme programming” in its simplest form.

·        See how we can use the full power of Arrays.binarySearch to locate an insertion spot in an array.

·        Think about how we could wrap a JDK class to implement a specialized interface like OrderedList.

Reading

Read Weiss, Chapter 16 and Section 5.6, for the binary search algorithm.

Description

In this assignment you will work with the ordered list interface, specified in OrderedList.java. An ordered list is a list whose elements can be ordered and that maintains the elements in a specified order (smallest to largest, or vice-versa). In the first part, you will implement an ordered list using a singly linked list, and in the second part, you will implement an ordered list in terms of an array. The principal difference between the two implementations is the way that an element in the list is searched for, sequentially in the case of the linked list implementation versus a binary search in the case of the array implementation.

1.       Try out the XP “extreme programming” approach of writing the test program first, i.e., “test-driven development.”  Write OrderedListTester to test any OrderedList implementation.  Generate a list of 10 random Integers from 1 to 100 by adding to an originally empty list.  Make sure the list is in order after it is built.  Delete the first, last, and middle elements, and then retest for order.  Do get(middle-index) and then indexOf(that element) and check consistency.  Think of two more tests, so that all methods of the interface are tested.  In main, loop over 10 tests like this, each with a list of 10 elements (main itself should be short.).  Eventually, write OrderedListDriver with the main() for testing. Usage: java OrderedListDriver.

2.       Implement the OrderedList interface in class SLLOrderedList using a singly linked list, or SSL for short

3.       Next implement the OrderedList interface in class ArrayOrderedList using an array.

Design

For 1., reread pa1 solution’s BinCollectionTester and BinCollectionDriver and mimic them for your OrderedListTester and OrderedListDriver. In particular, OrderedListTester does not have main(), where the actual OrdedList objects are constructed.  The main() should be in OrderedListDriver.  Make sure that OrderedListTester can compile without any concrete classes of OrderedList available, just the OrderedList interface itself and the interface classes Comparable and Iterator.  This is easy to check if you write OrdedListTester first, before 2. and 3.

For 2., a good strategy here is to start with any singly linked list implementation and modify it, by deleting unnecessary code and modifying other code for the current task. I recommend that you start with SinglyLinkedList.java . However, note that the iterator implementation is missing, so you need to fill that in.  Also, consider removing the recursion in the code you keep.

For 3., you can similarly borrow from the ArrayList.java from Weiss’s website. An interesting and important part of this implementation is the code to locate the position in the array at which to insert or delete. Here we can use the power of the binarySearch in Arrays.  Note that it does more than the binarySearch in Section 5.6, which just returns NOT_FOUND if the element searched for is not already in the array. The additional special return values are explained in comments in the middle of pg. 200.  This additional power lets us use Arrays.binarySeach to find the spot to insert a new element, as well as locate an old element by value.

In both cases, please edit out all unused code.  We might search for unused method names in the grading script. Note that OrderedList.java is a “standalone” interface, not extended from anything, so the only methods you need other than the ones listed in it are those needed by any object.  You don’t have to do Javadoc for all these methods unless you want to.

memo.txt

In the file memo.txt, answer the following questions, in one to three pages (60-180 lines) of text.  Use complete sentences, as you would for an English class

·        What problems did you encounter and how did you solve them? 

·        Did you have any trouble with the iterator implementation, or the call to binarySearch?

·        Note that SinglyLinkedList implements Collection.  Why can’t this implementation claim “implements List”?

·        Give the big-Oh performance of get(i) and searchFor(obj) for both implementations, for N elements in the OrderedList.

·        Compare the power of OrderedList vs. LinkedList—what can LLs do that OLs can’t, and vice versa.

·        Did you remove any recursion in the code you took from SinglyLinkedList?  Discuss.

·        Does your OrderedList implementation check for concurrent modification in 2?, in 3?  Just discuss this: you don’t have to implement this advanced feature unless you want to.

·        Another approach to implementing OrderedList is to wrap a JDK Collection class, that is, have a private field of type LinkedList or ArrayList (or something else??).  Discuss how you would do this.

Turn In

Use the turnin system to submit your files.  Before the due date, submit the following files to the pa5 folder of your turnin system account:

  • memo.txt
  • OrderedListTester.java
  • OrderedListDriver.java
  • SLLOrderedList.java
  • ArrayOrderedList.java