The constructor for the lists will take a generic array (E[]) as a parameter. We provide you with the constructor for ArrayGL.java. It is your task to complete the constructor for LinkedGL.java.

computer science

Description

Assignment Overview

Part 1: Implementation of MyList (42 points)

The MyList interface is implemented in ArrayGL.java and in LinkedGL.java:

public interface MyList<E>{

    E[] toArray();

    void transformAll(MyTransformer mt);

    void chooseAll(MyChooser mc);

    boolean isEmpty();

}

The constructor for the lists will take a generic array (E[]) as a parameter. We provide you with the constructor for ArrayGL.java. It is your task to complete the constructor for LinkedGL.java.

In part 1 and part 2, you will implement and thoroughly test the following methods:

LinkedGL(E[] contents)

isEmpty for both types of list

toArray for both types of list

transformAll for both types of list

chooseAll for both types of list

Implementations of the MyTransformer and MyChooser interfaces

The related interfaces MyTransformer and MyChooser are defined as:

public interface MyTransformer<E>{

    E transformElement(E e);

}


public interface MyChooser<E>{

    boolean chooseElement(E e);

}


public LinkedGL(E[] contents)

Constructor that creates a new LinkedGL with its elements from contents in the same order. For example, the following constructor call:

Integer[] input = {1, 2, 3};

LinkedGL<Integer> list = new LinkedGL<Integer>(input);

should create a new Linked Integer list with contents {1, 2, 3}.


public E[] toArray()

Returns the contents of the list as a new array, with shallow copy of the elements in the same order they appear in the list. The length of the array produced must be the same as the size of the list. To notice, you cannot initialize a generic array, like E[] array = new E[];. Hint: consider typecaste or check out lecture materials.


public boolean isEmpty()

Returns true if the list has no elements, false otherwise.


public void transformAll(MyTransformer mt)

Changes the contents of the list according to the provided MyTransformer. mt is a concrete class that implements MyTransformer. (We will illustrate how to implement MyTransformer in part 2.) It has a method called transformElement(E e), which takes an element as an argument and returns the transformed element. Therefore, you should apply transformElement method of mt class to each element in the list, getting the transformed element and replacing the orginal one. For example, consider the provided UpperCaseTransformer that implements MyChooser, which transforms a string into uppercase. If we construct a list like:

String[] contents = {"a", "b", "c"};

ArrayGL<String> agl = new ArrayGL<String>(contents);

agl.transformAll(new UpperCaseTransformer());

then we should expect the contents of the list after to be {"A", "B", "C"}.

When the element is null, it should not be transformed. For example,

String[] contents = {"a", "b", null};

ArrayGL<String> agl = new ArrayGL<String>(contents);

agl.transformAll(new UpperCaseTransformer());

then we should expect the contents of the list after to be {"A", "B", null}.


Instruction Files

Related Questions in computer science category