Difference between revisions of "ImmutableList Assignment"
(→Apps) |
|||
Line 37: | Line 37: | ||
{{JavaToImplement|Length|length|immutable.list.apps.assignment}} | {{JavaToImplement|Length|length|immutable.list.apps.assignment}} | ||
− | < | + | <pre>public static <E> int length(ImmutableList<E> list)</pre> |
===SumProductCountdownFactorial=== | ===SumProductCountdownFactorial=== | ||
Line 43: | Line 43: | ||
====sum==== | ====sum==== | ||
− | < | + | <pre>public static int sum(ImmutableList<Integer> xs)</pre> |
====product==== | ====product==== | ||
− | < | + | <pre>public static int product(ImmutableList<Integer> xs)</pre> |
====countdown==== | ====countdown==== | ||
− | < | + | <pre>public static ImmutableList<Integer> countdown(int n)</pre> |
====factorial==== | ====factorial==== | ||
− | < | + | <pre>public static int factorial(int n)</pre> |
===Concat=== | ===Concat=== | ||
{{JavaToImplement|Concat|concat|immutable.list.apps.assignment}} | {{JavaToImplement|Concat|concat|immutable.list.apps.assignment}} | ||
− | < | + | <pre>public static <E> ImmutableList<E> concat(ImmutableList<E> xs, ImmutableList<E> ys)</pre> |
=Test= | =Test= | ||
{{TestSuite|ImmutableListTestSuite|immutable.list.assignment}} | {{TestSuite|ImmutableListTestSuite|immutable.list.assignment}} |
Revision as of 23:07, 17 January 2020
Contents
Code To Implement
Utilities
DefaultImmutableList<E>
class: | DefaultImmutableList.java | ![]() |
methods: | constructors head tail isEmpty |
|
package: | immutable.list.assignment | |
source folder: | src/main/java |
class DefaultImmutableList implements ImmutableList<E>
Note: consider using java.util.Optional
Note: you need not implement iterator() now. That will be the subject of a future studio.
Lists
class: | Lists.java | ![]() |
methods: | nil cons brackets |
|
package: | immutable.list.assignment | |
source folder: | src/main/java |
nil
public static <E> ImmutableList<E> nil()
Constructs a new empty list, analogous to the nil constructor for SML List.
cons
public static <E> ImmutableList<E> cons(E head, ImmutableList<E> tail)
Constructs a new list comprised of head::tail, analogous to the :: constructor for SML List.
brackets
public static <E> ImmutableList<E> brackets(E... elements)
Constructs an ImmutableList<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?
Apps
Length
class: | Length.java | ![]() |
methods: | length | |
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
public static <E> int length(ImmutableList<E> list)
SumProductCountdownFactorial
class: | SumProductCountdownFactorial.java | ![]() |
methods: | sum product countdown factorial |
|
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
sum
public static int sum(ImmutableList<Integer> xs)
product
public static int product(ImmutableList<Integer> xs)
countdown
public static ImmutableList<Integer> countdown(int n)
factorial
public static int factorial(int n)
Concat
class: | Concat.java | ![]() |
methods: | concat | |
package: | immutable.list.apps.assignment | |
source folder: | src/main/java |
public static <E> ImmutableList<E> concat(ImmutableList<E> xs, ImmutableList<E> ys)
Test
class: | ImmutableListTestSuite.java | ![]() |
package: | immutable.list.assignment | |
source folder: | src/test/java |