1. You are given 5 source files:

  • MyList: interface
  • MyAbstractList: Abstract class
  • MyArrayList: Concrete class
  • MyLinkedList: Concrete class
  • TestMyLists: Concrete class

2. The two concrete classes MyArrayList and MyLinkedList implement the abstract class, which extends the interface. However, the two concrete classes have some of their methods missing their bodies (the body will contain the following comment: // COMPLETE THIS METHOD. Based on what we learned in class, complete the bodies of methods in both classes MyArrayList and MyLinkedList.

3. Use the concrete class TestMyLists to test your implementation

MyList

public interface MyList< E > extends java.lang.Iterable {
/** 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();

/** Return an iterator for the list */
public java.util.Iterator< E > iterator();
}

MyAbstractList


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;
}
}
}

TestMyList

public class TestMyLists {

public static void main(String[] args) {
// Create a list
MyList< String > arraylist = new MyArrayList< String >();

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

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

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

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

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

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

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

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

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

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

MyList< String > mylinkedlist = new MyLinkedList< >();

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

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

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

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

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

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

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

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

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

for (String s : mylinkedlist) {
System.out.print(s.toUpperCase() + " ");
}
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.