CS230 ObjectMList Contract

The ObjectMList class describes mutable linked lists of objects. An mutable 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 one public instance method (toString()). Instead, object lists are created and manipulated via the class methods specified below. By default, an ObjectMList class method is invoked via ObjectMList.methodName(args), but this can be abbreviated to OML.methodName(args) by including the following "magical" declaration in the body of a class:

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

Class Methods

public static ObjectMList empty()
Returns an empty mutable object list.

public static ObjectMList prepend (Object x, ObjectMList L)
Returns a new mutable object list node whose head is x and whose tail is L. From another perspective, returns a mutable 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 (ObjectMList L)
Returns true if L is an empty mutable object list and false if L is a mutable object list node.

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

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

public static void setHead (ObjectMList L, Object newHead)
Changes the head component of the given mutable object list node L to be newHead. Signals an exception if L is empty.

public static void setTail (ObjectMList L, ObjectMList newTail)
Changes the tail component of the given mutable object list node L to be newTail. Signals an exception if L is empty.

public static String toString (ObjectMList 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 ObjectMList fromString (String s)
If s is the printed representation of an object list (i.e., comma separated elements delimited by square brackets), returns an ObjectMList 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.