Sale!

Homework 6: Array Lists and Linked Lists

$30.00

Category:

Description

COMP 2150 –
Homework 6: Array Lists and Linked Lists
(25 points)
Add the following methods to the ArrayList<E> class that we wrote during lecture (code posted on Oct. 25). You may
call the existing methods in
ArrayList<E> if you want, but do not use anything from the built-in
java.util.ArrayList<E> class.
1.
(3 pts) Write a new method named addAll(ArrayList<E> 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.
2.
(4 pts) Write a new method named trimToSize() that changes the capacity of the calling list to the list’s
current size. Example: If
list1 is an ArrayList<Integer> with 4 elements and a capacity of 7, calling
list1.trimToSize() should change its capacity to 4.
3.
(5 pts) Write a new method named slice(int beginIndex, int endIndex) that returns a new
ArrayList<E> 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<Integer> object containing {1, 2, 3, 3, 6, 2, 2, 3, 1, 4}, then
calling
list1.slice(4,7) should return a new ArrayList<Integer> 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<E> class that we wrote during lecture (code posted on Oct. 25). You may
call the existing methods in
LinkedList<E> if you want, but do not use anything from the built-in
java.util.LinkedList<E> class.
4.
(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.
5.
(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, it’s
acceptable for this assignment. But if possible, try to make your solution run in
O(n) time.)
6.
(4 pts) Write a new method named toArrayList() that returns an ArrayList<E> object containing all
elements in the calling list, in the same order (i.e., the head node’s data should be stored in index 0 of the returned
array list). Use your
ArrayList<E> class from the first part of this assignment. If the calling list is empty, just
return an
ArrayList<E> of size 0.
Number of People: Individual. Feel free to ask me for help, or visit the Computer Science Learning Center
(
http://www.memphis.edu/cs/current_students/cslc.php).
Due: Wed., Nov. 1 by 5:30 pm
Submission: Zip all your Java source files (you can zip the entire project folder if using an IDE) into a single file and
upload it to the proper folder in the eCourseware dropbox at
https://elearn.memphis.edu.
Coding Style: Use consistent indentation. Use standard Java naming conventions for
variableAndMethodNames,
ClassNames, CONSTANT_NAMES. Include a reasonable amount of comments.
Grader: Lasang Tamang,
[email protected]. Questions about grading? Please contact him first!
#protips
As usual, be sure to test all of your methods thoroughly. When working with lists, you should always consider cases of
performing an action on:
An empty list
A list containing exactly one element
Something at the front of the list
Something at the back of the list
Something in the middle of the list