Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Define the following method in MyList and implement it in MyAbstractList. // Adds the elements in otherList to this list. // Returns true if //

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 otherlist)

*********Add code to TestMyArrayList.java to demonstrate that this method works correctly. Display the modified list using a java.util.Iterator object and a while loop.********

//MYLIST

package listapi;

/** * @author Y. Daniel Liang */ public interface MyList 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();

//MYABSTRACTLIST

package listapi;

/** * @author Y. Daniel Liang */ public abstract class MyAbstractList implements MyList {

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

//TESTMYARRAYLIST

package listapi; import java.util.Iterator;

/** * @author Y. Daniel Liang */ public class TestMyArrayList {

public TestMyArrayList() { // Create a list MyList 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() + " "); } }

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

Students also viewed these Databases questions

Question

How has this been a breakthrough for you?

Answered: 1 week ago