Y you were introduced to immutable Java classes by way of a quick tour of a class List135 and an assigned reading. In this programming assignment, you will extend the functionality of List135 by adding methods.

You will find the original class at List135.java. You should study it and then modify it as specified below, maintaining the class's immutability. Because it's a theme in this class, I want you to practice programming in a functional style in this assignment. That means you should make any fields you add final , don't alter any variable after the first time it's set, and don't use any loops. Disallowing local variable changes and loops is not always needed to maintain immutability in a language like Java, but it is a theme in this class, so I want you to practice it here. Make your recursive calls be in a tail position whenever possible.

Modifications

Add the following method stubs, which will allow your class to compile with my testers even if you have not completed the assignment.

// Returns a list consisting of the first n elements of this list
// Note: the first n elements are copied into a new list
// Throws IndexOutOfBoundsException if n < 0 or n >= length of list
public List135< E > firstN(int n) {
null;
}

// Returns a list consisting of all but the first n elements of this list
// Throws IndexOutOfBoundsException if n < 0 or n >= length of list
public List135< E > allButFirstN(int n) {
null;
}

// Returns true iff this and o are the same length List135 and each pair
// of elements (in the same relative position) are equal().
public boolean equals(Object o) {
// For explanation of the following pattern see
// https://www.sitepoint.com/implement-javas-equals-method-correctly/
if (this == o)
return true;
if (o == null)
return false;
if (getClass() != o.getClass())
return false;
@SuppressWarnings("unchecked")
List135< E > other = (List135) o;
return equalsHelper(this, other);
}

private boolean equalsHelper(List135< E > l1, List135 l2) {
return false;
}

Method firstN should return a List135 equivalent to the first n elements of the List135 receiving the call. If n is negative or greater than the length of the list the method should throw IndexOutOfBoundsException.

Method allButFirstN should return a List135 equivalent to all but the first n elements of the List135 receiving the call. If n is negative or greater than the length of the list the method should throw IndexOutOfBoundsException.

Method equals should return true if and only if this and o are lists containing the same sequence of equal elements. I have started equal with a common pattern for it. You don't want to do all the test that preface equal on every recursive call, so I've added an equalsHelper for the recursive part.

Starter Code


public class List135< E > {

// Both of these will be null in the empty list.
private final E firstElement;
private final List135 restOfList;

// returns reference to list with no elements, aka empty list
public List135() {
this(null, null);
}

// private constructor creates new front of list element
private List135(E firstElement, List135 restOfList) {
this.firstElement = firstElement;
this.restOfList = restOfList;
}

// returns whether "this" refers to an empty list
public boolean isEmpty() {
return (restOfList == null);
}

// returns first element of list "this" refers to
public E first() {
if (isEmpty()) {
throw new RuntimeException("first() on empty list");
} else {
return firstElement;
}
}

// returns reference to list that begins after the first element
public List135 rest() {
if (isEmpty()) {
throw new RuntimeException("rest() on empty list");
} else {
return restOfList;
}
}

// returns reference to list that is newFront prepended to "this" list
public List135< E > cons(E newFront) {
if (newFront == null) {
throw new RuntimeException("cons of null not supported");
} else {
return new List135(newFront, this);
}
}

private String toStringHelper() {
if (isEmpty()) {
return "";
} else if (restOfList.isEmpty()) {
return firstElement.toString();
} else {
return firstElement.toString() + "," + restOfList.toStringHelper();
}
}

// returns String representing list. Assumes each element has toString()
public String toString() {
return "[" + toStringHelper() + "]";
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.