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

intro java HW In this assignment you will extend the MyArrayList class by comple

ID: 645516 • Letter: I

Question

intro java HW

In this assignment you will extend the MyArrayList class by completing items 1 and 2.
Write your methods in MyArrayList.java that is posted on Blackboard. Do not use the
version you submitted for HW2. In addition, use the updated version of the
MyCollection interface. Note that this version defines the methods defined in item 1.
Submit the MyArrayList.java file via Blackboard. Make sure your code conforms to the
programming standards posted online.
(1) Implement the following methods in the MyCollection interface
(a)public boolean addAll(MyCollection c)
If c is null, then do nothing. Otherwise, add all items in the MyCollection object to the
end of the MyArrayList object. Always return true.
(b) public boolean containsAll(MyCollection c)
If c is null, then return false.
Otherwise, if all items stored in the MyCollection object are contained in the MyArrayList
object then return true. If at least one item in the MyCollection object is not contained
in the MyArrayList object, return false.
(c) public boolean removeAll(MyCollection c)
If c is null, then return false.
Otherwise, if an item is contained in the MyCollection object then remove all
occurences of this item from the MyArrayList object.
Return true if at least one item is removed from the MyArrayList object. Otherwise,
return false.
Example: Assume the MyArrayList object contains the following String objects:
"A", "B", "C", "A", "E" and the MyCollection object contains the following String objects:
"A", "C", "C", "D".
After calling the removeAll method, the MyArrayList object contains the following items:
"B", "E"HW3 Math 140 Due Date 3/25/2015 11:59 pm No Late Assignments
(d) public boolean retainAll(MyCollection c)
If c is null, then return false.
Otherwise, remove any items from the MyArrayList object that is not contained in the
MyCollection object. Return true if at least one item is removed from the MyArrayList
object. Otherwise, return false.
Example: Assume the MyArrayList object contains the following String objects:
"A", "B", "C", "A", "E" and the MyCollection object contains the following String objects:
"A", "C", "C", "D".
After calling the retainAll method, the MyArrayList object contains the following items:
"A", "C", "A"
(2) Implement the following method. (This method is not in the MyCollection
interface.)
public Iterable subList(int i, int j)
The methods throws an IndexOutOfBounds exception if i < 0, j < 0, i ? size, or
j ? size. Otherwise, return an Iterable object that contains every element of the
MyArrayList object starting from position i and ending at position j.

use these code above please

Explanation / Answer

Answer:

import java.util.Iterator;

public class HWThree

{

    public static void display(Iterable c)

   {

        System.out.println("Display List");

        for (Iterator i = c.iterator(); i.hasNext();)

    {

            System.out.println(i.next());

       }

       System.out.println();

    }

   public static void main(String[] args)

    {

            MyArrayList arr = new MyArrayList();

            for (int i = 1; i <= 5; i++) {

            arr.add("" + i);

       }

    display(arr);

    try

    {

           display(arr.subList(-2, 2));

      }

    catch (IndexOutOfBoundsException ex)

    {

            System.out.println("Index out of bounds");

     }

    try

    {

     display(arr.subList(0, 5));

     }

    catch (IndexOutOfBoundsException ex)

    {

            System.out.println("Index out of bounds");

     }

    try

    {

            display(arr.subList(1, 4));

     }

    catch (IndexOutOfBoundsException ex)

    {

            System.out.println("Index out of bounds");

     }

    MyArrayList arr2 = new MyArrayList<>();

     for (int i = 6; i <= 10; i++)

    {

            arr2.add("" + i);

     }

     arr.addAll(arr2);

   display(arr);

    arr2 = null;

     arr.addAll(arr2);

     display(arr);

     boolean change = arr.removeAll(null);

     if (change)

    {

                  System.out.println("Items removed from the list");

     }

    else

    {

            System.out.println("No change to list");

     }

     display(arr);

       

        arr2 = new MyArrayList<>();

        for (int i = 6; i <= 10; i++)

     {

            arr2.add("" + i);

        }

        arr.addAll(arr2);

     display(arr);

        change = arr.removeAll(arr2);

        if (change)

     {

            System.out.println("Items removed from the list");

        }

     else

     {

            System.out.println("No change to list");

       }

       display(arr);

     arr2 = new MyArrayList<>();

        for (int i = 1; i <= 2; i++) {

            arr2.add("" + i);

        }

        arr.addAll(arr2);

        display(arr);

        change = arr.retainAll(arr2);

        if (change) {

            System.out.println("Items removed from the list");

        } else {

            System.out.println("No change to list");

        }

        display(arr);

        change = arr.retainAll(arr2);

        if (change) {

            System.out.println("Items removed from the list");

        } else {

            System.out.println("No change to list");

        }

        display(arr);

       

        change = arr.retainAll(null);

        if (change) {

            System.out.println("Items removed from the list");

        } else {

            System.out.println("No change to list");

        }

        display(arr);

        arr2 = new MyArrayList<>();

        for (int i = 3; i <= 4; i++) {

            arr2.add("" + i);

        }

        change = arr.removeAll(arr2);

        if (change) {

            System.out.println("Items removed from the list");

        } else {

            System.out.println("No change to list");

        }

        display(arr);

        arr2 = null;

        change = arr.removeAll(arr2);

        if (change) {

            System.out.println("Items removed from the list");

        } else {

            System.out.println("No change to list");

        }

        arr2 = new MyArrayList<>();

        for (int i = 1; i <= 2; i++) {

            arr2.add("" + i);

        }

        if (arr.containsAll(arr2)) {

            System.out.println("Every item in arr2 in the list");

        } else {

            System.out.println("At least one item in arr2 is not in the list");

        }

        System.out.println();

        arr2.add("3");

        if (arr.containsAll(arr2)) {

            System.out.println("Every item in arr2 in the list");

        } else {

            System.out.println("At least one item in arr2 is not in the list");

        }

        arr2 = null;

        if (arr.containsAll(arr2)) {

            System.out.println("Every item in arr2 in the list");

        } else {

            System.out.println("At least one item in arr2 is not in the list");

        }

    }

}

public interface MyCollection extends Iterable{

   

    default int size(){

        return 0;

    }

   

    default E get(int i){

        return null;

    }

   

    default boolean add(E obj){

        return true;

    }

   

    default void add(int i, E obj){

       

    }

   

    default boolean remove(Object obj){

        return false;

    }

   

    default boolean contains(Object obj){

        return false;

    }

   

    default boolean isEmpty(){

        return false;

    }

   

    public boolean addAll(MyCollection c)

    {            

            return add(c);

    }

   

    public boolean containsAll(MyCollection c)

    {

            Return contain(c);

    }

    public boolean removeAll(MyCollection c)

    {

      Return remove(c);

    }

     public boolean retainAll(MyCollection c)

    {

            return remove(c);

    }

}

import java.util.ArrayList;

import java.util.Iterator;

public class MyArrayList implements MyCollection {

    private E[] list;

    private int size = 0;

    private int capacity;

    public MyArrayList() {

        this(10);

    }

    public MyArrayList(int capacity) {

        list = (E[]) new Object[capacity];

        this.capacity = capacity;

    }

    public boolean isEmpty() {

        return size == 0;

    }

    public int size() {

        return size;

    }

    public E get(int i) {

      if (i < 0 || i >= size) {

            throw new IndexOutOfBoundsException();

        }

        return list[i];

    }

    public boolean contains(Object o) {

        boolean found = false;

        int index = 0;

        while (index < size && !found) {

            if (list[index] == null) {

                if (o == null) {

                    found = true;

                }

            } else {

                found = list[index].equals(o);

            }

            index++;

        }

        return found;

    }

    public boolean add(E item) {

        if (size >= capacity) {

            ensureCapacity(2 * capacity);

        }

        // System.out.println("size = " + size);

        list[size++] = item;

        return true;

    }

    public void add(int index, E item) {

        if (index < 0 || index > size) {

            throw new IndexOutOfBoundsException();

        }

        if (size >= capacity) {

            ensureCapacity(2 * capacity);

        }

        System.arraycopy(list, index, list, index + 1, size - index);

        list[index] = item;

        size++;

    }

    private void ensureCapacity(int minCapacity) {

        if (minCapacity > size) {

            E[] tmp = (E[]) new Object[minCapacity];

            System.arraycopy(list, 0, tmp, 0, list.length);

            capacity = minCapacity;

            list = tmp;

        }

    }

    public boolean remove(Object o) {

        boolean removed = false;

        int index = 0;

        while (index < size && !removed) {

            if (list[index] == null) {

                if (o == null) {

                    removed = true;

                }

            } else {

                removed = list[index].equals(o);

            }

            if (!removed) {

                index++;

            }

        }

        if (removed) {

            System.arraycopy(list, index + 1, list, index, size - index - 1);

            size--;

        }

        return removed;

    }

    public Iterator iterator() {

        return new ArrayListIterator();

    }

@override

public boolean addAll(MyCollection c)

{

    if(c!=null)

    {

            Iterator it=c.Iterator();

            while(c!=null)

                  this.add(it.next());

    }

   

    return true;

   

}

public boolean containsAll(MyCollection c)

{

    if(c==null)

            return false;

    else

     return this.contains(c);

}

public boolean removeAll(MyCollection c)

{

    if(c==null)

            return false;

     else

    {

            return this.remove(c);

    }

}

public boolean retainAll(MyCollection c)

{

     if(c==null)

            return false;

     else

    {

            return this.remove(c);

    }

}

public Iterable subList(int i,int j)

{

    Try

    {

            MyArrayList list1[]=list.subList(i,j);

    }

    Catch(ArrayOutOfBoundException e)

    {

            Print(e);

    }

}   

   

    private class ArrayListIterator implements Iterator {

        private int index = 0;

        public boolean hasNext() {

            return index != size;

        }

        public E next() {

            E item = list[index];

            index++;

            return item;

        }

    }

}