Ex. 02 : ArrayList About Instructions : Create the following Array List Data Structure in your utility package and use "for loops" for your repetitive tasks. Where to find starter code in my-api package.class : utility.ArrayList package.class : testing.ArrayListTest What has to be added to my-api package.interface : utility.List Where to find unit test in my-api package.class : tests.ArrayListJUnitTest Task Check List Place a SCREENSHOT of your results (pass/fail) JUNIT Tests for your Array List in your report. ONLY "for" loops should be used within the data structure class. Names of identifiers MUST match the names listed in the description below. Deductions otherwise. Description This Array List Data Structure operates like a dynamic array , which can grow or reduce its size. Think through the class construction so that it meets the following specifications: data fields: The fields to declare have private accessibility. size : stores the number of occupied locations in internal array, and is of type int. data : is a reference variable for the internal array and is of type E[ ]. constant: Use to describe defaults. DEFAULT_CAPACITY is a constant that holds a default capacity of ten (10) for the underlying array, is of type int , public accessibility and modified to static. constructors: The overloaded constructors will initialize the data fields size and data. The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY. public ArrayList() The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException. @SuppressWarnings("unchecked") public ArrayList(int capacity) Note: Java does not allow you to construct a generic array i.e data = new E[capacity]; // illegal so, you must use data = (E[])new Object[capacity]; // valid There is no real way to get around this, because Java won’t let you construct a generic array. Therefore, you can ignore this warning and can include the annotation @SuppressWarnings("unchecked") to indicate that you don’t want to generate the warning for this method. methods: manages the behavior of the internal array. Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class. Method Description Header add(item) appends the item specified to the end of the list. This method returns true, if the data was added successfully. public boolean add(E item) add(index, item) inserts the item specified at the given index in the list. Shifts subsequent elements to the right. public void add(int index, E item) checkIndex(index) checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index. private void checkIndex(int index) clear() clears list of all elements, returns size back to zero. public void clear() contains(item) searches for a specific value within the internal array and returns true, if the value is in the list. public boolean contains(E item) ensureCapacity(int capacity) doubles the capacity of the underlying array, if necessary, to ensure that it can hold the number of elements specified by the capacity argument. This will most likely have a cast to a generic type. @SuppressWarnings("unchecked") public void ensureCapacity(int capacity) get(index) returns the item at the specified position in the list. This method first checks if the index requested is valid. public E get(int index) indexOf(item) searches for a specific item within the linked structure and returns the first occurrence (location/index) in the array, otherwise returns -1, if NOT found. public int indexOf(E item) isEmpty() returns true, if the list is empty, i.e., the list contains no elements. public boolean isEmpty() remove(index) removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. public E remove(int index) remove(item) removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. public boolean remove(E item) set(index, item) replaces the item at the specified position with the one specified. This method validates the index before replacing the item. public E set(int index, E item) shiftLeft(index) helper method that shifts elements of internal array left to index location. private void shiftLeft(int index) shiftRight(index) helper method that shifts elements of internal array right from index location. private void shiftRight(int index) size() returns the number of elements in the list. This is the size of the occupied locations of the array. public int size() toString() displays the contents of the list. public String toString()
Ex. 02 : ArrayList<E>
About
Instructions : Create the following Array List Data Structure in your utility package and use "for loops" for your repetitive tasks.
Where to find starter code in my-api
package.class : utility.ArrayList
package.class : testing.ArrayListTest
What has to be added to my-api
package.interface : utility.List
Where to find unit test in my-api
package.class : tests.ArrayListJUnitTest
Task Check List
- Place a SCREENSHOT of your results (pass/fail) JUNIT Tests for your Array List in your report.
- ONLY "for" loops should be used within the data structure class.
- Names of identifiers MUST match the names listed in the description below. Deductions otherwise.
Description
This Array List Data Structure operates like a dynamic array , which can grow or reduce its size. Think through the class construction so that it meets the following specifications:
data fields: The fields to declare have private accessibility.
- size : stores the number of occupied locations in internal array, and is of type int.
- data : is a reference variable for the internal array and is of type E[ ].
constant: Use to describe defaults.
- DEFAULT_CAPACITY is a constant that holds a default capacity of ten (10) for the underlying array, is of type int , public accessibility and modified to static.
constructors: The overloaded constructors will initialize the data fields size and data.
- The default constructor calls the second constructor, generating an internal array with the specified DEFAULT_CAPACITY.
- The second constructor generates an internal array with the specified initial capacity. If the capacity is less than zero, this constructor should throw an IllegalArgumentException.
public ArrayList(int capacity)
Note: Java does not allow you to construct a generic array i.e
data = new E[capacity]; // illegal
so, you must use
data = (E[])new Object[capacity]; // valid
methods: manages the behavior of the internal array.
Together, the methods below give the illusion of a dynamic array (an array that grows or shrinks). Implement these methods with in your generic Array List class.
Method |
Description |
Header |
add(item) | appends the item specified to the end of the list. This method returns true, if the data was added successfully. |
public boolean add(E item) |
add(index, item) | inserts the item specified at the given index in the list. Shifts subsequent elements to the right. |
public void add(int index, E item) |
checkIndex(index) |
checks if the given index is valid. Throws an IndexOutOfBoundsException, if invalid. This is a private helper method. Note: This method should be used in any method that requires a precondition for access to valid index. |
private void checkIndex(int index) |
clear() |
clears list of all elements, returns size back to zero. |
public void clear() |
contains(item) | searches for a specific value within the internal array and returns true, if the value is in the list. |
public boolean contains(E item) |
ensureCapacity(int capacity) | doubles the capacity of the underlying array, if necessary, to ensure that it can hold the number of elements specified by the capacity argument. This will most likely have a cast to a generic type. |
@SuppressWarnings("unchecked") public void ensureCapacity(int capacity) |
get(index) | returns the item at the specified position in the list. This method first checks if the index requested is valid. |
public E get(int index) |
indexOf(item) | searches for a specific item within the linked structure and returns the first occurrence (location/index) in the array, otherwise returns -1, if NOT found. |
public int indexOf(E item) |
isEmpty() |
returns true, if the list is empty, i.e., the list contains no elements. |
public boolean isEmpty() |
remove(index) | removes the item at the given index in the list. Shifts subsequent elements to the left and returns the item removed. |
public E remove(int index) |
remove(item) | removes the first occurrence of the item specified from the list, if present. Shifts subsequent elements to the left and returns true, if the item is removed. |
public boolean remove(E item) |
set(index, item) | replaces the item at the specified position with the one specified. This method validates the index before replacing the item. |
public E set(int index, E item) |
shiftLeft(index) |
helper method that shifts elements of internal array left to index location. |
private void shiftLeft(int index) |
shiftRight(index) |
helper method that shifts elements of internal array right from index location. |
private void shiftRight(int index)
|
size() | returns the number of elements in the list. This is the size of the occupied locations of the array. |
public int size() |
toString() | displays the contents of the list. |
public String toString() |
Trending now
This is a popular solution!
Step by step
Solved in 2 steps