Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete the implementation of the methods marked TODO in List.java. Many of the methods can be implemented in one line using map or reduce. If

Complete the implementation of the methods marked TODO in List.java. Many of the methods can be implemented in one line using map or reduce. If you choose to implement them this way, remove the "abstract" qualifier from the base class and implement the method there. Of course you should remove the methods in the two subclasses as well. Write test cases for each and every method. Be creative and thorough with the test cases import java.util.function.BiFunction; import java.util.function.Function; class EmptyListE extends Exception {} //------------------------------------------------------------- /** * This is persistent implementation. * After a list is created, it is never updated * * See the test cases for examples */ abstract class List { static List countdown (int n) { if (n == 0) return new NodeL<>(0, new EmptyL<>()); else return new NodeL<>(n, countdown(n-1)); } /** * Computes the length of the list */ abstract int length(); /** * Checks if the given elem occurs in the list * (Use .equals() to check for equality) */ abstract boolean inList(E elem); /** * Inserts newElem after every occurrence of elem */ abstract List insertAfter (E elem, E newElem); /** * Removes the first occurrence of elem (if * there is one) */ abstract List removeFirst (E elem); /** * Returns the 0-based index of elem in the list * If the element is not in the list, throw * an exception */ abstract int indexOf (E elem) throws EmptyListE; /** * Returns a new list that only contains the * elements satisfying the given predicate */ abstract List filter (Function pred); /** * Returns a new list in which every element * is transformed using the given function */ abstract List map (Function f); /** * Starting with base as the current result, * repeatedly applies the bifunction f to the * current result and one element from the list */ abstract F reduce(F base, BiFunction f); /** * Appends the given list at the end of the * current list */ abstract List append (List other); /** * Use to accumulate the reverse of the given list */ abstract List reverseHelper (List result); /** * Returns a big list containing all the sublists of * the current list */ abstract List> powerSet (); List reverse () { return reverseHelper(new EmptyL<>()); } } //------------------------------------------------------------- class EmptyL extends List { int length() { return 0; // TODO } boolean inList(E elem) { return false; // TODO } List insertAfter(E elem, E newElem) { return null; // TODO } List removeFirst(E elem) { return null; // TODO } int indexOf(E elem) throws EmptyListE { return 0; // TODO } List filter(Function pred) { return null; // TODO } List map(Function f) { return null; // TODO } F reduce(F base, BiFunction f) { return null; // TODO } List append(List other) { return null; // TODO } List reverseHelper(List result) { return null; // TODO } List> powerSet() { return null; // TODO } public boolean equals (Object o) { return o instanceof EmptyL; } } //------------------------------------------------------------- class NodeL extends List { private final E first; private final List rest; NodeL (E first, List rest) { this.first = first; this.rest = rest; } int length() { return 0; // TODO } boolean inList(E elem) { return false; // TODO } List insertAfter(E elem, E newElem) { return null; // TODO } List removeFirst(E elem) { return null; // TODO } int indexOf(E elem) throws EmptyListE { return 0; // TODO } List filter(Function pred) { return null; // TODO } List map(Function f) { return null; // TODO } F reduce(F base, BiFunction f) { return null; // TODO } List append(List other) { return null; // TODO } List reverseHelper(List result) { return null; // TODO } List> powerSet() { return null; // TODO } public boolean equals (Object o) { if (o instanceof NodeL) { NodeL other = (NodeL) o; return first.equals(other.first) && rest.equals(other.rest); } else return false; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

4th Edition

0805360476, 978-0805360479

More Books

Students also viewed these Databases questions

Question

What is group replacement? Explain with an example. (2-3 lines)

Answered: 1 week ago