Question: [ *!!!*JAVA *!!!*] Add the following methods to the ArrayList class . This homework is over ArrayLists and LinkedLists, WITHOUT using any built-in methods. Any

[*!!!*JAVA*!!!*] Add the following methods to the ArrayList class.

This homework is over ArrayLists and LinkedLists, WITHOUT using any built-in methods. Any resource I try to use calls java.util.ArrayList which does me absolutely NO good whatsoever. I'm having trouble understanding the proper way to set it up. I'm not very good with Java, but I can make due.

THE FOLLOWING ARE INSTRUCTIONS FOR THE HOMEWORK

*******Add the following methods to the ArrayList class that we wrote during lecture (code posted on Oct. 25). You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. ********

(3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified.

(4 pts) Write a new method named trimToSize() that changes the capacity of the calling list to the lists current size. Example: If list1 is an ArrayList with 4 elements and a capacity of 7, calling list1.trimToSize() should change its capacity to 4.

(5 pts) Write a new method named slice(int beginIndex, int endIndex) that returns a new ArrayList object containing the elements of the calling list between beginIndex (inclusive) and endIndex (exclusive). The calling list should not be modified. This method should throw an IndexOutOfBoundsException if an invalid index is supplied, or if beginIndex is not at least 1 less than endIndex. Example: If list1 is an ArrayList object containing {1, 2, 3, 3, 6, 2, 2, 3, 1, 4}, then calling list1.slice(4,7) should return a new ArrayList object containing the values at indices 4, 5, and 6. The returned list would contain the elements {6, 2, 2}.

********Add the following methods to the LinkedList class that we wrote during lecture (code posted on Oct. 25). You may call the existing methods in LinkedList if you want, but do not use anything from the built-in java.util.LinkedList class. *********

(3 pts) Write a new method named remove(E item) that removes and returns the first item in the list that is equivalent to the specified object. If the list does not contain such an item, the method should return null.

(6 pts) Write a new method named reverse() that reverses the order of the nodes in the calling list. (There are several ways you can do this, some of which are more efficient than others! As long as your solution works, its acceptable for this assignment. But if possible, try to make your solution run in O(n) time.)

(4 pts) Write a new method named toArrayList() that returns an ArrayList object containing all elements in the calling list, in the same order (i.e., the head nodes data should be stored in index 0 of the returned array list). Use your ArrayList class from the first part of this assignment. If the calling list is empty, just return an ArrayList of size 0.

THE FOLLOWING IS THE CODE CREATED FROM THE AFOREMENTIONED LECTURE

ArrayList

NOTICE HOW THERE IS NO "IMPORT JAVA.UTIL.ARRAYLIST;" ????? THAT'S ON PURPOSE!

public class ArrayList implements List { private E[] data = (E[])(new Object[3]); private int size = 0; public E get(int index) { if (index >= 0 && index < size) return data[index]; else { System.out.println("You can't do that!"); throw new IndexOutOfBoundsException(); } } public void set(int index, E newValue) { if (index >= 0 && index < size) data[index] = newValue; else { System.out.println("You can't do that!"); throw new IndexOutOfBoundsException(); } } public void add(E newValue) { if (size == data.length) { E[] newData = (E[])(new Object[data.length * 2]); for (int i = 0; i < data.length; i++) newData[i] = data[i]; data = newData; } data[size] = newValue; size++; } public boolean addAll(ArrayList anotherList) { int arraySize = data.length; } public E remove(int index) { if (index >= 0 && index < size) { E thingToReturn = data[index]; for (int i = index; i < size - 1; i++) data[i] = data[i+1]; size--; return thingToReturn; } else { System.out.println("You can't do that!"); throw new IndexOutOfBoundsException(); } } public String toString() { String r = "ArrayList (size = " + size + ", capacity = " + data.length + "), containing the following: "; for (int i = 0; i < size; i++) r += data[i] + " "; return r; }

}

LinkedList

SAME FORMAT AS BEFORE, NO "IMPORT JAVA.UTIL.LINKEDLIST"!!!!

public class LinkedList implements List { private static class Node { private E data; private Node next; public Node(E data, Node next) { super(); this.data = data; this.next = next; } } private Node head; public E get(int index) { return nodeAt(index).data; } public void set(int index, E newValue) { nodeAt(index).data = newValue; } public void add(E newValue) { if (size == 0) head = new Node<>(newValue, null); else nodeAt(size - 1).next = new Node<>(newValue, null); size++; } public E remove(int index) { E temp; if (index == 0) { temp = head.data; head = head.next; } else { Node nodeBefore = nodeAt(index - 1); temp = nodeBefore.next.data; nodeBefore.next = nodeBefore.next.next; } size--; return temp; } private Node nodeAt(int index) { if (index >= 0 && index < size) { Node temp = head; for (int i = 0; i < index; i++) temp = temp.next; return temp; } else throw new IndexOutOfBoundsException(); }

public String toString() { String r = "LinkedList (size = " + size + "), containing: head -> "; for (Node temp = head; temp != null; temp = temp.next) r += temp.data + " -> "; r += "null"; return r; }

List Interface

public interface List { E get(int index); void set(int index, E newValue); void add(E newValue); E remove(int index); }

Now, some of this may be staring me right in the face and that's okay. I have been behind in my class for awhile and am struggling to keep up. If I can't get any answers, that's fine. I'm not one to stress over timely answers or even unhelpful answers. I appreciate any and all effort that anybody decides to impart onto me. I thank anyone and everyone for their input. Of course, the correct input is the most appreciated.

Thank you for your time.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!