Length.java
Created with JBuilder
/**
 * A non object-oriented approach to computing the length of a list, treating
 * the list as an "abstract data type".  Uses a static method.
 * @author D.X. Nguyen
 * @since 01/25/02
 */
public class Length {

    /**
     * Calculates the length of a list by checking what type of a list it is.
     * Returns 0 if the list an EmptyList, otherwise recursively calculates
     * the length of the rest of the list, adds 1 to the length of the rest, and
     * returns the result.
     * Uses the instanceof operator.
     * @param list an AList whose length is to be computed and printed.
     * @return an int >= 0.
     */
    public static int CalcLen(AList list) {
        if (list instanceof EmptyList) {
            return 0;
        }
        else {
            return 1 + CalcLen(list.getRest());
        }

        /*
        Equivalent code:
        return (list instanceof EmptyList)? (0) : (1 + CalcLen(list.getRest()));
        */
    }
}

Length.java
Created with JBuilder