Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Modify ListAPI. a) Download “ListAPI.zip” from Moodle and extract the NetBean

ID: 3827041 • Letter: 1

Question

1) Modify ListAPI.

a) Download “ListAPI.zip” from Moodle and extract the NetBeans project folder “ListAPI” from the archive file. In NetBeans under the Project tab, right click on ListAPI and select Rename. In the Rename Project dialog box, rename the project “p5_1_your-clid”, select the Also Rename Project Folder option and click Rename.

b) Define the following method in MyList and implement it in MyAbstractList.

// Adds the elements in otherList to this list.

// Returns true if

// this list changed as a result of the call.

public boolean addAll(MyList<E> otherlist)

c) Add code to “TestMyArrayList.java” to demonstrate that this method works correctly. Display the modified list using a java.util.Iterator<E> object and a while loop.

I already did part a) but I am having trouble understanding what it is asking me to do #MyList

public interface MyList<E> extends java.lang.Iterable<E> {

/** Add a new element at the end of this list*/
public void add(E e);
  
/**Add a new element at the specified index in this list*/
public void add(int index, E e);

/**Clear the list*/
public void clear();

/**Return true if this list contains the element*/
public boolean contains(E e);

/**Return the element from this list at the specified index*/
public E get(int index);

/**Return the index of the first matching element in this list. Return -1 if
* no match.*/
public int indexOf(E e);

/**Return true if this list contains no elements*/
public boolean isEmpty();

/**Return the index of the last matching element in this list Return -1 if
* no match.*/
public int lastIndexOf(E e);

/**Remove the first occurrence of the element o from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.*/
public boolean remove(E e);

/**Remove the element at the specified position in this list Shift any
* subsequent elements to the left. Return the element that was removed from
* the list.*/
public E remove(int index);

/**Replace the element at the specified position in this list with the
* specified element and returns the new set.*/
public Object set(int index, E e);

/** Return the number of elements in this list */
public int size();
}

#MyAbsrtactList

public abstract class MyAbstractList<E> implements MyList<E> {

protected int size = 0; // The size of the list

/**Create a default list*/
protected MyAbstractList() {
}

/**Create a list from an array of objects*/
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++) {
add(objects[i]);
}
}

@Override
/**Add a new element at the end of this list*/
public void add(E e) {
add(size, e);
}

@Override
/**Return true if this list contains no elements*/
public boolean isEmpty() {
return size == 0;
}

@Override
/**Return the number of elements in this list*/
public int size() {
return size;
}

@Override
/**Remove the first occurrence of the element e from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.
*/
public boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else {
return false;
}
}
  
public boolean addAll(MyList<E> otherList) {
for (int i=0; i<otherList.size(); i++) {
add(otherList.get(i));
}
return !otherList.isEmpty();
}
  
public boolean removeAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<otherList.size(); i++) {
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
  
public boolean retainAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
  
}

#TestMyArrayList

public class TestMyArrayList {

public TestMyArrayList() {
// Create a list
MyList<String> list = new MyArrayList<>();

// Add elements to the list
list.add("America"); // Add it to the list
System.out.println("(1) " + list);

list.add(0,"Canada"); // Add it to the beginning of the list
System.out.println("(2) " + list);

list.add("Russia"); // Add it to the end of the list
System.out.println("(3) " + list);

list.add("France"); // Add it to the end of the list
System.out.println("(4) " + list);

list.add(2, "Germany"); // Add it to the list at index 2
System.out.println("(5) " + list);

list.add(5, "Norway"); // Add it to the list at index 5
System.out.println("(6) " + list);

// Remove elements from the list
list.remove("Canada"); // Same as list.remove(0) in this case
System.out.println("(7) " + list);

list.remove(2); // Remove the element at index 2
System.out.println("(8) " + list);

list.remove(list.size() - 1); // Remove the last element
System.out.print("(9) " + list + " (10) ");

for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
}
}

Explanation / Answer

The question asks you to download "ListAPI.zip" from Moole, and extract the zip file. Then you need to open the project in NetBeans IDE. If you don't have one, kindly search for NetBeans in google and you will get serveral links on where to download and how to install. After successfull installation, open up the project file in NetBeans IDE and rename the file to "p5_1_your-clid", also rename the folder to same.

After renaming the project and the file, the problem asks to declare method in the interface "MyList". Once method is defined, now you have to define the method in the abstract class "MyAbstractList" which implements the interface "MyList". Method should be "public boolean addAll(MyList<E> otherlist)". What this method should do is that it should add elements of some other list to your original list. Let say you have elements in your original list as {1,2,3,4}, & you have one more list in which you have elements as {5,6}. Now the function should add elements of your other list in your original list i.e. original list should become {1,2,3,4,5,6,7}.Function should return true if this operation was successfull.


The final part of problem asks to add code to "TestMyArrayList.java" file to demonstrate that this new method to add two lists works perfectly. Also it asks to print the list.

The code provided is already correct.

interface MyList<E> extends java.lang.Iterable<E> {
/** Add a new element at the end of this list*/
public void add(E e);
  
/**Add a new element at the specified index in this list*/
public void add(int index, E e);
/**Clear the list*/
public void clear();
/**Return true if this list contains the element*/
public boolean contains(E e);
/**Return the element from this list at the specified index*/
public E get(int index);
/**Return the index of the first matching element in this list. Return -1 if
* no match.*/
public int indexOf(E e);
/**Return true if this list contains no elements*/
public boolean isEmpty();
/**Return the index of the last matching element in this list Return -1 if
* no match.*/
public int lastIndexOf(E e);
/**Remove the first occurrence of the element o from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.*/
public boolean remove(E e);
/**Remove the element at the specified position in this list Shift any
* subsequent elements to the left. Return the element that was removed from
* the list.*/
public E remove(int index);
/**Replace the element at the specified position in this list with the
* specified element and returns the new set.*/
public Object set(int index, E e);
/** Return the number of elements in this list */
public int size();
}
//MyAbsrtactList
abstract class MyAbstractList<E> implements MyList<E> {
protected int size = 0; // The size of the list
/**Create a default list*/
protected MyAbstractList() {
}
/**Create a list from an array of objects*/
protected MyAbstractList(E[] objects) {
for (int i = 0; i < objects.length; i++) {
add(objects[i]);
}
}
@Override
/**Add a new element at the end of this list*/
public void add(E e) {
add(size, e);
}
@Override
/**Return true if this list contains no elements*/
public boolean isEmpty() {
return size == 0;
}
@Override
/**Return the number of elements in this list*/
public int size() {
return size;
}
@Override
/**Remove the first occurrence of the element e from this list. Shift any
* subsequent elements to the left. Return true if the element is removed.
*/
public boolean remove(E e) {
if (indexOf(e) >= 0) {
remove(indexOf(e));
return true;
} else {
return false;
}
}
  
public boolean addAll(MyList<E> otherList) {
for (int i=0; i<otherList.size(); i++) {
add(otherList.get(i));
}
return !otherList.isEmpty();
}
  
public boolean removeAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<otherList.size(); i++) {
while (remove(otherList.get(i))) {
returnValue = true;
}
}
return returnValue;
}
  
public boolean retainAll(MyList<E> otherList) {
boolean returnValue = false;
for (int i=0; i<size(); i++) {
while (!otherList.contains(get(i))) {
remove(i);
returnValue = true;
}
}
return returnValue;
}
  
}
//TestMyArrayList
public class HelloWorld {
public static void main(String[] args) {
// Create a list
MyList<String> list = new MyAbstractList<>();
// Add elements to the list
list.add("America"); // Add it to the list
System.out.println("(1) " + list);
list.add(0,"Canada"); // Add it to the beginning of the list
System.out.println("(2) " + list);
list.add("Russia"); // Add it to the end of the list
System.out.println("(3) " + list);
list.add("France"); // Add it to the end of the list
System.out.println("(4) " + list);
list.add(2, "Germany"); // Add it to the list at index 2
System.out.println("(5) " + list);
list.add(5, "Norway"); // Add it to the list at index 5
System.out.println("(6) " + list);
// Remove elements from the list
list.remove("Canada"); // Same as list.remove(0) in this case
System.out.println("(7) " + list);
list.remove(2); // Remove the element at index 2
System.out.println("(8) " + list);
list.remove(list.size() - 1); // Remove the last element
System.out.print("(9) " + list + " (10) ");
for (String s : list) {
System.out.print(s.toUpperCase() + " ");
}
}
}