Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

*Java* ArrayList a generic list collection that is backed by an Object array that stored in contiguous memory locations. In addition to an Object array,

*Java*

ArrayList a generic list collection that is backed by an Object array that stored in contiguous memory locations. In addition to an Object array, the list should have a constant DEFAULT_CAPACITY of 10 elements and integer fields for its length (the number of items currently stored in it) and capacity (the max number of elements it can hold). The underlying array must be able to expand when new items are added to it that would exceed the maximum capacity. When this occurs, the array should grow by the default size, e.g., an ArrayList with a default size of 10 should expand to have a capacity of 20 when the 11th element is added to it. The Iterator for this class must implement the methods hasNext() and next().

The ArrayList must implement the methods:

public int size()

public boolean isEmpty()

public boolean contains(Object o)

public Iterator iterator()

public boolean add(E e)

public boolean remove(Object o)

public boolean addAll(Collection c)

public boolean addAll(int index, Collection c)

public void clear()

public E get(int index)

public E set(int index, E element)

public void add(int index, E element)

public E remove(int index)

public int indexOf(Object o)

public int lastIndexOf(Object o)

public List subList(int fromIndex, int toIndex)

The Test file named ArrayListTest.java:

ackage test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import java.util.Iterator; import java.util.List; import org.junit.jupiter.api.Test; import lists.ArrayList; class ArrayListTest { @Test void add_ExpectListSizeToIncreaseBy1() { var list = new ArrayList(); var expectedSize = 1; list.add(5); assertEquals(expectedSize, list.size()); } @Test void add_addTwo_ExpectListSizeToIncreaseBy2() { var list = new ArrayList(); var expectedSize = 2; list.add(5); list.add(10); assertEquals(expectedSize, list.size()); } @Test void add_addThree_ExpectThemAddedToEnd() { var list = new ArrayList(); var expectedOrder = List.of(5, 10, 15); list.add(5); list.add(10); list.add(15); assertEquals(expectedOrder, list); } @Test void add_atSpecificIndex_ExpectIndexToContainValue() { var list = new ArrayList(); var expectedSize = 3; list.add("Hello"); list.add("World"); list.add(1, ", "); assertEquals(expectedSize, list.size()); assertEquals(", ", list.get(1)); } @Test void add_atIndex0_ExpectListInReverseOrder() { var list = new ArrayList(); var expectedOrder = List.of("Geralt", "of", "Rivia"); list.add(0, "Rivia"); list.add(0, "of"); list.add(0, "Geralt"); assertEquals(expectedOrder, list); } @Test void addAll_ShouldAddAllItemsToEnd() { var list = new ArrayList(); var toAdd = List.of(3, 4, 5, 6); var expectedSize = 6; list.add(1); list.add(2); list.addAll(toAdd); assertEquals(expectedSize, list.size()); var added = list.subList(2, 6); assertEquals(toAdd, added); } @Test void addAll_AtSpecificIndex_ShouldAddAllItemsAtIndex() { var list = new ArrayList(); var toAdd = List.of(3, 4, 5, 6); var expectedSize = 6; list.add(1); list.add(2); list.addAll(1, toAdd); assertEquals(expectedSize, list.size()); var added = list.subList(1, 5); assertEquals(toAdd, added); } @Test void clear_ShouldMakeListSizeZero() { var list = new ArrayList(); var expectedSize = 0; list.add(1); list.add(2); list.clear(); assertEquals(expectedSize, list.size()); } @Test void contains_ItemIsInList_ShouldReturnTrue() { var list = new ArrayList(); var item = "42"; list.add(String.valueOf(item)); list.add("Hello World"); assertTrue(list.contains(item)); } @Test void contains_ItemIsNotInList_ShouldReturnFalse() { var list = new ArrayList(); var item = "42"; list.add("39"); list.add("Hello World"); assertFalse(list.contains(item)); } @Test void indexOf_ItemIsInList_ShouldReturnIndex() { var list = new ArrayList(); var expectedIndex = 4; var value = 42; list.add(0); list.add(1); list.add(2); list.add(3); list.add(value); list.add(5); assertEquals(expectedIndex, list.indexOf(value)); } @Test void indexOf_ItemIsNotInList_ShouldReturnNegative1() { var list = new ArrayList(); var expectedIndex = -1; var value = 39; list.add(0); list.add(1); list.add(2); list.add(3); list.add(4); list.add(5); assertEquals(expectedIndex, list.indexOf(value)); } @Test void isEmpty_EmptyList_ShouldReturnTrue() { var list = new ArrayList(); assertTrue(list.isEmpty()); } @Test void isEmpty_ListHasItems_ShouldReturnFalse() { var list = new ArrayList(); list.add("hello"); list.add("world"); assertFalse(list.isEmpty()); } @Test void lastIndexOf_ItemAppearsMultipleTimes_ShouldReturnIndex() { var list = new ArrayList(); var expectedIndex = 5; var value = 42; var toAdd = List.of(0, 1, value, value, 4, value, 6, 7, 8, 9, 10); list.addAll(toAdd); assertEquals(expectedIndex, list.lastIndexOf(value)); } @Test void lastIndexOf_ItemDoesNotAppear_ShouldReturnIndex() { var list = new ArrayList(); var expectedIndex = -1; var value = 42; var toAdd = List.of(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); list.addAll(toAdd); assertEquals(expectedIndex, list.lastIndexOf(value)); } @Test void remove_Index_ShouldRemoveItemAtIndex() { var list = new ArrayList(); var removeAt = 0; var value = "hello"; var toAdd = List.of(value, "world", "I'm", "java"); list.addAll(toAdd); var sizeBeforeRemove = list.size(); var valueBeforeRemove = list.get(removeAt); var removed = list.remove(removeAt); var valueAfterRemove = list.get(removeAt); assertEquals(value, removed); assertNotEquals(valueBeforeRemove, valueAfterRemove); assertEquals(sizeBeforeRemove - 1, list.size()); } @Test void remove_IndexDoesNotExist_ShouldReturnNull() { var list = new ArrayList(); var removeAt = 42; var value = "hello"; var toAdd = List.of(value, "world", "I'm", "java"); list.addAll(toAdd); var sizeBeforeRemove = list.size(); var removed = list.remove(removeAt); assertNull(removed); assertEquals(sizeBeforeRemove, list.size()); } @Test void remove_ObjectParam_ShouldReturnTrue() { var list = new ArrayList(); var removeAt = 0; var value = "hello"; var toAdd = List.of(value, "world", "I'm", "java"); list.addAll(toAdd); var sizeBeforeRemove = list.size(); var valueBeforeRemove = list.get(removeAt); assertTrue(list.remove(value)); var valueAfterRemove = list.get(removeAt); assertNotEquals(valueBeforeRemove, valueAfterRemove); assertEquals(sizeBeforeRemove - 1, list.size()); } @Test void iterator_EmptyListHasNext_ShouldReturnFalse() { var list = new ArrayList(); Iterator itr = list.iterator(); assertEquals(itr.hasNext(), false); } @Test void iterator_FullListHasNext_ShouldReturnTrue() { var list = new ArrayList(); var toAdd = List.of("Sterling", "Goodfellow", "Iacas", "Tonwen"); list.addAll(toAdd); Iterator itr = list.iterator(); assertEquals(itr.hasNext(), true); } @Test void iterator_FullListNext_ShouldGetItem() { var list = new ArrayList(); var toAdd = List.of("Sterling", "Goodfellow", "Iacas", "Tonwen"); list.addAll(toAdd); Iterator itrToAdd = toAdd.iterator(); for (String element : list) assertEquals(element, itrToAdd.next()); } }

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

More Books

Students also viewed these Databases questions