Comp 212 Homework 1
Lists

Due Friday Sept. 15, 2000 11:59 PM.

This homework exercise serves as a transition from functional programming to object-oriented programming.  It also serves as a "dry run" for the larger programming projects that will come later.  It is based on the Scheme list example discussed in class and in lab 02.

Scheme List

You are given the following object model for a Scheme-like list structure.

list.png (9749 bytes)

Your task is to add the public methods to these classes as specified in the following problems.  You are free to add any private and protected methods to your classes to support your public operations.  You are not allowed to check for the type of AList, such as using instanceof or add a method to check for the type, to perform any task.  The classes AList, EmptyList, NEList, and ListFactory should be in a package named scheme as shown in lab 02.

  1. Modify the toString method to print AList with matching parentheses as in Scheme.  The empty list should prints as ().
  2. Add the method public AList reverse()   to class AList and appropriate methods to the supporting classes to compute and return the receiver in reverse order.
  3. Add the method public AList makeClone () to class AList and appropriate methods to the supporting classes to compute and return a copy of the receiver.
  4. Add the method public Object getNth (int n) to class AList and appropriate methods to the supporting classes to compute and return the n-th elements of the receiver.  Here 0 <= n, and the case n equals 0 corresponds to the first element of the list.
  5. Add the method public Object lastElement()   to class AList and appropriate methods to the supporting classes to find and return the last element of the receiver.  Do this without using the length of the receiver.
  6. Add the method public AList firstNElements (int n) to class AList and appropriate methods to the supporting classes to compute and return an AList that contains the first n elements of the receiver.  Here 0 <= n, and the case n equals 0 corresponds to the empty list.
  7. Add the method public AList concatWith (AList rhs) to class AList and appropriate methods to the supporting classes to concatenate the receiver with the parameter and return the resulting AList.  For example, (a b c).concatWith (1 2) returns (a b c 1 2).

For each of the above problems, be sure to add test cases which cover all reasonable cases.  All programs in this exercise should be written in a purely functional style: no object fields should be modified once they have been initialized by a constructor.

As with all programs in this course, lack of good coding style (good style includes reasonable variable names, a header before each non-overridden function, reasonable indentation) will result in a substantial loss of points.  The code should be documented in javadoc style.  The provided java files in lab 02 can serve as examples of coding style and documentation format that are acceptable to us. 

Submission

The homework is due Friday Sept. 15, 2000 11:59 PM.  It is to be submitted electronically.  Your labbies will give you specific submission instructions in the upcoming lab tutorial.  No late submission will be accepted.  The complete homework set should contain the following:


dxnguyen@cs.rice.edu         Please let us know of any broken links             Revised Sept. 11, 2000