Difference between revisions of "ImmutableList Assignment"

From CSE425S Wiki
Jump to navigation Jump to search
Line 68: Line 68:
 
{{JavaToImplement|NonEmptyImList|constructor<br/>isEmpty<br/>head<br/>tail<br/>|immutable.list.util.exercise}}
 
{{JavaToImplement|NonEmptyImList|constructor<br/>isEmpty<br/>head<br/>tail<br/>|immutable.list.util.exercise}}
  
<code>final class NonEmptyImList<E> implements ImList<E>[https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse425s/current/apidocs/immutable/list/util/core/ImList.html ImList<Object>]</code>
+
<code>final class NonEmptyImList<E> implements [https://www.cse.wustl.edu/~dennis.cosgrove/courses/cse425s/current/apidocs/immutable/list/util/core/ImList.html ImList<E>]</code>
  
 
WARNING: you need NOT implement iterator() now.  That will be the subject of a future studio.
 
WARNING: you need NOT implement iterator() now.  That will be the subject of a future studio.

Revision as of 22:31, 20 January 2022

Code To Investigate

interface ImList<E>

public interface ImList<E> extends Iterable<E> {
	/**
	 * @return the head value if it is present
	 * @throws NoSuchElementException if empty
	 */
	E head();

	/**
	 * @return the tail if it is present
	 * @throws NoSuchElementException if empty
	 */
	ImList<E> tail();

	/**
	 * @return true if empty, false otherwise
	 */
	boolean isEmpty();
}

Code To Implement

Utilities

ImLists

class: ImLists.java Java.png
methods: nil
cons
brackets
package: immutable.list.util.exercise
source folder: src/main/java

nil

public static <E> ImList<E> nil()

Constructs a new empty list, analogous to the nil constructor for SML list datatype.

cons

public static <E> ImList<E> cons(E head, ImList<E> tail)

Constructs a new list comprised of head::tail, analogous to the :: constructor for SML list datatype.

brackets

public static <E> ImList<E> brackets(E... elements)

Constructs an ImList<E> whose contents are defined by the elements passed in.

The parameter E... arguments is an example usage of varargs in Java. You can treat parameter elements as if it is an E[]. The varargs ... simply allows for more convenient invocations of the brackets method.

In order to build this method, consider added a private static helper method to perform the actual work.

It is suggested that you adopt a recursive approach to solving this problem. What is the base case? What is the recursive case?

enum EmptyImList

class: EmptyImList.java Java.png
methods: constructor
isEmpty
head
tail
package: immutable.list.util.exercise
source folder: src/main/java

enum EmptyImList implements ImList<Object>

WARNING: you need NOT implement iterator() now. That will be the subject of a future studio.

constructor

What fields, if any, are required as state for an empty list?

isEmpty

Hey EmptyImList, are you empty?

head

Check the behavior of head() dictated by the specification in ImList<E>.

tail

Check the behavior of tail() dictated by the specification in ImList<E>.

class NonEmptyImList<E>

class: NonEmptyImList.java Java.png
methods: constructor
isEmpty
head
tail
package: immutable.list.util.exercise
source folder: src/main/java

final class NonEmptyImList<E> implements ImList<E>

WARNING: you need NOT implement iterator() now. That will be the subject of a future studio.

constructor

What fields, if any, are required as state for a non-empty list?

isEmpty

Hey NonEmptyImList, are you empty?

head

Check the behavior of head() dictated by the specification in ImList<E>.

tail

Check the behavior of tail() dictated by the specification in ImList<E>.

Clients

NOTE: Since the constructors on our implementations are package protected, you will need to use nil() and cons(head,tail).

Length

class: Length.java Java.png
methods: length
package: immutable.list.clients.exercise
source folder: src/main/java
public static <E> int length(ImList<E> list)

Compute the length of an arbitrary ImList<E>.

SumProductCountdownFactorial

class: SumProductCountdownFactorial.java Java.png
methods: sum
product
countdown
factorial
package: immutable.list.clients.exercise
source folder: src/main/java

sum

public static int sum(ImList<Integer> xs)

Compute the sum of all the items in an ImList<Integer>.

product

public static int product(ImList<Integer> xs)

Compute the produce all the items in an ImList<Integer>.

countdown

public static ImList<Integer> countdown(int n)

Produce a new list which contains the values from n down to 1. For example, countdown(5) would produce the list [5,4,3,2,1].

factorial

public static int factorial(int n)

Can you implement this method using the methods you have already created?

Concat

class: Concat.java Java.png
methods: concat
package: immutable.list.clients.exercise
source folder: src/main/java
public static <E> ImList<E> concat(ImList<E> xs, ImList<E> ys)

Concatenate one list on to another.

Test

class: ImListUtilAndClientsTestSuite.java Junit.png
package: immutable.list
source folder: src/test/java

Pledge, Acknowledgments, Citations

file: studio-immutable-list-pledge-acknowledgments-citations.txt

More info about the Honor Pledge