1. Engineering
  2. Computer Science
  3. this two files and requirements are in files file one...

Question: this two files and requirements are in files file one...

Question details

this two files and requirements are in files

File one

import java.util.function.Predicate;

import java.util.Comparator;

import java.util.Random;

class EmptyListE extends Exception {}

abstract class List1<E> {

/**

The first three methods below are as written in class.

*/

abstract int length ();

abstract boolean isEmpty();

abstract List1<E> append (List1<E> ys);

/**

The next three methods are simple getters, except that they

might throw exceptions if the list does not have enough

elements.

*/

abstract E getFirst() throws EmptyListE;

abstract E getSecond() throws EmptyListE;

abstract List1<E> getRest() throws EmptyListE;

/**

The method 'triplicate' returns a new list in which every

element is repeated three times.

*/

abstract List1<E> triplicate();

/**

The method 'filter' takes a predicate on values of type E

(which is a function of one argument of type E that returns a

boolean) and returns a new list that only includes those

elements from the current list that satisfy the predicate.

*/

abstract List1<E> filter (Predicate<E> p);

/**

The static method 'makeIntList1' takes a size and a bound and

returns a list of the given size in which every element a

random number between 0 and bound-1. You can produce such

numbers using the class System.Random.

*/

static List1<Integer> makeIntList1 (int size, int bound) {

return null; // TODO

}

/**

The static method 'sort2' takes two elements of type E and a

comparison function that compares values of type E and returns

a list of two elements in the sorted order.

*/

static <E> List1<E> sort2 (E e1, E e2, Comparator<E> c) {

return null; // TODO

}

/**

The static method 'quicksort' sorts a list by the order implied

by the given comparator. The method works as follows:

- if the list is of size 0 or 1, then it is already sorted

- if the list is of size 2 then use the method 'sort2' to

produce the result

- otherwise, let the first element be called 'e' and the rest be

called 'r'.

Use the method filter on 'r' twice to produce one sublist of

all the elements

that are < e and another sublist of all the elements >= e.

Recursively call

quicksort on the sublists and use append to produce the

combined sorted list.

The entire implementation shoudl take a few lines of code. If your

code starts

getting too big, then stop and start again.

*/

static <E> List1<E> quickSort (List1<E> xs, Comparator<E> c) {

return null; // TODO

}

}

class Empty<E> extends List1<E> {

// TODO

}

class Node<E> extends List1<E> {

// TODO

}

File 2:

import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.function.Predicate;

public class List2<E> {
    private LinkedList<E> deleg;

    List2() {
        this.deleg = new LinkedList<>();
    }

    E get (int index) {
        return deleg.get(index);
    }

    int length () {
        return deleg.size();
    }

    void add (E elem) {
        deleg.add(elem);
    }

    /**
       This is the same method from the class List1. Your solution must use 
       the method listIterator to get an iterator for the list.
     */
    void triplicate () {

    }

    /**
       This is the method sum we wrote in the Jan. 15 lecture for the
       List1 class.  Your solution for this class must use the method
       listIterator to get an iterator for the list.
     */
    E sum (E base, BiFunction<E,E,E> acc) {

    }

    /**
       The following method reverses the list using iterators. A
       simple idea is to the 'descendingIterator' method to traverse
       the list backwards and build the reversed list.
     */
    void reverse () {
   return null; // TODO
    }

    /**
       The following method takes a predicate on values type E and
       returns the number of list elements that satisfy the
       predicate. Again use an iterator to traverse the list.
     */
    int countOccurrences (Predicate<E> pred) {
   return 0; // TODO
    }

    void sort (Comparator<E> c) {
        deleg.sort(c);
    }

    /**
       This is similar to the method in List1. 
     */
    static List2<Integer> makeIntList2 (int size, int bound) {
   return null; // TODO
    }
}
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution