Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//you can just implement method only for To do section import java.util.function.BiFunction; import java.util.function.Function; class EmptyListE extends Exception {} //------------------------------------------------------------- /** * This is persistent

//you can just implement method only for "To do" section 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; } } //------------------------------------------------------------- //------------------------------------------------------------- 

//below is the test file for this

import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; class ListTest { @Test void lists() throws EmptyListE { List ints = new NodeL<>(5, new NodeL<>(4, new NodeL<>(3, new NodeL<>(2, new NodeL<>(1, new NodeL<>(0, new EmptyL<>())))))); assertEquals(ints, List.countdown(5)); List ints100 = new NodeL<>(5, new NodeL<>(4, new NodeL<>(3, new NodeL<>(100, new NodeL<>(2, new NodeL<>(1, new NodeL<>(0, new EmptyL<>()))))))); assertEquals(ints100, ints.insertAfter(3, 100)); assertEquals(ints, ints100.removeFirst(100)); assertEquals(3, ints.indexOf(2)); List intsEvens = new NodeL<>(4, new NodeL<>(2, new NodeL<>(0, new EmptyL<>()))); List intsOdds = new NodeL<>(5, new NodeL<>(3, new NodeL<>(1, new EmptyL<>()))); assertEquals(intsEvens, ints.filter(n -> n%2==0)); assertEquals(intsOdds, ints.filter(n -> n%2==1)); List intsSquared = new NodeL<>(25, new NodeL<>(16, new NodeL<>(9, new NodeL<>(4, new NodeL<>(1, new NodeL<>(0, new EmptyL<>())))))); assertEquals(intsSquared, ints.map(n -> n*n)); assertEquals(15, ints.reduce(0, Integer::sum)); List intsEvensOdds = new NodeL<>(5, new NodeL<>(3, new NodeL<>(1, new NodeL<>(4, new NodeL<>(2, new NodeL<>(0, new EmptyL<>())))))); assertEquals(intsEvensOdds, intsOdds.append(intsEvens)); List intsRev = new NodeL<>(0, new NodeL<>(1, new NodeL<>(2, new NodeL<>(3, new NodeL<>(4, new NodeL<>(5, new EmptyL<>())))))); assertEquals(intsRev, ints.reverse()); List> evensPS = intsEvens.powerSet(); assertEquals(8, evensPS.length()); assertTrue(evensPS.inList(new EmptyL<>())); assertTrue(evensPS.inList(new NodeL<>(0, new EmptyL<>()))); assertTrue(evensPS.inList(new NodeL<>(2, new EmptyL<>()))); assertTrue(evensPS.inList(new NodeL<>(4, new EmptyL<>()))); assertTrue(evensPS.inList(new NodeL<>(4, new NodeL<>(2, new EmptyL<>())))); assertTrue(evensPS.inList(new NodeL<>(4, new NodeL<>(0, new EmptyL<>())))); assertTrue(evensPS.inList(new NodeL<>(2, new NodeL<>(0, new EmptyL<>())))); assertTrue(evensPS.inList(new NodeL<>(4, new NodeL<>(2, new NodeL<>(0, new EmptyL<>()))))); } }

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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions

Question

Describe Table Structures in RDMSs.

Answered: 1 week ago