Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*DOWN BELOW IS THE CODE* package Lab3; import java.io.PrintStream; import java.util.Comparator; import java.util.Iterator; import java.util.NoSuchElementException; import java.util.Random; public class Lab03P5Wrapper { public interface List extends

image text in transcribed

*DOWN BELOW IS THE CODE*

package Lab3;

import java.io.PrintStream;

import java.util.Comparator;

import java.util.Iterator;

import java.util.NoSuchElementException;

import java.util.Random;

public class Lab03P5Wrapper {

public interface List extends Iterable{

public int size();

public boolean isEmpty();

public boolean isMember(E e);

public int firstIndex(E e);

public int lastIndex(E e);

public void add(E e); // at the end

public void add(E e, int index);

public E get(int index);

public E remove(int index);

public boolean remove(E e);

public int removeAll(E e);

public E replace(int index, E newElement);

public void clear();

public Object[] toArray();

public void print(PrintStream out);

public Comparator getComparator();

}

public static class ArrayList> implements List {

private static final int DEFAULT_SIZE = 10;

private E[] elements;

private int currentSize;

private Comparator cmp;

public class ArrayListIterator implements Iterator{

private int currentPosition;

public ArrayListIterator(){

this.currentPosition = 0;

}

@Override

public boolean hasNext() {

return this.currentPosition

}

@SuppressWarnings("unchecked")

@Override

public E next() {

if (this.hasNext()){

E result = (E) elements[this.currentPosition++];

return result;

}

else {

throw new NoSuchElementException();

}

}

}

/**

* TODO ADD HERE THE ADDITIONAL CONSTRUCTOR TO INITIALIZE AN ARRAY LIST THAT IS

* PASSED THE INITIAL CAPACITY AND THE COMPARATOR (IN THAT ORDER)

*/

@SuppressWarnings("unchecked")

public ArrayList(int initSize) {

if (initSize

throw new IllegalArgumentException("Initial size must be at least 1.");

}

this.currentSize = 0;

this.elements = (E[]) new Comparable[initSize];

/*TODO ADD THE NECESSARY CODE TO CONSTRUCT AN INSTANCE OF THE GENERIC COMPARATOR USING LAMBDA FUNCTIONS*/

}

public ArrayList(){

this(DEFAULT_SIZE);

}

@Override

public int size() {

return this.currentSize;

}

@Override

public boolean isEmpty() {

return this.size() == 0;

}

@Override

public boolean isMember(E e) {

return this.firstIndex(e) >=0;

}

@Override

public int firstIndex(E e) {

for (int i =0; i

if(cmp.compare(elements[i], e) == 0){

return i;

}

}

return -1;

// == compares references or scalar values

}

@Override

public int lastIndex(E e) {

for (int i = this.currentSize -1; i >= 0; --i){

if(cmp.compare(elements[i], e) == 0){

return i;

}

}

return -1;

}

@Override

public void add(E e) {

// add at the end

if (this.size() == this.elements.length){

this.reAllocate();

}

this.elements[this.currentSize++] = e;

}

@SuppressWarnings("unchecked")

private void reAllocate() {

E[] newElements = (E[]) new Comparable[2*this.size()];

for (int i=0; i

newElements[i] = this.elements[i];

}

this.elements = newElements;

}

@Override

public void add(E e, int index) {

if ((index this.currentSize)){

throw new IndexOutOfBoundsException("Illegal Position");

}

// special case: index == this.currentSize

if (index == this.currentSize){

this.add(e);

}

else {

if (this.size() == this.elements.length){

this.reAllocate();

}

for (int i = this.currentSize; i > index; --i){

this.elements[i] = this.elements[i-1];

}

this.elements[index] = e;

this.currentSize++;

}

}

@Override

public E get(int index) {

if ((index = this.currentSize)){

throw new IndexOutOfBoundsException("Illegal Position");

}

return this.elements[index];

}

@Override

public E remove(int index) {

if ((index = this.currentSize)){

throw new IndexOutOfBoundsException("Illegal Position");

}

E result = this.elements[index]; // el q se borra

for (int i=index; i

this.elements[i] = this.elements[i + 1];

}

this.elements[this.size()-1] = null;

this.currentSize--;

return result;

}

@Override

public boolean remove(E e) {

int targetIndex = this.firstIndex(e);

if (targetIndex

return false;

}

else {

this.remove(targetIndex);

return true;

}

}

@Override

public int removeAll(E e) {

int counter = 0;

while(this.remove(e)){

counter++;

}

return counter;

}

@Override

public E replace(int index, E newElement) {

// valid values [0, this.currentSize-1]

// same as [0, this.size() -1]

// && both case are True

// || at least one is true

if ((index = this.currentSize)){

throw new IndexOutOfBoundsException("Illegal Position");

}

E result = this.elements[index]; // the old one

this.elements[index] = newElement; // put the new one

return result; // return old one

}

@Override

public void clear() {

while(!this.isEmpty()){

// empty body

this.remove(0) ;

}

}

@Override

public Object[] toArray() {

Object[] result = new Object[this.size()];

for (int i=0; i

result[i] = this.elements[i];

}

return result;

}

@Override

public void print(PrintStream out) {

for (int i=0; i

out.print(this.elements[i]);

out.print(" ");

}

out.println();

}

@Override

public Iterator iterator() {

return new ArrayListIterator();

}

public E set(int index, E newElement){

if ((index = this.size())){

throw new IndexOutOfBoundsException("index is out of range.");

}

E result = this.elements[index];

this.elements[index] = newElement;

return result;

}

public Comparator getComparator() {

return cmp;

}

}

public static > List sort(List dataSet, Comparator comparator) {

// If List has one element, the list is already sorted

if(dataSet.size() == 1)

return dataSet;

//Split list in two equal halves

List left = new ArrayList();

List right = new ArrayList();

for(int i = 0; i

left.add(dataSet.get(i));

for(int i = dataSet.size() / 2; i

right.add(dataSet.get(i));

//Recursively sort each half

left = sort(left, comparator);

right = sort(right, comparator);

//Merge back together each sorted half

return merge(left, right, comparator);

}

private static > List merge(List left, List right, Comparator comparator) {

int i = 0, j = 0;

List result = new ArrayList();

// Traverse each halve at the same time using two indices (i & j)

// While one of the halves still has elements, keep traversing

while(i

// If the current element in the left half is less than or equal to the current element in the right half

// then add the element to the result list, if not add the current element in the right half

/*TODO ADD THE CORRESPONDING IF-ELSE STATEMENTS THAT USE THE COMPARATOR CREATED*/

}

//Add the leftover elements from the other halve that may have been left empty

while(i

while(j

return result;

}

}

In Java there are not any functional types, rather there are Functional Interfaces. These consist of an interface that only contain one explicit abstract method signature, which they are used also to implement and use Lambda Functions. These can be optionally annotated with the a Functiona I Interface. The Java API contains some Functional Interfaces, one of these is the Comparator E> interface inside the java. util package. The purpose of this interface is to define custom ways to compare data types with the compare (E 01, E o2) method. The method signature is as follows: public int compare (E1,E E2){ I/ some code to compare o1 and 02 \} The method's return type is an integer, that represents the result of the comparison as follows: nZsuchthatn=0n0ifo1=02ifo1o2 The Comparator interface definition is as follows, as specified in the Java API: QFunctionalinterface public interface Comparator T{ int compare ( T o1, T o2); boolean equals (object object); // Other methods \} The goal of this exercise is to modify the ArrayList implementation of the List ADT by making the List able to use a comparator object that is inside the clas: as a private field. The List will then use the Comparator object in an implementation of the Merge Sort algorithm that's already implemented for you. All you need to do is to finish the certain lines of code inside the algortihm that make use of the comparator and create the constructors such that they initializ the comparator object properly. in the cases where the user passes a comparator as a parameter as well as when they don't. Hint: Notice that in this implementation E extends Comparable and the generic array is created using the Comparable interface. What is the Comparable class? And how can you use it for initializing the Comparator when the user doesn't pass it as a parameter

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

More Books

Students also viewed these Databases questions

Question

a. Describe the encounter. What made it intercultural?

Answered: 1 week ago