CS230 ObjectList Contract

The ObjectList class describes immutable linked lists of objects. An object list is defined recursively as either

Object lists are immutable in the sense that the head or tail of a list node cannot be changed after the list node has been created (via the prepend() method).

There are no public constructor methods for object lists, and only two public instance methods (equals() and toString()). Instead, object lists are created and manipulated via the class methods specified below. By default, an ObjectList class method is invoked via ObjectList.methodName(args), but this can be abbreviated to OL.methodName(args) by including the following "magical" declaration in the body of a class:

public static ObjectList OL; // declare but never initialize this variable.

Class Methods

public static ObjectList empty()
Returns an empty integer list.

public static ObjectList prepend (Object x, ObjectList L)
Returns a new object list node whose head is x and whose tail is L. From another perspective, returns a list whose length is one more than the length of L and whose elements are x followed by the elements of L, in order.

public static boolean isEmpty (ObjectList L)
Returns true if L is an empty object list and false if L is an object list node.

public static int head (ObjectList L)
Returns the object that is the head component of the object list node L. Signals an exception if L is empty.

public static ObjectList tail (ObjectList L)
Returns the object list that is the tail component of the object list node L. Signals an exception if L is empty.

public static boolean equals (ObjectList L1, ObjectList L2)
Returns true if L1 and L2 have the same length and have elements that are correspondingly equal via the equals() instance method. Otherwise returns false.

public static String toString (ObjectList L)
Returns a string representation of the object list L in which the string representations of the list elements are separated by commas and delimited by square brackets. For example, if L1 is a list containing the sequence of strings "Sam", "I", "am", then toString(L1) returns "[Sam,I,am]".

public static ObjectList fromString (String s)
If s is the printed representation of an object list (i.e., comma separated elements delimited by square brackets), returns an ObjectList containing the strings of each element. For example, fromString("[Sam,I,am]") returns a 3-element object list with elements "Sam", "I", and "am". If s is not the printed representation of an object list, a RuntimeException is thrown.

Instance Methods

public String toString ()
Returns a string representation of this object list that is equivalent to toString(this).

public boolean equals (Object L)
Returns a boolean that is equivalent to equals(this,L) if L is an object list and false otherwise.