From e3cef89bf996efe9b664d0399f9466e37932b3dc Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Tue, 13 Feb 2024 04:17:30 -0800 Subject: [PATCH 01/52] Created ArrayList class implementing List - Added helper fields - Implemented size and isEmpty methods --- src/ArrayList.java | 149 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/ArrayList.java diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..b7d39e1 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,149 @@ +import java.util.Iterator; + +public class ArrayList implements List { + /** + * An array used to store values placed within the ArrayList + */ + private E[] buffer; + + /** + * The number of values stored within buffer + */ + private int size; + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + + } + + /** + * Add an item at specified index (position). + * + * @param i the index where the item should be added + * @param item the item to be added + */ + @Override + public void add(int i, E item) { + + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + return null; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param i the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int i, E item) { + + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + return null; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + + } + + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int i) { + return null; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + return false; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} \ No newline at end of file From 6320b7cfa6969cedae113f926113eb37749435aa Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Tue, 13 Feb 2024 04:23:01 -0800 Subject: [PATCH 02/52] Created test directory and setup test file for ArrayList --- SDEV333-Term-Project.iml | 17 ++++++++++++ tests/ArrayListTest.java | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 tests/ArrayListTest.java diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..0f07f6c 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -4,8 +4,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java new file mode 100644 index 0000000..122c74c --- /dev/null +++ b/tests/ArrayListTest.java @@ -0,0 +1,56 @@ +import org.junit.jupiter.api.Test; + +class ArrayListTest { + /** + * Create ArrayList at start for testing + */ + private final ArrayList testArrayList = new ArrayList<>(); + + @Test + void addFront() { + } + + @Test + void addBack() { + } + + @Test + void add() { + } + + @Test + void get() { + } + + @Test + void set() { + } + + @Test + void removeFront() { + } + + @Test + void removeBack() { + } + + @Test + void remove() { + } + + @Test + void testRemove() { + } + + @Test + void contains() { + } + + @Test + void isEmpty() { + } + + @Test + void size() { + } +} \ No newline at end of file From a592f018cd0d5efac4ebfa50cc1182175b8c37d6 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Tue, 13 Feb 2024 04:39:11 -0800 Subject: [PATCH 03/52] Implemented constructor for the ArrayList class - Initial implementation of addBack, addFront, and add methods - Updated all function parameters named i to index for clarity --- src/ArrayList.java | 71 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 10 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index b7d39e1..41aa2d2 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -11,6 +11,17 @@ public class ArrayList implements List { */ private int size; + /** + * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 values + */ + public ArrayList() { + // setup buffer with default max capacity of 10 + buffer = (E[]) new Object[10]; + + // no values are stored in buffer + size = 0; + } + /** * Add item to the front. * @@ -18,7 +29,23 @@ public class ArrayList implements List { */ @Override public void addFront(E item) { - + // if the buffer already contains items + if(size != 0) { + // if the buffer is full, increase max capacity + + // run through buffer backwards + for (int i = size; i > 0; i--) { + // get item at previous index and place in current index, + // thereby shifting all items to the left + buffer[i] = buffer[i - 1]; + } + } + + // add the given item at index 0 + buffer[0] = item; + + // a new item has been added to buffer + size++; } /** @@ -28,28 +55,52 @@ public void addFront(E item) { */ @Override public void addBack(E item) { + // if the buffer is full, increase max capacity + // add the given item at final index + buffer[size] = item; + + // a new item has been added to buffer + size++; } /** * Add an item at specified index (position). * - * @param i the index where the item should be added + * @param index the index where the item should be added * @param item the item to be added */ @Override - public void add(int i, E item) { - + public void add(int index, E item) { + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if the buffer is full, increase max capacity + + // run through buffer backwards, up to given index + for (int i = size; i > index; i--) { + // get item at previous index and place in current index, + // thereby shifting all items to the left + buffer[i] = buffer[i - 1]; + } + + // add the given item at current index + buffer[index] = item; + + // a new item has been added to buffer + size++; } /** * Get the item at a specified index. * - * @param i the index where the item should be retrieved + * @param index the index where the item should be retrieved * @return the item located at that index */ @Override - public E get(int i) { + public E get(int index) { return null; } @@ -57,11 +108,11 @@ public E get(int i) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * - * @param i the index where the item should be saved + * @param index the index where the item should be saved * @param item the item to be saved */ @Override - public void set(int i, E item) { + public void set(int index, E item) { } @@ -98,11 +149,11 @@ public void remove(E item) { /** * Remove item at a specified index. * - * @param i the index where the item should be removed + * @param index the index where the item should be removed * @return the item that was removed */ @Override - public E remove(int i) { + public E remove(int index) { return null; } From 4eb65c317267ad003b70a2d300ce5d0670020479 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Tue, 13 Feb 2024 04:47:24 -0800 Subject: [PATCH 04/52] Implemented ArrayList's get and set methods --- src/ArrayList.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 41aa2d2..d41ff21 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class ArrayList implements List { /** @@ -101,7 +102,17 @@ public void add(int index, E item) { */ @Override public E get(int index) { - return null; + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if buffer contains no items, one cannot be retrieved + if(size == 0) { + throw new NoSuchElementException("Cannot retrieve values from empty ArrayList"); + } + + return buffer[index]; } /** @@ -113,7 +124,18 @@ public E get(int index) { */ @Override public void set(int index, E item) { + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + // if buffer contains no items, one cannot be retrieved + if(size == 0 && index != 0) { + throw new NoSuchElementException("Cannot place item at given index of empty ArrayList"); + } + + // place the given item at the requested index + buffer[index] = item; } /** From 55463c2119ecd4f7da0378529091dd93c64567b5 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Tue, 13 Feb 2024 23:53:32 -0800 Subject: [PATCH 05/52] Implemented ArrayList's contains method --- src/ArrayList.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index d41ff21..ce1af13 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -5,7 +5,7 @@ public class ArrayList implements List { /** * An array used to store values placed within the ArrayList */ - private E[] buffer; + private final E[] buffer; /** * The number of values stored within buffer @@ -187,6 +187,16 @@ public E remove(int index) { */ @Override public boolean contains(E item) { + // run through buffer + for (int i = 0; i < size; i++) { + // check if item at current index of buffer is given item + if(item.equals(buffer[i])) { + // item was located + return true; + } + } + + // item could not be found return false; } From 13477ae9a1781b75d63703de8c5b9e9a93b085ab Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 00:47:32 -0800 Subject: [PATCH 06/52] Implemented ArrayList's removeFront, removeBack, remove item, and remove index methods --- src/ArrayList.java | 81 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 77 insertions(+), 4 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index ce1af13..bbdc936 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -83,7 +83,7 @@ public void add(int index, E item) { // run through buffer backwards, up to given index for (int i = size; i > index; i--) { // get item at previous index and place in current index, - // thereby shifting all items to the left + // thereby shifting all items to the right buffer[i] = buffer[i - 1]; } @@ -145,7 +145,24 @@ public void set(int index, E item) { */ @Override public E removeFront() { - return null; + // if buffer contains no items, one cannot be removed + if(size == 0) { + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + } + + // get requested item prior to removal + E requestedItem = buffer[0]; + + // run through buffer + for(int i = 0; i < size; i++) { + // replace item at current index with item at next index + buffer[i] = buffer[i + 1]; + } + + // account for removal of item + size--; + + return requestedItem; } /** @@ -155,7 +172,21 @@ public E removeFront() { */ @Override public E removeBack() { - return null; + // if buffer contains no items, one cannot be removed + if(size == 0) { + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + } + + // get requested item prior to removal, accounting for index + E requestedItem = buffer[size - 1]; + + // clear the final item in buffer + buffer[size - 1] = null; + + // account for removal of item + size--; + + return requestedItem; } /** @@ -165,7 +196,26 @@ public E removeBack() { */ @Override public void remove(E item) { + // if buffer contains no items, one cannot be removed + if(size == 0) { + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + } + + // if item is not in buffer + if(!contains(item)) { + throw new NoSuchElementException("Given item is not located in ArrayList"); + } + // run through buffer + for (int i = 0; i < size; i++) { + // check if item at current index of buffer is given item + if(item.equals(buffer[i])) { + // call remove method at that index + remove(i); + } + } + + // do not account for removal of item, as remove(i) does so } /** @@ -176,7 +226,30 @@ public void remove(E item) { */ @Override public E remove(int index) { - return null; + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if buffer contains no items, one cannot be removed + if(size == 0) { + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + } + + // get requested item prior to removal + E requestedItem = buffer[index]; + + // run through buffer, starting at given index + // and accounting for removal of requested value + for(int i = index; i <= size - 1; i++) { + // replace item at current index with item at next index + buffer[i] = buffer[i + 1]; + } + + // account for removal of item + size--; + + return requestedItem; } /** From 11221a8e969bbdbd2334a0c8fc19d601cbe3d0bb Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 00:55:12 -0800 Subject: [PATCH 07/52] Added doubleMaxCapacity method to ArrayList --- src/ArrayList.java | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index bbdc936..c5b5ebc 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -5,7 +5,7 @@ public class ArrayList implements List { /** * An array used to store values placed within the ArrayList */ - private final E[] buffer; + private E[] buffer; /** * The number of values stored within buffer @@ -23,6 +23,25 @@ public ArrayList() { size = 0; } + /** + * If all slots in buffer are full, double its max capacity + */ + private void doubleMaxCapacity() { + // if all buffer slots are filled + if(size == buffer.length) { + // create a new buffer, with double the capacity of the existing buffer + E[] newBuffer = (E[]) new Object[size * 2]; + + // run through previous buffer and copy over all values + for(int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + + // replace buffer with newBuffer, now with double the length + buffer = newBuffer; + } + } + /** * Add item to the front. * @@ -33,6 +52,7 @@ public void addFront(E item) { // if the buffer already contains items if(size != 0) { // if the buffer is full, increase max capacity + doubleMaxCapacity(); // run through buffer backwards for (int i = size; i > 0; i--) { @@ -57,6 +77,7 @@ public void addFront(E item) { @Override public void addBack(E item) { // if the buffer is full, increase max capacity + doubleMaxCapacity(); // add the given item at final index buffer[size] = item; @@ -79,6 +100,7 @@ public void add(int index, E item) { } // if the buffer is full, increase max capacity + doubleMaxCapacity(); // run through buffer backwards, up to given index for (int i = size; i > index; i--) { From 14ca13c5115230935906dfbb1804d61d068049a5 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 04:52:07 -0800 Subject: [PATCH 08/52] Added tests for ArrayList's addFront, addBack, and add methods --- src/ArrayList.java | 2 +- tests/ArrayListTest.java | 205 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 202 insertions(+), 5 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index c5b5ebc..0a17551 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -62,7 +62,7 @@ public void addFront(E item) { } } - // add the given item at index 0 + // add the given item at the now cleared index 0 buffer[0] = item; // a new item has been added to buffer diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 122c74c..6962231 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -1,21 +1,218 @@ import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + class ArrayListTest { /** * Create ArrayList at start for testing */ - private final ArrayList testArrayList = new ArrayList<>(); + private final ArrayList testArrayIntList = new ArrayList<>(); + + /** + * The default max capacity of buffer, at creation + */ + private final int DEFAULT_BUFFER_LENGTH = 10; + + /** + * The first index in buffer is 0 + */ + private final int FIRST_INDEX = 0; + + /** + * The value returned by methods if value not found + */ + private final int INVALID_INDEX = -1; + + /** + * The standard filler value added to buffer prior to testing method + */ + private final int FILLER_VALUE = 5; + + /** + * The standard value used to test methods + */ + private final int TEST_VALUE = 20; @Test - void addFront() { + void addFront_bufferContainsOneValue_addedSuccessfully() { + // add initial value + testArrayIntList.addBack(FILLER_VALUE); + + // add expected value to front + testArrayIntList.addFront(TEST_VALUE); + + // ensure expected value is at index 0 + assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); } @Test - void addBack() { + void addFront_bufferContainsMultipleValues_addedSuccessfully() { + // add several initial values + testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_VALUE); + + // add expected value to front + testArrayIntList.addFront(TEST_VALUE); + + // ensure expected value is at index 0 + assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); } @Test - void add() { + void addFront_bufferEmpty_addedSuccessfully() { + testArrayIntList.addFront(TEST_VALUE); + + // ensure expected value is at index 0 + assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + } + + @Test + void addFront_bufferFull_addedSuccessfully() { + // add 10 values to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { + testArrayIntList.addBack(FILLER_VALUE); + } + + // add 11th value to front + testArrayIntList.addFront(TEST_VALUE); + + // ensure expected value is at index 0 + assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + + } + + @Test + void addBack_bufferContainsOneValue_addedSuccessfully() { + // add initial value + testArrayIntList.addFront(FILLER_VALUE); + + // add expected value to back + testArrayIntList.addBack(TEST_VALUE); + + // ensure expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void addBack_bufferContainsMultipleValues_addedSuccessfully() { + // add several initial values + testArrayIntList.addFront(FILLER_VALUE); + testArrayIntList.addFront(FILLER_VALUE); + testArrayIntList.addFront(FILLER_VALUE); + + // add expected value to back + testArrayIntList.addBack(TEST_VALUE); + + // ensure expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void addBack_bufferEmpty_addedSuccessfully() { + testArrayIntList.addBack(TEST_VALUE); + + // ensure the expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void addBack_bufferFull_addedSuccessfully() { + // add 10 values to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { + testArrayIntList.addFront(FILLER_VALUE); + } + + // add 11th value to back + testArrayIntList.addBack(TEST_VALUE); + + // ensure expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void add_bufferContainsOneValue_addedSuccessfully() { + // add initial value + testArrayIntList.addBack(FILLER_VALUE); + + // add expected value to buffer + testArrayIntList.add(1, TEST_VALUE); + + // ensure expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void add_bufferContainsMultipleValues_addedSuccessfully() { + // add several initial values + testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_VALUE); + + // add expected value somewhere in buffer + testArrayIntList.add(2, TEST_VALUE); + + // ensure expected value is at middle index + assertEquals(TEST_VALUE, testArrayIntList.get(2)); + } + + @Test + void add_bufferEmpty_addedSuccessfully() { + testArrayIntList.add(0, TEST_VALUE); + + // ensure the expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void add_bufferFull_addedSuccessfully() { + // add 10 values to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { + testArrayIntList.addBack(FILLER_VALUE); + } + + // add 11th value to buffer + testArrayIntList.add(2, TEST_VALUE); + + // ensure expected value is at final index + assertEquals(TEST_VALUE, testArrayIntList.get(2)); + } + + @Test + void add_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to add value at invalid index + testArrayIntList.add(-1, TEST_VALUE); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void add_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to add value at invalid index + testArrayIntList.add(1, TEST_VALUE); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From a7c11deb47df44f996b9ffe641ce529fca0664c4 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 05:15:50 -0800 Subject: [PATCH 09/52] Refactored set method of ArrayList - Implemented tests for set method of ArrayList --- src/ArrayList.java | 14 +- tests/ArrayListTest.java | 314 ++++++++++++++++++++++++++++----------- 2 files changed, 235 insertions(+), 93 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 0a17551..abe36ff 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -130,8 +130,8 @@ public E get(int index) { } // if buffer contains no items, one cannot be retrieved - if(size == 0) { - throw new NoSuchElementException("Cannot retrieve values from empty ArrayList"); + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve value from empty ArrayList"); } return buffer[index]; @@ -151,13 +151,13 @@ public void set(int index, E item) { throw new IndexOutOfBoundsException(index + " is not a valid index"); } - // if buffer contains no items, one cannot be retrieved - if(size == 0 && index != 0) { - throw new NoSuchElementException("Cannot place item at given index of empty ArrayList"); - } - // place the given item at the requested index buffer[index] = item; + + // if the buffer is regarded as empty, account for new item placed at index 0 + if(isEmpty()) { + size++; + } } /** diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 6962231..d17042c 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -1,5 +1,7 @@ import org.junit.jupiter.api.Test; +import java.util.NoSuchElementException; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -20,163 +22,163 @@ class ArrayListTest { private final int FIRST_INDEX = 0; /** - * The value returned by methods if value not found + * The item returned by methods if item not found */ private final int INVALID_INDEX = -1; /** - * The standard filler value added to buffer prior to testing method + * The standard filler item added to buffer prior to testing method */ - private final int FILLER_VALUE = 5; + private final int FILLER_ITEM = 5; /** - * The standard value used to test methods + * The standard item used to test methods */ - private final int TEST_VALUE = 20; + private final int TEST_ITEM = 20; @Test - void addFront_bufferContainsOneValue_addedSuccessfully() { - // add initial value - testArrayIntList.addBack(FILLER_VALUE); + void addFront_bufferContainsOneItem_addedSuccessfully() { + // add initial item + testArrayIntList.addBack(FILLER_ITEM); - // add expected value to front - testArrayIntList.addFront(TEST_VALUE); + // add expected item to front + testArrayIntList.addFront(TEST_ITEM); - // ensure expected value is at index 0 - assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + // ensure expected item is at index 0 + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); } @Test - void addFront_bufferContainsMultipleValues_addedSuccessfully() { - // add several initial values - testArrayIntList.addBack(FILLER_VALUE); - testArrayIntList.addBack(FILLER_VALUE); - testArrayIntList.addBack(FILLER_VALUE); + void addFront_bufferContainsMultipleItems_addedSuccessfully() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); - // add expected value to front - testArrayIntList.addFront(TEST_VALUE); + // add expected item to front + testArrayIntList.addFront(TEST_ITEM); - // ensure expected value is at index 0 - assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + // ensure expected item is at index 0 + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); } @Test void addFront_bufferEmpty_addedSuccessfully() { - testArrayIntList.addFront(TEST_VALUE); + testArrayIntList.addFront(TEST_ITEM); - // ensure expected value is at index 0 - assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + // ensure expected item is at index 0 + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); } @Test void addFront_bufferFull_addedSuccessfully() { - // add 10 values to buffer + // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_ITEM); } - // add 11th value to front - testArrayIntList.addFront(TEST_VALUE); + // add 11th item to front + testArrayIntList.addFront(TEST_ITEM); - // ensure expected value is at index 0 - assertEquals(TEST_VALUE, testArrayIntList.get(FIRST_INDEX)); + // ensure expected item is at index 0 + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); } @Test - void addBack_bufferContainsOneValue_addedSuccessfully() { - // add initial value - testArrayIntList.addFront(FILLER_VALUE); + void addBack_bufferContainsOneItem_addedSuccessfully() { + // add initial item + testArrayIntList.addFront(FILLER_ITEM); - // add expected value to back - testArrayIntList.addBack(TEST_VALUE); + // add expected item to back + testArrayIntList.addBack(TEST_ITEM); - // ensure expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test - void addBack_bufferContainsMultipleValues_addedSuccessfully() { - // add several initial values - testArrayIntList.addFront(FILLER_VALUE); - testArrayIntList.addFront(FILLER_VALUE); - testArrayIntList.addFront(FILLER_VALUE); + void addBack_bufferContainsMultipleItems_addedSuccessfully() { + // add several initial items + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); - // add expected value to back - testArrayIntList.addBack(TEST_VALUE); + // add expected item to back + testArrayIntList.addBack(TEST_ITEM); - // ensure expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test void addBack_bufferEmpty_addedSuccessfully() { - testArrayIntList.addBack(TEST_VALUE); + testArrayIntList.addBack(TEST_ITEM); - // ensure the expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure the expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test void addBack_bufferFull_addedSuccessfully() { - // add 10 values to buffer + // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addFront(FILLER_VALUE); + testArrayIntList.addFront(FILLER_ITEM); } - // add 11th value to back - testArrayIntList.addBack(TEST_VALUE); + // add 11th item to back + testArrayIntList.addBack(TEST_ITEM); - // ensure expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test - void add_bufferContainsOneValue_addedSuccessfully() { - // add initial value - testArrayIntList.addBack(FILLER_VALUE); + void add_bufferContainsOneItem_addedSuccessfully() { + // add initial item + testArrayIntList.addBack(FILLER_ITEM); - // add expected value to buffer - testArrayIntList.add(1, TEST_VALUE); + // add expected item to buffer + testArrayIntList.add(1, TEST_ITEM); - // ensure expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test - void add_bufferContainsMultipleValues_addedSuccessfully() { - // add several initial values - testArrayIntList.addBack(FILLER_VALUE); - testArrayIntList.addBack(FILLER_VALUE); - testArrayIntList.addBack(FILLER_VALUE); + void add_bufferContainsMultipleItems_addedSuccessfully() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); - // add expected value somewhere in buffer - testArrayIntList.add(2, TEST_VALUE); + // add expected item somewhere in buffer + testArrayIntList.add(2, TEST_ITEM); - // ensure expected value is at middle index - assertEquals(TEST_VALUE, testArrayIntList.get(2)); + // ensure expected item is at middle index + assertEquals(TEST_ITEM, testArrayIntList.get(2)); } @Test void add_bufferEmpty_addedSuccessfully() { - testArrayIntList.add(0, TEST_VALUE); + testArrayIntList.add(0, TEST_ITEM); - // ensure the expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(testArrayIntList.size() - 1)); + // ensure the expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); } @Test void add_bufferFull_addedSuccessfully() { - // add 10 values to buffer + // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_VALUE); + testArrayIntList.addBack(FILLER_ITEM); } - // add 11th value to buffer - testArrayIntList.add(2, TEST_VALUE); + // add 11th item to buffer + testArrayIntList.add(2, TEST_ITEM); - // ensure expected value is at final index - assertEquals(TEST_VALUE, testArrayIntList.get(2)); + // ensure expected item is at final index + assertEquals(TEST_ITEM, testArrayIntList.get(2)); } @Test @@ -185,8 +187,8 @@ void add_invalidIndexNegative_throwsException() { boolean exceptionThrown = false; try { - // attempt to add value at invalid index - testArrayIntList.add(-1, TEST_VALUE); + // attempt to add item at invalid index + testArrayIntList.add(-1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -203,8 +205,8 @@ void add_invalidIndexMoreThanSize_throwsException() { boolean exceptionThrown = false; try { - // attempt to add value at invalid index - testArrayIntList.add(1, TEST_VALUE); + // attempt to add item at invalid index + testArrayIntList.add(1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -216,11 +218,151 @@ void add_invalidIndexMoreThanSize_throwsException() { } @Test - void get() { + void get_bufferContainsOneItem_returnsitem() { + // add expected item to buffer + testArrayIntList.addFront(TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + } + + @Test + void get_bufferContainsMultipleItems_returnsitem() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // add item to retrieve + testArrayIntList.addBack(TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + } + + @Test + void get_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get item from empty buffer + testArrayIntList.get(FIRST_INDEX); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test - void set() { + void get_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get item from invalid index + testArrayIntList.get(-1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void get_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get item from invalid index + testArrayIntList.get(1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void set_bufferContainsOneItem_setItemAtIndex() { + // add initial item to buffer + testArrayIntList.addFront(FILLER_ITEM); + + // replace item at that index + testArrayIntList.set(FIRST_INDEX, TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + } + + @Test + void set_bufferContainsMultipleItems_setItemAtIndex() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // replace item at second index + testArrayIntList.set(2, TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testArrayIntList.get(2)); + } + + @Test + void set_bufferEmpty_setItemAtIndex() { + // add item to index 0 of empty buffer + testArrayIntList.set(FIRST_INDEX, TEST_ITEM); + + // check if exception was thrown + assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + } + + @Test + void set_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to set item at invalid index + testArrayIntList.set(-1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void set_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to set item at invalid index + testArrayIntList.set(1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From 11635252e99969a691338b6d805ee663d1c8d945 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 05:28:13 -0800 Subject: [PATCH 10/52] Refactored ArrayList checks to utilize isEmpty - Implemented tests for ArrayList's isEmpty and size methods --- src/ArrayList.java | 10 +++--- tests/ArrayListTest.java | 72 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index abe36ff..3d160f3 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -50,7 +50,7 @@ private void doubleMaxCapacity() { @Override public void addFront(E item) { // if the buffer already contains items - if(size != 0) { + if(!isEmpty()) { // if the buffer is full, increase max capacity doubleMaxCapacity(); @@ -168,7 +168,7 @@ public void set(int index, E item) { @Override public E removeFront() { // if buffer contains no items, one cannot be removed - if(size == 0) { + if(isEmpty()) { throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } @@ -195,7 +195,7 @@ public E removeFront() { @Override public E removeBack() { // if buffer contains no items, one cannot be removed - if(size == 0) { + if(isEmpty()) { throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } @@ -219,7 +219,7 @@ public E removeBack() { @Override public void remove(E item) { // if buffer contains no items, one cannot be removed - if(size == 0) { + if(isEmpty()) { throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } @@ -254,7 +254,7 @@ public E remove(int index) { } // if buffer contains no items, one cannot be removed - if(size == 0) { + if(isEmpty()) { throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index d17042c..2284a6c 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -2,8 +2,8 @@ import java.util.NoSuchElementException; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertFalse; class ArrayListTest { /** @@ -374,22 +374,82 @@ void removeBack() { } @Test - void remove() { + void removeItem() { } @Test - void testRemove() { + void removeIndex() { } @Test void contains() { } + @Test + void isEmpty_bufferContainsOneItem_returnsFalse() { + // add item so the buffer is not empty + testArrayIntList.addFront(FILLER_ITEM); + + assertFalse(testArrayIntList.isEmpty()); + } + + @Test + void isEmpty_bufferContainsMultipleValues_returnsFalse() { + // add items so the buffer is not empty + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); + + // check if buffer is empty + assertFalse(testArrayIntList.isEmpty()); + } + + @Test + void isEmpty_bufferFull_returnsFalse() { + // add 10 items to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { + testArrayIntList.addBack(FILLER_ITEM); + } + + // check if buffer is empty + assertFalse(testArrayIntList.isEmpty()); + } @Test - void isEmpty() { + void isEmpty_bufferEmpty_returnsTrue() { + // check if buffer is empty + assertTrue(testArrayIntList.isEmpty()); } @Test - void size() { + void size_bufferContainsOneItem_returnsSize() { + // add item so the buffer is not empty + testArrayIntList.addFront(FILLER_ITEM); + + assertEquals(1, testArrayIntList.size()); + } + + @Test + void size_bufferContainsMultipleItems_returnsSize() { + // add items so the buffer is not empty + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); + testArrayIntList.addFront(FILLER_ITEM); + + assertEquals(3, testArrayIntList.size()); + } + + @Test + void size_bufferEmpty_returnsSize() { + assertEquals(0, testArrayIntList.size()); + } + + @Test + void size_bufferFull_returnsSize() { + // add 10 items to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { + testArrayIntList.addBack(FILLER_ITEM); + } + + assertEquals(DEFAULT_BUFFER_LENGTH, testArrayIntList.size()); } } \ No newline at end of file From ad16279d279cf053adbcce2d1c1f4095d0deef17 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 05:36:50 -0800 Subject: [PATCH 11/52] Implemented tests for ArrayList's contains method --- tests/ArrayListTest.java | 45 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 2284a6c..9b8fa8c 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -382,8 +382,49 @@ void removeIndex() { } @Test - void contains() { + void contains_itemExists_returnsTrue() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // add item to check for + testArrayIntList.addBack(TEST_ITEM); + + // check if item is in buffer + assertTrue(testArrayIntList.contains(TEST_ITEM)); } + + @Test + void contains_itemNotExists_returnsFalse() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // check if item is in buffer + assertFalse(testArrayIntList.contains(TEST_ITEM)); + } + + @Test + void contains_bufferEmpty_returnsFalse() { + assertFalse(testArrayIntList.contains(TEST_ITEM)); + } + + @Test + void contains_bufferFullItemExists_returnsTrue() { + // add 9 items to buffer + for(int i = 0; i < DEFAULT_BUFFER_LENGTH - 1; i++) { + testArrayIntList.addBack(FILLER_ITEM); + } + + // add test item as last value + testArrayIntList.addBack(TEST_ITEM); + + // check if item is in buffer + assertTrue(testArrayIntList.contains(TEST_ITEM)); + } + @Test void isEmpty_bufferContainsOneItem_returnsFalse() { // add item so the buffer is not empty @@ -393,7 +434,7 @@ void isEmpty_bufferContainsOneItem_returnsFalse() { } @Test - void isEmpty_bufferContainsMultipleValues_returnsFalse() { + void isEmpty_bufferContainsMultipleItems_returnsFalse() { // add items so the buffer is not empty testArrayIntList.addFront(FILLER_ITEM); testArrayIntList.addFront(FILLER_ITEM); From e725a9828b88396598593759bf09598000400901 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 05:54:00 -0800 Subject: [PATCH 12/52] Implemented tests for ArrayList's removeFront and removeBack methods --- tests/ArrayListTest.java | 92 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 2 deletions(-) diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 9b8fa8c..7a8d5a7 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -366,11 +366,99 @@ void set_invalidIndexMoreThanSize_throwsException() { } @Test - void removeFront() { + void removeFront_bufferContainsOneItem_removedSuccessfully() { + // add item to remove + testArrayIntList.addFront(TEST_ITEM); + + // attempt to remove first item from buffer + assertEquals(TEST_ITEM, testArrayIntList.removeFront()); + + // ensure buffer is now empty + assertTrue(testArrayIntList.isEmpty()); + } + + @Test + void removeFront_bufferContainsMultipleItems_removedSuccessfully() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // add item to remove + testArrayIntList.addFront(TEST_ITEM); + + // attempt to remove first item from buffer + assertEquals(TEST_ITEM, testArrayIntList.removeFront()); + + // ensure value was removed from buffer + assertNotEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(3, testArrayIntList.size()); + } + + @Test + void removeFront_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at index 0 + testArrayIntList.removeFront(); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test - void removeBack() { + void removeBack_bufferContainsOneItem_removedSuccessfully() { + // add value to remove + testArrayIntList.addFront(TEST_ITEM); + + // attempt to remove last value from buffer + assertEquals(TEST_ITEM, testArrayIntList.removeBack()); + + // ensure buffer is now empty + assertTrue(testArrayIntList.isEmpty()); + } + + @Test + void removeBack_bufferContainsMultipleItems_removedSuccessfully() { + // add several initial values + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // add value to remove + testArrayIntList.addBack(TEST_ITEM); + + // attempt to remove back value from buffer + assertEquals(TEST_ITEM, testArrayIntList.removeBack()); + + // ensure value was removed from buffer + assertNotEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size())); + assertEquals(3, testArrayIntList.size()); + } + + @Test + void removeBack_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove value at index 0 + testArrayIntList.removeBack(); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From 56048d4c4bc41ca7d8fb540e2fa9cfbaa55582d0 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 06:02:53 -0800 Subject: [PATCH 13/52] Implemented tests for ArrayList's remove(index) method --- tests/ArrayListTest.java | 81 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 1 deletion(-) diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 7a8d5a7..ac3d5f3 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -466,7 +466,86 @@ void removeItem() { } @Test - void removeIndex() { + void removeIndex_bufferContainsOneItem_removedSuccessfully() { + // add value to remove + testArrayIntList.addBack(TEST_ITEM); + + // attempt to remove item from buffer + assertEquals(TEST_ITEM, testArrayIntList.remove(FIRST_INDEX)); + + // ensure buffer is now empty + assertTrue(testArrayIntList.isEmpty()); + } + + @Test + void removeIndex_bufferContainsMultipleItems_removedSuccessfully() { + // add several initial items + testArrayIntList.addBack(TEST_ITEM); + testArrayIntList.addBack(TEST_ITEM); + testArrayIntList.addBack(TEST_ITEM); + + // add item to remove + testArrayIntList.add(2, TEST_ITEM); + + // attempt to remove item from buffer + assertEquals(TEST_ITEM, testArrayIntList.remove(2)); + + // ensure value was removed from buffer + assertEquals(3, testArrayIntList.size()); + } + + @Test + void removeIndex_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at index 0 + testArrayIntList.remove(FIRST_INDEX); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void removeIndex_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at invalid index + testArrayIntList.remove(-1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void removeIndex_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at invalid index + testArrayIntList.remove(1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From c6df2416283559088ea5a25452c442f81cc825a3 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 07:09:40 -0800 Subject: [PATCH 14/52] Refactored remove(item) method of ArrayList - Added indexOf helper method - Implemented tests for remove(item) --- src/ArrayList.java | 33 ++++++++++++++---- tests/ArrayListTest.java | 75 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 98 insertions(+), 10 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 3d160f3..1c72c85 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -223,21 +223,42 @@ public void remove(E item) { throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } + // find item in buffer and get its index + int index = indexOf(item); + // if item is not in buffer - if(!contains(item)) { + if(index == -1) { throw new NoSuchElementException("Given item is not located in ArrayList"); } + // otherwise, run through buffer, starting at given index + // and accounting for removal of requested value + for(int i = index; i <= size - 1; i++) { + // replace item at current index with item at next index + buffer[i] = buffer[i + 1]; + } + + // account for removal of item + size--; + } + + /** + * Gets and returns the index of the given item in buffer, if exists + * @param item the item being searched for in buffer + * @return the index of given item in buffer if exists; otherwise -1 + */ + private int indexOf(E item) { // run through buffer - for (int i = 0; i < size; i++) { - // check if item at current index of buffer is given item + for(int i = 0; i < buffer.length; i++) { + // check if value stored at current index is the specified value if(item.equals(buffer[i])) { - // call remove method at that index - remove(i); + // return the current index, as that is where the specified value is located + return i; } } - // do not account for removal of item, as remove(i) does so + // if the value does not exist in buffer + return -1; } /** diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index ac3d5f3..392f809 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -462,7 +462,74 @@ void removeBack_bufferEmpty_throwsException() { } @Test - void removeItem() { + void removeItem_bufferContainsOneItem_removedSuccessfully() { + // add value to remove + testArrayIntList.addBack(TEST_ITEM); + + // attempt to remove item from buffer + testArrayIntList.remove((Integer) TEST_ITEM); + + // ensure buffer is now empty + assertTrue(testArrayIntList.isEmpty()); + } + + @Test + void removeItem_bufferContainsMultipleItems_removedSuccessfully() { + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + // add item to remove + testArrayIntList.add(2, TEST_ITEM); + + // attempt to remove item from buffer + testArrayIntList.remove((Integer) TEST_ITEM); + + // ensure value was removed from buffer + assertNotEquals(TEST_ITEM, testArrayIntList.get(2)); + assertEquals(3, testArrayIntList.size()); + } + + @Test + void removeItem_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item from empty buffer + testArrayIntList.remove((Integer) TEST_ITEM); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void removeItem_itemNotInBuffer_throwsException() { + // setup flag + boolean exceptionThrown = false; + + // add several initial items + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + + try { + // attempt to remove nonexistent item from buffer + testArrayIntList.remove((Integer) TEST_ITEM); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test @@ -480,9 +547,9 @@ void removeIndex_bufferContainsOneItem_removedSuccessfully() { @Test void removeIndex_bufferContainsMultipleItems_removedSuccessfully() { // add several initial items - testArrayIntList.addBack(TEST_ITEM); - testArrayIntList.addBack(TEST_ITEM); - testArrayIntList.addBack(TEST_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); + testArrayIntList.addBack(FILLER_ITEM); // add item to remove testArrayIntList.add(2, TEST_ITEM); From 6c09b606020e132d22eb17290c08084fc267fe20 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 07:16:01 -0800 Subject: [PATCH 15/52] Implemented iterator for ArrayList - Updated various references to match current project --- src/ArrayList.java | 74 +++++++-- tests/ArrayListTest.java | 314 +++++++++++++++++++-------------------- 2 files changed, 219 insertions(+), 169 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 1c72c85..a087ae7 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -3,23 +3,23 @@ public class ArrayList implements List { /** - * An array used to store values placed within the ArrayList + * An array used to store items placed within the ArrayList */ private E[] buffer; /** - * The number of values stored within buffer + * The number of items stored within buffer */ private int size; /** - * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 values + * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 items */ public ArrayList() { // setup buffer with default max capacity of 10 buffer = (E[]) new Object[10]; - // no values are stored in buffer + // no items are stored in buffer size = 0; } @@ -32,7 +32,7 @@ private void doubleMaxCapacity() { // create a new buffer, with double the capacity of the existing buffer E[] newBuffer = (E[]) new Object[size * 2]; - // run through previous buffer and copy over all values + // run through previous buffer and copy over all items for(int i = 0; i < buffer.length; i++) { newBuffer[i] = buffer[i]; } @@ -131,7 +131,7 @@ public E get(int index) { // if buffer contains no items, one cannot be retrieved if(isEmpty()) { - throw new NoSuchElementException("Cannot retrieve value from empty ArrayList"); + throw new NoSuchElementException("Cannot retrieve item from empty ArrayList"); } return buffer[index]; @@ -232,7 +232,7 @@ public void remove(E item) { } // otherwise, run through buffer, starting at given index - // and accounting for removal of requested value + // and accounting for removal of requested item for(int i = index; i <= size - 1; i++) { // replace item at current index with item at next index buffer[i] = buffer[i + 1]; @@ -250,14 +250,14 @@ public void remove(E item) { private int indexOf(E item) { // run through buffer for(int i = 0; i < buffer.length; i++) { - // check if value stored at current index is the specified value + // check if item stored at current index is the specified item if(item.equals(buffer[i])) { - // return the current index, as that is where the specified value is located + // return the current index, as that is where the specified item is located return i; } } - // if the value does not exist in buffer + // if the item does not exist in buffer return -1; } @@ -283,7 +283,7 @@ public E remove(int index) { E requestedItem = buffer[index]; // run through buffer, starting at given index - // and accounting for removal of requested value + // and accounting for removal of requested item for(int i = index; i <= size - 1; i++) { // replace item at current index with item at next index buffer[i] = buffer[i + 1]; @@ -343,6 +343,56 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new ArrayListIterator(); + } + + /** + * Implementation of an Iterator for the ArrayIntList class + */ + private class ArrayListIterator implements Iterator { + /** + * The current index being tracked by the Iterator + */ + private int index; + + /** + * Constructs an ArrayIntListIterator with the index initialized to 0 + */ + ArrayListIterator() { + index = 0; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return index < size; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get the item at current index of buffer + E currItem = buffer[index]; + + // move on to next element + index++; + + return currItem; + } } } \ No newline at end of file diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 392f809..6720cef 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -9,7 +9,7 @@ class ArrayListTest { /** * Create ArrayList at start for testing */ - private final ArrayList testArrayIntList = new ArrayList<>(); + private final ArrayList testArrayList = new ArrayList<>(); /** * The default max capacity of buffer, at creation @@ -39,146 +39,146 @@ class ArrayListTest { @Test void addFront_bufferContainsOneItem_addedSuccessfully() { // add initial item - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add expected item to front - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // ensure expected item is at index 0 - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void addFront_bufferContainsMultipleItems_addedSuccessfully() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add expected item to front - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // ensure expected item is at index 0 - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void addFront_bufferEmpty_addedSuccessfully() { - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // ensure expected item is at index 0 - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void addFront_bufferFull_addedSuccessfully() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); } // add 11th item to front - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // ensure expected item is at index 0 - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void addBack_bufferContainsOneItem_addedSuccessfully() { // add initial item - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); // add expected item to back - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // ensure expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void addBack_bufferContainsMultipleItems_addedSuccessfully() { // add several initial items - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); // add expected item to back - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // ensure expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void addBack_bufferEmpty_addedSuccessfully() { - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // ensure the expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void addBack_bufferFull_addedSuccessfully() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); } // add 11th item to back - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // ensure expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void add_bufferContainsOneItem_addedSuccessfully() { // add initial item - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add expected item to buffer - testArrayIntList.add(1, TEST_ITEM); + testArrayList.add(1, TEST_ITEM); // ensure expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void add_bufferContainsMultipleItems_addedSuccessfully() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add expected item somewhere in buffer - testArrayIntList.add(2, TEST_ITEM); + testArrayList.add(2, TEST_ITEM); // ensure expected item is at middle index - assertEquals(TEST_ITEM, testArrayIntList.get(2)); + assertEquals(TEST_ITEM, testArrayList.get(2)); } @Test void add_bufferEmpty_addedSuccessfully() { - testArrayIntList.add(0, TEST_ITEM); + testArrayList.add(0, TEST_ITEM); // ensure the expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test void add_bufferFull_addedSuccessfully() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); } // add 11th item to buffer - testArrayIntList.add(2, TEST_ITEM); + testArrayList.add(2, TEST_ITEM); // ensure expected item is at final index - assertEquals(TEST_ITEM, testArrayIntList.get(2)); + assertEquals(TEST_ITEM, testArrayList.get(2)); } @Test @@ -188,7 +188,7 @@ void add_invalidIndexNegative_throwsException() { try { // attempt to add item at invalid index - testArrayIntList.add(-1, TEST_ITEM); + testArrayList.add(-1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -206,7 +206,7 @@ void add_invalidIndexMoreThanSize_throwsException() { try { // attempt to add item at invalid index - testArrayIntList.add(1, TEST_ITEM); + testArrayList.add(1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -220,24 +220,24 @@ void add_invalidIndexMoreThanSize_throwsException() { @Test void get_bufferContainsOneItem_returnsitem() { // add expected item to buffer - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // attempt to retrieve expected item - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void get_bufferContainsMultipleItems_returnsitem() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add item to retrieve - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // attempt to retrieve expected item - assertEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size() - 1)); + assertEquals(TEST_ITEM, testArrayList.get(testArrayList.size() - 1)); } @Test @@ -247,7 +247,7 @@ void get_bufferEmpty_throwsException() { try { // attempt to get item from empty buffer - testArrayIntList.get(FIRST_INDEX); + testArrayList.get(FIRST_INDEX); } catch (NoSuchElementException e) { @@ -265,7 +265,7 @@ void get_invalidIndexNegative_throwsException() { try { // attempt to get item from invalid index - testArrayIntList.get(-1); + testArrayList.get(-1); } catch (IndexOutOfBoundsException e) { @@ -283,7 +283,7 @@ void get_invalidIndexMoreThanSize_throwsException() { try { // attempt to get item from invalid index - testArrayIntList.get(1); + testArrayList.get(1); } catch (IndexOutOfBoundsException e) { @@ -297,36 +297,36 @@ void get_invalidIndexMoreThanSize_throwsException() { @Test void set_bufferContainsOneItem_setItemAtIndex() { // add initial item to buffer - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); // replace item at that index - testArrayIntList.set(FIRST_INDEX, TEST_ITEM); + testArrayList.set(FIRST_INDEX, TEST_ITEM); // attempt to retrieve expected item - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test void set_bufferContainsMultipleItems_setItemAtIndex() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // replace item at second index - testArrayIntList.set(2, TEST_ITEM); + testArrayList.set(2, TEST_ITEM); // attempt to retrieve expected item - assertEquals(TEST_ITEM, testArrayIntList.get(2)); + assertEquals(TEST_ITEM, testArrayList.get(2)); } @Test void set_bufferEmpty_setItemAtIndex() { // add item to index 0 of empty buffer - testArrayIntList.set(FIRST_INDEX, TEST_ITEM); + testArrayList.set(FIRST_INDEX, TEST_ITEM); // check if exception was thrown - assertEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); } @Test @@ -336,7 +336,7 @@ void set_invalidIndexNegative_throwsException() { try { // attempt to set item at invalid index - testArrayIntList.set(-1, TEST_ITEM); + testArrayList.set(-1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -354,7 +354,7 @@ void set_invalidIndexMoreThanSize_throwsException() { try { // attempt to set item at invalid index - testArrayIntList.set(1, TEST_ITEM); + testArrayList.set(1, TEST_ITEM); } catch (IndexOutOfBoundsException e) { @@ -368,31 +368,31 @@ void set_invalidIndexMoreThanSize_throwsException() { @Test void removeFront_bufferContainsOneItem_removedSuccessfully() { // add item to remove - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // attempt to remove first item from buffer - assertEquals(TEST_ITEM, testArrayIntList.removeFront()); + assertEquals(TEST_ITEM, testArrayList.removeFront()); // ensure buffer is now empty - assertTrue(testArrayIntList.isEmpty()); + assertTrue(testArrayList.isEmpty()); } @Test void removeFront_bufferContainsMultipleItems_removedSuccessfully() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add item to remove - testArrayIntList.addFront(TEST_ITEM); + testArrayList.addFront(TEST_ITEM); // attempt to remove first item from buffer - assertEquals(TEST_ITEM, testArrayIntList.removeFront()); + assertEquals(TEST_ITEM, testArrayList.removeFront()); - // ensure value was removed from buffer - assertNotEquals(TEST_ITEM, testArrayIntList.get(FIRST_INDEX)); - assertEquals(3, testArrayIntList.size()); + // ensure item was removed from buffer + assertNotEquals(TEST_ITEM, testArrayList.get(FIRST_INDEX)); + assertEquals(3, testArrayList.size()); } @Test @@ -402,7 +402,7 @@ void removeFront_bufferEmpty_throwsException() { try { // attempt to remove item at index 0 - testArrayIntList.removeFront(); + testArrayList.removeFront(); } catch (NoSuchElementException e) { @@ -415,32 +415,32 @@ void removeFront_bufferEmpty_throwsException() { @Test void removeBack_bufferContainsOneItem_removedSuccessfully() { - // add value to remove - testArrayIntList.addFront(TEST_ITEM); + // add item to remove + testArrayList.addFront(TEST_ITEM); - // attempt to remove last value from buffer - assertEquals(TEST_ITEM, testArrayIntList.removeBack()); + // attempt to remove last item from buffer + assertEquals(TEST_ITEM, testArrayList.removeBack()); // ensure buffer is now empty - assertTrue(testArrayIntList.isEmpty()); + assertTrue(testArrayList.isEmpty()); } @Test void removeBack_bufferContainsMultipleItems_removedSuccessfully() { - // add several initial values - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + // add several initial items + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); - // add value to remove - testArrayIntList.addBack(TEST_ITEM); + // add item to remove + testArrayList.addBack(TEST_ITEM); - // attempt to remove back value from buffer - assertEquals(TEST_ITEM, testArrayIntList.removeBack()); + // attempt to remove back item from buffer + assertEquals(TEST_ITEM, testArrayList.removeBack()); - // ensure value was removed from buffer - assertNotEquals(TEST_ITEM, testArrayIntList.get(testArrayIntList.size())); - assertEquals(3, testArrayIntList.size()); + // ensure item was removed from buffer + assertNotEquals(TEST_ITEM, testArrayList.get(testArrayList.size())); + assertEquals(3, testArrayList.size()); } @Test @@ -449,8 +449,8 @@ void removeBack_bufferEmpty_throwsException() { boolean exceptionThrown = false; try { - // attempt to remove value at index 0 - testArrayIntList.removeBack(); + // attempt to remove item at index 0 + testArrayList.removeBack(); } catch (NoSuchElementException e) { @@ -463,32 +463,32 @@ void removeBack_bufferEmpty_throwsException() { @Test void removeItem_bufferContainsOneItem_removedSuccessfully() { - // add value to remove - testArrayIntList.addBack(TEST_ITEM); + // add item to remove + testArrayList.addBack(TEST_ITEM); // attempt to remove item from buffer - testArrayIntList.remove((Integer) TEST_ITEM); + testArrayList.remove((Integer) TEST_ITEM); // ensure buffer is now empty - assertTrue(testArrayIntList.isEmpty()); + assertTrue(testArrayList.isEmpty()); } @Test void removeItem_bufferContainsMultipleItems_removedSuccessfully() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add item to remove - testArrayIntList.add(2, TEST_ITEM); + testArrayList.add(2, TEST_ITEM); // attempt to remove item from buffer - testArrayIntList.remove((Integer) TEST_ITEM); + testArrayList.remove((Integer) TEST_ITEM); - // ensure value was removed from buffer - assertNotEquals(TEST_ITEM, testArrayIntList.get(2)); - assertEquals(3, testArrayIntList.size()); + // ensure item was removed from buffer + assertNotEquals(TEST_ITEM, testArrayList.get(2)); + assertEquals(3, testArrayList.size()); } @Test @@ -498,7 +498,7 @@ void removeItem_bufferEmpty_throwsException() { try { // attempt to remove item from empty buffer - testArrayIntList.remove((Integer) TEST_ITEM); + testArrayList.remove((Integer) TEST_ITEM); } catch (NoSuchElementException e) { @@ -515,13 +515,13 @@ void removeItem_itemNotInBuffer_throwsException() { boolean exceptionThrown = false; // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); try { // attempt to remove nonexistent item from buffer - testArrayIntList.remove((Integer) TEST_ITEM); + testArrayList.remove((Integer) TEST_ITEM); } catch (NoSuchElementException e) { @@ -534,31 +534,31 @@ void removeItem_itemNotInBuffer_throwsException() { @Test void removeIndex_bufferContainsOneItem_removedSuccessfully() { - // add value to remove - testArrayIntList.addBack(TEST_ITEM); + // add item to remove + testArrayList.addBack(TEST_ITEM); // attempt to remove item from buffer - assertEquals(TEST_ITEM, testArrayIntList.remove(FIRST_INDEX)); + assertEquals(TEST_ITEM, testArrayList.remove(FIRST_INDEX)); // ensure buffer is now empty - assertTrue(testArrayIntList.isEmpty()); + assertTrue(testArrayList.isEmpty()); } @Test void removeIndex_bufferContainsMultipleItems_removedSuccessfully() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add item to remove - testArrayIntList.add(2, TEST_ITEM); + testArrayList.add(2, TEST_ITEM); // attempt to remove item from buffer - assertEquals(TEST_ITEM, testArrayIntList.remove(2)); + assertEquals(TEST_ITEM, testArrayList.remove(2)); - // ensure value was removed from buffer - assertEquals(3, testArrayIntList.size()); + // ensure item was removed from buffer + assertEquals(3, testArrayList.size()); } @Test @@ -568,7 +568,7 @@ void removeIndex_bufferEmpty_throwsException() { try { // attempt to remove item at index 0 - testArrayIntList.remove(FIRST_INDEX); + testArrayList.remove(FIRST_INDEX); } catch (NoSuchElementException e) { @@ -586,7 +586,7 @@ void removeIndex_invalidIndexNegative_throwsException() { try { // attempt to remove item at invalid index - testArrayIntList.remove(-1); + testArrayList.remove(-1); } catch (IndexOutOfBoundsException e) { @@ -604,7 +604,7 @@ void removeIndex_invalidIndexMoreThanSize_throwsException() { try { // attempt to remove item at invalid index - testArrayIntList.remove(1); + testArrayList.remove(1); } catch (IndexOutOfBoundsException e) { @@ -618,113 +618,113 @@ void removeIndex_invalidIndexMoreThanSize_throwsException() { @Test void contains_itemExists_returnsTrue() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // add item to check for - testArrayIntList.addBack(TEST_ITEM); + testArrayList.addBack(TEST_ITEM); // check if item is in buffer - assertTrue(testArrayIntList.contains(TEST_ITEM)); + assertTrue(testArrayList.contains(TEST_ITEM)); } @Test void contains_itemNotExists_returnsFalse() { // add several initial items - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // check if item is in buffer - assertFalse(testArrayIntList.contains(TEST_ITEM)); + assertFalse(testArrayList.contains(TEST_ITEM)); } @Test void contains_bufferEmpty_returnsFalse() { - assertFalse(testArrayIntList.contains(TEST_ITEM)); + assertFalse(testArrayList.contains(TEST_ITEM)); } @Test void contains_bufferFullItemExists_returnsTrue() { // add 9 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH - 1; i++) { - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); } - // add test item as last value - testArrayIntList.addBack(TEST_ITEM); + // add test item as last item + testArrayList.addBack(TEST_ITEM); // check if item is in buffer - assertTrue(testArrayIntList.contains(TEST_ITEM)); + assertTrue(testArrayList.contains(TEST_ITEM)); } @Test void isEmpty_bufferContainsOneItem_returnsFalse() { // add item so the buffer is not empty - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); - assertFalse(testArrayIntList.isEmpty()); + assertFalse(testArrayList.isEmpty()); } @Test void isEmpty_bufferContainsMultipleItems_returnsFalse() { // add items so the buffer is not empty - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); // check if buffer is empty - assertFalse(testArrayIntList.isEmpty()); + assertFalse(testArrayList.isEmpty()); } @Test void isEmpty_bufferFull_returnsFalse() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); } // check if buffer is empty - assertFalse(testArrayIntList.isEmpty()); + assertFalse(testArrayList.isEmpty()); } @Test void isEmpty_bufferEmpty_returnsTrue() { // check if buffer is empty - assertTrue(testArrayIntList.isEmpty()); + assertTrue(testArrayList.isEmpty()); } @Test void size_bufferContainsOneItem_returnsSize() { // add item so the buffer is not empty - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); - assertEquals(1, testArrayIntList.size()); + assertEquals(1, testArrayList.size()); } @Test void size_bufferContainsMultipleItems_returnsSize() { // add items so the buffer is not empty - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); - testArrayIntList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); + testArrayList.addFront(FILLER_ITEM); - assertEquals(3, testArrayIntList.size()); + assertEquals(3, testArrayList.size()); } @Test void size_bufferEmpty_returnsSize() { - assertEquals(0, testArrayIntList.size()); + assertEquals(0, testArrayList.size()); } @Test void size_bufferFull_returnsSize() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { - testArrayIntList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); } - assertEquals(DEFAULT_BUFFER_LENGTH, testArrayIntList.size()); + assertEquals(DEFAULT_BUFFER_LENGTH, testArrayList.size()); } } \ No newline at end of file From e506d922e3ac55b3f807bb01a9b00bd97a5d7a72 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 07:21:47 -0800 Subject: [PATCH 16/52] Set up LinkedList class and test files --- src/LinkedList.java | 164 ++++++++++++++++++++++++++++++++++++++ tests/LinkedListTest.java | 78 ++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 src/LinkedList.java create mode 100644 tests/LinkedListTest.java diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..b15d0ce --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,164 @@ +import java.util.Iterator; + +public class LinkedList implements List { + /** + * A container which contains an item and a connection to another Node + */ + private class Node { + /** + * The item stored within the Node + */ + E item; + + /** + * The node this Node is pointing to + */ + Node next; + } + + /** + * The first Node of the current LinkedList + */ + private Node head; + + /** + * The number of Nodes contained within this LinkedList + */ + private int size; + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + + } + + /** + * Add an item at specified index (position). + * + * @param i the index where the item should be added + * @param item the item to be added + */ + @Override + public void add(int i, E item) { + + } + + /** + * Get the item at a specified index. + * + * @param i the index where the item should be retrieved + * @return the item located at that index + */ + @Override + public E get(int i) { + return null; + } + + /** + * Set (save) an item at a specified index. Previous + * item at that index is overwritten. + * + * @param i the index where the item should be saved + * @param item the item to be saved + */ + @Override + public void set(int i, E item) { + + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + return null; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + + } + + /** + * Remove item at a specified index. + * + * @param i the index where the item should be removed + * @return the item that was removed + */ + @Override + public E remove(int i) { + return null; + } + + /** + * Checks if an item is in the list. + * + * @param item the item to search for + * @return true if the item is in the list, false otherwise + */ + @Override + public boolean contains(E item) { + return false; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Provides a count of the number of items in the list. + * + * @return number of items in the list + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java new file mode 100644 index 0000000..a8e6a19 --- /dev/null +++ b/tests/LinkedListTest.java @@ -0,0 +1,78 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LinkedListTest { + /** + * Create LinkedList at start for testing + */ + private final LinkedList testLinkedList = new LinkedList<>(); + + /** + * The first index in list is 0 + */ + private final int FIRST_INDEX = 0; + + /** + * The item returned by methods if item not found + */ + private final int INVALID_INDEX = -1; + + /** + * The standard filler item added to container prior to testing method + */ + private final int FILLER_ITEM = 5; + + /** + * The standard item used to test methods + */ + private final int TEST_ITEM = 20; + + @Test + void addFront() { + } + + @Test + void addBack() { + } + + @Test + void add() { + } + + @Test + void get() { + } + + @Test + void set() { + } + + @Test + void removeFront() { + } + + @Test + void removeBack() { + } + + @Test + void removeItem() { + } + + @Test + void removeIndex() { + } + + @Test + void contains() { + } + + @Test + void isEmpty() { + } + + @Test + void size() { + } +} \ No newline at end of file From f91aae752a7f7b225d23bb75722dd26ff7afb84e Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 18:56:00 -0800 Subject: [PATCH 17/52] Implemented LinkedList's size, isEmpty, and contains methods --- src/LinkedList.java | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index b15d0ce..b1e12ba 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -49,22 +49,22 @@ public void addBack(E item) { /** * Add an item at specified index (position). * - * @param i the index where the item should be added + * @param index the index where the item should be added * @param item the item to be added */ @Override - public void add(int i, E item) { + public void add(int index, E item) { } /** * Get the item at a specified index. * - * @param i the index where the item should be retrieved + * @param index the index where the item should be retrieved * @return the item located at that index */ @Override - public E get(int i) { + public E get(int index) { return null; } @@ -72,11 +72,11 @@ public E get(int i) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * - * @param i the index where the item should be saved + * @param index the index where the item should be saved * @param item the item to be saved */ @Override - public void set(int i, E item) { + public void set(int index, E item) { } @@ -113,11 +113,11 @@ public void remove(E item) { /** * Remove item at a specified index. * - * @param i the index where the item should be removed + * @param index the index where the item should be removed * @return the item that was removed */ @Override - public E remove(int i) { + public E remove(int index) { return null; } @@ -129,6 +129,25 @@ public E remove(int i) { */ @Override public boolean contains(E item) { + // if list is not empty + if(head != null) { + // setup pointer + Node current = head; + + // run through list + while (current != null) { + // check if the current Node contains given item + if (item.equals(current.item)) { + // item was found + return true; + } + + // move on to next node + current = current.next; + } + } + + // item not located in list, or list empty return false; } @@ -139,7 +158,7 @@ public boolean contains(E item) { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -149,7 +168,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** From 76f49d9f1a00d2665769f06fe43ba383f2389727 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 19:18:10 -0800 Subject: [PATCH 18/52] Added constructor to LinkedList and its Node - Implemented addFront method - Fixed isEmpty method - Implemented tests for size, isEmpty, and contains --- src/LinkedList.java | 35 ++++++++++++++++-- tests/LinkedListTest.java | 74 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 104 insertions(+), 5 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index b1e12ba..29924d5 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -14,6 +14,14 @@ private class Node { * The node this Node is pointing to */ Node next; + + /** + * Creates a node, and stores the given item within it + * @param item the item being stored in node + */ + public Node(E item) { + this.item = item; + } } /** @@ -26,6 +34,15 @@ private class Node { */ private int size; + /** + * Constructs an empty LinkedList + */ + public LinkedList() { + // list starts off empty + head = null; + size = 0; + } + /** * Add item to the front. * @@ -33,7 +50,21 @@ private class Node { */ @Override public void addFront(E item) { + // create new node containing given item + Node newNode = new Node(item); + + // if the list is not empty + if(!isEmpty()) { + // point new node at head + newNode.next = head; + } + + // override head with newly created node + // (making new node first node in list) + head = newNode; + // account for new item in list + size++; } /** @@ -130,7 +161,7 @@ public E remove(int index) { @Override public boolean contains(E item) { // if list is not empty - if(head != null) { + if(!isEmpty()) { // setup pointer Node current = head; @@ -158,7 +189,7 @@ public boolean contains(E item) { */ @Override public boolean isEmpty() { - return size == 0; + return size == 0 && head == null; } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index a8e6a19..7157fd9 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -28,6 +28,16 @@ class LinkedListTest { */ private final int TEST_ITEM = 20; + /** + * Adds ten items to list for testing + */ + private void addMultipleItems() { + // add 10 items to list + for(int i = 0; i < 10; i++) { + testLinkedList.addFront(FILLER_ITEM); + } + } + @Test void addFront() { } @@ -65,14 +75,72 @@ void removeIndex() { } @Test - void contains() { + void contains_itemExists_returnsTrue() { + // add several initial items + addMultipleItems(); + + // add item to check for + testLinkedList.addFront(TEST_ITEM); + + // check if item is in list + assertTrue(testLinkedList.contains(TEST_ITEM)); } @Test - void isEmpty() { + void contains_itemNotExists_returnsFalse() { + // add several initial items + addMultipleItems(); + + // check if item is in buffer + assertFalse(testLinkedList.contains(TEST_ITEM)); + } + + @Test + void contains_empty_returnsFalse() { + assertFalse(testLinkedList.contains(TEST_ITEM)); + } + + @Test + void isEmpty_hasItem_returnsFalse() { + // add item so the list is not empty + testLinkedList.addFront(FILLER_ITEM); + + assertFalse(testLinkedList.isEmpty()); + } + + @Test + void isEmpty_hasMultipleItems_returnsFalse() { + // add items so list is not empty + addMultipleItems(); + + // check if list is empty + assertFalse(testLinkedList.isEmpty()); + } + + @Test + void isEmpty_empty_returnsTrue() { + // check if list is empty + assertTrue(testLinkedList.isEmpty()); + } + + @Test + void size_hasItem_returnsSize() { + // add item so list is not empty + testLinkedList.addFront(FILLER_ITEM); + + assertEquals(1, testLinkedList.size()); + } + + @Test + void size_hasMultipleItems_returnsSize() { + // add items so the list is not empty + addMultipleItems(); + + assertEquals(10, testLinkedList.size()); } @Test - void size() { + void size_empty_returnsSize() { + assertEquals(0, testLinkedList.size()); } } \ No newline at end of file From 2515cbc1548de3ac36e01d4b10fb2261d79b042a Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 19:36:21 -0800 Subject: [PATCH 19/52] Implemented addBack and get methods for LinkedList - Implemented tests for get method --- src/LinkedList.java | 52 +++++++++++++++++++++++++- tests/LinkedListTest.java | 79 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 127 insertions(+), 4 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 29924d5..7d4643b 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,4 +1,5 @@ import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedList implements List { /** @@ -20,7 +21,11 @@ private class Node { * @param item the item being stored in node */ public Node(E item) { + // store given item this.item = item; + + // node starts off disconnected + next = null; } } @@ -74,7 +79,30 @@ public void addFront(E item) { */ @Override public void addBack(E item) { + // if list is empty + if(isEmpty()) { + // add item at front of list + addFront(item); + } + + // if list is not empty + else { + // create new node containing given item + Node newNode = new Node(item); + + // setup tracker and run to end of list + Node current = head; + + while(current.next != null) { + current = current.next; + } + + // point final node at new node + current.next = newNode; + // account for new item in list + size++; + } } /** @@ -96,7 +124,29 @@ public void add(int index, E item) { */ @Override public E get(int index) { - return null; + // if given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if list is empty + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty LinkedList"); + } + + // setup trackers + int currIndex = 0; + Node current = head; + + // run through list, up to given index + while(current != null && currIndex != index) { + // update trackers + current = current.next; + currIndex++; + } + + // return item at current node/index + return current.item; } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 7157fd9..6f2e1cc 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -1,5 +1,7 @@ import org.junit.jupiter.api.Test; +import java.util.NoSuchElementException; + import static org.junit.jupiter.api.Assertions.*; class LinkedListTest { @@ -34,7 +36,7 @@ class LinkedListTest { private void addMultipleItems() { // add 10 items to list for(int i = 0; i < 10; i++) { - testLinkedList.addFront(FILLER_ITEM); + testLinkedList.addBack(FILLER_ITEM); } } @@ -51,7 +53,78 @@ void add() { } @Test - void get() { + void get_hasItem_returnsItem() { + // add expected item to list + testLinkedList.addFront(TEST_ITEM); + + // attempt to retrieve expected value + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + } + + @Test + void get_hasMultipleItems_returnsItem() { + // add several initial items + addMultipleItems(); + + // add item to retrieve + testLinkedList.addBack(TEST_ITEM); + + // attempt to retrieve expected item at final index + assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() - 1)); + } + + @Test + void get_listEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get value from empty list + testLinkedList.get(FIRST_INDEX); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void get_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get item from invalid index + testLinkedList.get(-1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void get_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to get item from invalid index + testLinkedList.get(1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test @@ -80,7 +153,7 @@ void contains_itemExists_returnsTrue() { addMultipleItems(); // add item to check for - testLinkedList.addFront(TEST_ITEM); + testLinkedList.addBack(TEST_ITEM); // check if item is in list assertTrue(testLinkedList.contains(TEST_ITEM)); From 288a5379e127c4955883aeebb6cf9c8e03800ac5 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 20:49:56 -0800 Subject: [PATCH 20/52] Implemented tests for addFront and addBack --- tests/ArrayListTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 6720cef..e3bf106 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -662,7 +662,7 @@ void contains_bufferFullItemExists_returnsTrue() { @Test void isEmpty_bufferContainsOneItem_returnsFalse() { // add item so the buffer is not empty - testArrayList.addFront(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); assertFalse(testArrayList.isEmpty()); } @@ -670,9 +670,9 @@ void isEmpty_bufferContainsOneItem_returnsFalse() { @Test void isEmpty_bufferContainsMultipleItems_returnsFalse() { // add items so the buffer is not empty - testArrayList.addFront(FILLER_ITEM); - testArrayList.addFront(FILLER_ITEM); - testArrayList.addFront(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); // check if buffer is empty assertFalse(testArrayList.isEmpty()); @@ -698,7 +698,7 @@ void isEmpty_bufferEmpty_returnsTrue() { @Test void size_bufferContainsOneItem_returnsSize() { // add item so the buffer is not empty - testArrayList.addFront(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); assertEquals(1, testArrayList.size()); } @@ -706,9 +706,9 @@ void size_bufferContainsOneItem_returnsSize() { @Test void size_bufferContainsMultipleItems_returnsSize() { // add items so the buffer is not empty - testArrayList.addFront(FILLER_ITEM); - testArrayList.addFront(FILLER_ITEM); - testArrayList.addFront(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); + testArrayList.addBack(FILLER_ITEM); assertEquals(3, testArrayList.size()); } From dd34dbdade19076fe0ab7538cad7185b50597e46 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 22:05:08 -0800 Subject: [PATCH 21/52] Committed wrong file last time - Implemented tests for addFront and addBack --- tests/LinkedListTest.java | 66 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 6f2e1cc..8fd9956 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -41,11 +41,69 @@ private void addMultipleItems() { } @Test - void addFront() { + void addFront_hasItem_addedSuccessfully() { + // add initial item + testLinkedList.addBack(FILLER_ITEM); + + // add test value + testLinkedList.addFront(TEST_ITEM); + + // ensure expected value is at index 0 + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + } + + @Test + void addFront_hasMultipleItems_addedSuccessfully() { + // add several initial values + addMultipleItems(); + + // add test value + testLinkedList.addFront(TEST_ITEM); + + // ensure expected value is at index 0 + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + } + + @Test + void addFront_empty_addedSuccessfully() { + // make test value new head + testLinkedList.addFront(TEST_ITEM); + + // ensure expected value is at index 0 + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); } @Test - void addBack() { + void addBack_hasItem_addedSuccessfully() { + // add initial value + testLinkedList.addFront(FILLER_ITEM); + + // add test value + testLinkedList.addBack(TEST_ITEM); + + // ensure expected value is at end of list + assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); + } + + @Test + void addBack_hasMultipleItems_addedSuccessfully() { + // add several initial values + addMultipleItems(); + + // add test value + testLinkedList.addBack(TEST_ITEM); + + // ensure expected value is at end of list + assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); + } + + @Test + void addBack_empty_addedSuccessfully() { + // make test value new tail + testLinkedList.addBack(TEST_ITEM); + + // ensure expected value is at end of list + assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); } @Test @@ -98,7 +156,7 @@ void get_invalidIndexNegative_throwsException() { try { // attempt to get item from invalid index - testLinkedList.get(-1); + testLinkedList.get(INVALID_INDEX); } catch (IndexOutOfBoundsException e) { @@ -116,7 +174,7 @@ void get_invalidIndexMoreThanSize_throwsException() { try { // attempt to get item from invalid index - testLinkedList.get(1); + testLinkedList.get(FIRST_INDEX + 1); } catch (IndexOutOfBoundsException e) { From dad9e26cd56fb1ad1d0a728ba5e2ab896c753a94 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 22:20:01 -0800 Subject: [PATCH 22/52] Implemented LinkedList's removeFront and removeBack methods - Created tests for those methods --- src/LinkedList.java | 40 ++++++++++++++++- tests/LinkedListTest.java | 90 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 7d4643b..83c7d20 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -168,7 +168,22 @@ public void set(int index, E item) { */ @Override public E removeFront() { - return null; + // if list contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + } + + // get requested item from head + E requestedItem = head.item; + + // overwrite head with next item in sequence, + // if no next item, head will become null + head = head.next; + + // account for element removal + size--; + + return requestedItem; } /** @@ -178,7 +193,28 @@ public E removeFront() { */ @Override public E removeBack() { - return null; + // if list contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + } + + // setup trackers and run through list till second to last node + Node current = head; + + while(current.next.next != null) { + current = current.next; + } + + // get requested item from final node + E requestedItem = current.next.item; + + // update current node to stop tracking final node + current.next = null; + + // account for item removal + size--; + + return requestedItem; } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 8fd9956..8c5e1eb 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -190,11 +190,97 @@ void set() { } @Test - void removeFront() { + void removeFront_hasItem_removedSuccessfully() { + // add value to remove + testLinkedList.addFront(TEST_ITEM); + + // attempt to remove value from list + assertEquals(TEST_ITEM, testLinkedList.removeFront()); + + // ensure list is now empty + assertTrue(testLinkedList.isEmpty()); + } + + @Test + void removeFront_hasMultipleItems_removedSuccessfully() { + // add several initial values + addMultipleItems(); + + // add value to remove + testLinkedList.addFront(TEST_ITEM); + + // attempt to remove front value from list + testLinkedList.removeFront(); + + // ensure value was removed from list + assertNotEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + assertFalse(testLinkedList.contains(TEST_ITEM)); + assertEquals(10, testLinkedList.size()); + } + + @Test + void removeFront_empty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove nonexistent item + testLinkedList.removeFront(); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test - void removeBack() { + void removeBack_hasItem_removedSuccessfully() { + // add item to remove + testLinkedList.addBack(TEST_ITEM); + + // attempt to remove item from list + testLinkedList.removeFront(); + + // ensure list is now empty + assertTrue(testLinkedList.isEmpty()); + } + + @Test + void removeBack_hasMultipleItems_removedSuccessfully() { + // add several initial items + addMultipleItems(); + + // add item to remove + testLinkedList.addBack(TEST_ITEM); + + // attempt to remove front item from list + testLinkedList.removeBack(); + + // ensure item was removed from list + assertNotEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() - 1)); + assertFalse(testLinkedList.contains(TEST_ITEM)); + assertEquals(10, testLinkedList.size()); + } + + @Test + void removeBack_empty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove nonexistent item + testLinkedList.removeBack(); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From cf36dafc626c3760bd0e8d24632bf3459958de17 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 22:39:56 -0800 Subject: [PATCH 23/52] Implemented LinkedList's add method and tests for it --- src/LinkedList.java | 37 +++++++++++++ tests/LinkedListTest.java | 113 ++++++++++++++++++++++++++++++-------- 2 files changed, 126 insertions(+), 24 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 83c7d20..47747e5 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -113,7 +113,44 @@ public void addBack(E item) { */ @Override public void add(int index, E item) { + // if given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + // if given index was at front or back, use those methods + else if(index == 0) { + addFront(item); + } + + else if(index == size) { + addBack(item); + } + + else { + // setup trackers + Node previous = null; + Node current = head; + int currIndex = 0; + + // run through list, up to given index + while(current != null && currIndex != index) { + // update trackers + previous = current; + current = current.next; + currIndex++; + } + + // place given item in new node + Node newNode = new Node(item); + + // update links of previous and current nodes to account for new node + previous.next = newNode; + newNode.next = current; + + // account for new item in list + size++; + } } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 8c5e1eb..44cd0f5 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -45,69 +45,134 @@ void addFront_hasItem_addedSuccessfully() { // add initial item testLinkedList.addBack(FILLER_ITEM); - // add test value + // add test item testLinkedList.addFront(TEST_ITEM); - // ensure expected value is at index 0 + // ensure expected item is at index 0 assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); } @Test void addFront_hasMultipleItems_addedSuccessfully() { - // add several initial values + // add several initial items addMultipleItems(); - // add test value + // add test item testLinkedList.addFront(TEST_ITEM); - // ensure expected value is at index 0 + // ensure expected item is at index 0 assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); } @Test void addFront_empty_addedSuccessfully() { - // make test value new head + // make test item new head testLinkedList.addFront(TEST_ITEM); - // ensure expected value is at index 0 + // ensure expected item is at index 0 assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); } @Test void addBack_hasItem_addedSuccessfully() { - // add initial value + // add initial item testLinkedList.addFront(FILLER_ITEM); - // add test value + // add test item testLinkedList.addBack(TEST_ITEM); - // ensure expected value is at end of list + // ensure expected item is at end of list assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); } @Test void addBack_hasMultipleItems_addedSuccessfully() { - // add several initial values + // add several initial items addMultipleItems(); - // add test value + // add test item testLinkedList.addBack(TEST_ITEM); - // ensure expected value is at end of list + // ensure expected item is at end of list assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); } @Test void addBack_empty_addedSuccessfully() { - // make test value new tail + // make test item new tail testLinkedList.addBack(TEST_ITEM); - // ensure expected value is at end of list + // ensure expected item is at end of list assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() -1)); } @Test - void add() { + void add_hasItem_addedSuccessfully() { + // add initial item + testLinkedList.addFront(FILLER_ITEM); + + // add test item + testLinkedList.add(testLinkedList.size(), TEST_ITEM); + + // ensure expected item is at expected index + assertEquals(TEST_ITEM, testLinkedList.get(testLinkedList.size() - 1)); + } + + @Test + void add_hasMultipleItems_addedSuccessfully() { + // add several initial items + addMultipleItems(); + + // add test item + testLinkedList.add(4, TEST_ITEM); + + // ensure expected item is at expected index + assertEquals(TEST_ITEM, testLinkedList.get(4)); + } + + @Test + void add_empty_addedSuccessfully() { + // add test item to list + testLinkedList.add(FIRST_INDEX, TEST_ITEM); + + // ensure expected item is in list + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + } + + @Test + void add_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to add item at invalid index + testLinkedList.add(FIRST_INDEX - 1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void add_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to add item at invalid index + testLinkedList.add(FIRST_INDEX + 1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test @@ -115,7 +180,7 @@ void get_hasItem_returnsItem() { // add expected item to list testLinkedList.addFront(TEST_ITEM); - // attempt to retrieve expected value + // attempt to retrieve expected item assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); } @@ -137,7 +202,7 @@ void get_listEmpty_throwsException() { boolean exceptionThrown = false; try { - // attempt to get value from empty list + // attempt to get item from empty list testLinkedList.get(FIRST_INDEX); } @@ -191,10 +256,10 @@ void set() { @Test void removeFront_hasItem_removedSuccessfully() { - // add value to remove + // add item to remove testLinkedList.addFront(TEST_ITEM); - // attempt to remove value from list + // attempt to remove item from list assertEquals(TEST_ITEM, testLinkedList.removeFront()); // ensure list is now empty @@ -203,16 +268,16 @@ void removeFront_hasItem_removedSuccessfully() { @Test void removeFront_hasMultipleItems_removedSuccessfully() { - // add several initial values + // add several initial items addMultipleItems(); - // add value to remove + // add item to remove testLinkedList.addFront(TEST_ITEM); - // attempt to remove front value from list + // attempt to remove front item from list testLinkedList.removeFront(); - // ensure value was removed from list + // ensure item was removed from list assertNotEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); assertFalse(testLinkedList.contains(TEST_ITEM)); assertEquals(10, testLinkedList.size()); From 42e0cbe3dd001141b8344f37b80c867c560d101d Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 22:50:03 -0800 Subject: [PATCH 24/52] Implemented LinkedList's set method and tests for it --- src/LinkedList.java | 25 ++++++++++++++ tests/LinkedListTest.java | 70 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 47747e5..eb3aedc 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -195,7 +195,32 @@ public E get(int index) { */ @Override public void set(int index, E item) { + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if list is empty + if(isEmpty()) { + // add given item to list at front + addFront(item); + } + + else { + // setup trackers + int currIndex = 0; + Node current = head; + + // run through list, up to given index + while (current != null && currIndex != index) { + // update trackers + current = current.next; + currIndex++; + } + // overwrite item in current node with given item + current.item = item; + } } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 44cd0f5..dcde4c3 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -251,7 +251,75 @@ void get_invalidIndexMoreThanSize_throwsException() { } @Test - void set() { + void set_hasItem_setItemAtIndex() { + // add initial item to buffer + testLinkedList.addFront(FILLER_ITEM); + + // replace item at that index + testLinkedList.set(FIRST_INDEX, TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + } + + @Test + void set_hasMultipleItems_setItemAtIndex() { + // add several initial items + addMultipleItems(); + + // replace item at sixth index + testLinkedList.set(6, TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testLinkedList.get(6)); + } + + @Test + void set_empty_setItemAtIndex() { + // add item to index 0 of empty buffer + testLinkedList.set(FIRST_INDEX, TEST_ITEM); + + // attempt to retrieve expected item + assertEquals(TEST_ITEM, testLinkedList.get(FIRST_INDEX)); + + // ensure size tracker was updated + assertEquals(1, testLinkedList.size()); + } + + @Test + void set_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to set item at invalid index + testLinkedList.set(FIRST_INDEX - 1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void set_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to set item at invalid index + testLinkedList.set(FIRST_INDEX + 1, TEST_ITEM); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From f367ee9abb1dce871a40fce4d87531ae3397ec5d Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 22:53:20 -0800 Subject: [PATCH 25/52] Implemented an iterator helper for LinkedList --- src/ArrayList.java | 2 +- src/LinkedList.java | 53 +++++++++++++++++++++++++++++++++++++++- tests/ArrayListTest.java | 10 ++++---- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index a087ae7..f529937 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -356,7 +356,7 @@ private class ArrayListIterator implements Iterator { private int index; /** - * Constructs an ArrayIntListIterator with the index initialized to 0 + * Constructs an ArrayIntList iterator with the index initialized to 0 */ ArrayListIterator() { index = 0; diff --git a/src/LinkedList.java b/src/LinkedList.java index eb3aedc..05a52d0 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -357,6 +357,57 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new LinkedListIterator(); + } + + /** + * Implementation of an Iterator for the LinkedList class + */ + private class LinkedListIterator implements Iterator { + /** + * The current Node being tracked by the Iterator + */ + private Node current; + + /** + * Constructs a LinkedList iterator with the head tracked as the first Node + */ + LinkedListIterator() { + current = head; + } + + /** + * Returns {@code true} if the iteration has more elements. + * (In other words, returns {@code true} if {@link #next} would + * return an element rather than throwing an exception.) + * + * @return {@code true} if the iteration has more elements + */ + @Override + public boolean hasNext() { + return current != null; + } + + /** + * Returns the next element in the iteration. + * + * @return the next element in the iteration + * @throws NoSuchElementException if the iteration has no more elements + */ + @Override + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get the item stored in current node + E currItem = current.item; + + // move on to the next node + current = current.next; + + // return the current item + return currItem; + } } } diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index e3bf106..37c14dc 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -218,7 +218,7 @@ void add_invalidIndexMoreThanSize_throwsException() { } @Test - void get_bufferContainsOneItem_returnsitem() { + void get_bufferContainsOneItem_returnsItem() { // add expected item to buffer testArrayList.addFront(TEST_ITEM); @@ -227,7 +227,7 @@ void get_bufferContainsOneItem_returnsitem() { } @Test - void get_bufferContainsMultipleItems_returnsitem() { + void get_bufferContainsMultipleItems_returnItem() { // add several initial items testArrayList.addBack(FILLER_ITEM); testArrayList.addBack(FILLER_ITEM); @@ -704,7 +704,7 @@ void size_bufferContainsOneItem_returnsSize() { } @Test - void size_bufferContainsMultipleItems_returnsSize() { + void size_bufferContainsMultipleItems_returnSize() { // add items so the buffer is not empty testArrayList.addBack(FILLER_ITEM); testArrayList.addBack(FILLER_ITEM); @@ -714,12 +714,12 @@ void size_bufferContainsMultipleItems_returnsSize() { } @Test - void size_bufferEmpty_returnsSize() { + void size_bufferEmpty_returnSize() { assertEquals(0, testArrayList.size()); } @Test - void size_bufferFull_returnsSize() { + void size_bufferFull_returnSize() { // add 10 items to buffer for(int i = 0; i < DEFAULT_BUFFER_LENGTH; i++) { testArrayList.addBack(FILLER_ITEM); From 743e6894d44648c503a1eeb1ff3d2b69310dff36 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Wed, 14 Feb 2024 23:10:08 -0800 Subject: [PATCH 26/52] Implemented and tested LinkedList's remove(index) method --- src/LinkedList.java | 46 ++++++++++++++++++- tests/LinkedListTest.java | 96 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 140 insertions(+), 2 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 05a52d0..4b7c47e 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -297,7 +297,51 @@ public void remove(E item) { */ @Override public E remove(int index) { - return null; + // if the given index is out of range + if(index < 0 || index > size) { + throw new IndexOutOfBoundsException(index + " is not a valid index"); + } + + // if list contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + } + + // if given index was at front or back, use those methods + else if(index == 0) { + return removeFront(); + } + + else if(index == size) { + return removeBack(); + } + + else { + // setup trackers + Node previous = null; + Node current = head; + int currIndex = 0; + + // run through list, up to given index + while(current != null && currIndex != index) { + // update trackers + previous = current; + current = current.next; + currIndex++; + } + + // get item stored in node at current index + E requestedItem = current.item; + + // point previous node at current's next node, + // thereby cutting out current + previous.next = current.next; + + // account for removal of item + size--; + + return requestedItem; + } } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index dcde4c3..617dcbd 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -421,7 +421,101 @@ void removeItem() { } @Test - void removeIndex() { + void removeIndex_hasItem_removedSuccessfully() { + // add item to remove + testLinkedList.addBack(TEST_ITEM); + + // attempt to remove item from list + assertEquals(TEST_ITEM, testLinkedList.remove(FIRST_INDEX)); + + // ensure list is now empty + assertTrue(testLinkedList.isEmpty()); + } + + @Test + void removeIndex_removeItemFromMiddle_removedSuccessfully() { + // add several initial items + addMultipleItems(); + + // add item to remove + testLinkedList.add(7, TEST_ITEM); + + // attempt to remove item from list + assertEquals(TEST_ITEM, testLinkedList.remove(7)); + + // ensure item was removed from list + assertFalse(testLinkedList.contains(TEST_ITEM)); + assertEquals(10, testLinkedList.size()); + } + + @Test + void removeIndex_removeItemFromEnd_removedSuccessfully() { + // add several initial items + addMultipleItems(); + + // add item to remove + testLinkedList.add(testLinkedList.size(), TEST_ITEM); + + // attempt to remove item from list + assertEquals(TEST_ITEM, testLinkedList.remove(testLinkedList.size())); + + // ensure item was removed from list + assertFalse(testLinkedList.contains(TEST_ITEM)); + assertEquals(10, testLinkedList.size()); + } + + @Test + void removeIndex_bufferEmpty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove nonexistent item + testLinkedList.remove(FIRST_INDEX); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void removeIndex_invalidIndexNegative_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at invalid index + testLinkedList.remove(FIRST_INDEX - 1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + + @Test + void removeIndex_invalidIndexMoreThanSize_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item at invalid index + testLinkedList.remove(FIRST_INDEX + 1); + } + + catch (IndexOutOfBoundsException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test From e60d55929e0da0210c3306d1e330f6becbe01bd7 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 00:09:43 -0800 Subject: [PATCH 27/52] Implemented and tested LinkedList's remove(item) method --- src/LinkedList.java | 29 ++++++++++++++ tests/LinkedListTest.java | 83 +++++++++++++++++++++++++++++++++------ 2 files changed, 101 insertions(+), 11 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 4b7c47e..f64f0c5 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -286,7 +286,36 @@ public E removeBack() { */ @Override public void remove(E item) { + // if list contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + } + + // check if list contains given item + if(!contains(item)) { + throw new NoSuchElementException("Given item is not located in LinkedList"); + } + + // setup trackers and flag + Node current = head; + int currIndex = 0; + boolean itemNotFound = true; + + // run through list until item is found + while(current != null && itemNotFound) { + // check if the current node contains given item + if(item.equals(current.item)) { + // call the other remove method on current index + remove(currIndex); + + // cease loop, item was found and removed + itemNotFound = false; + } + // update trackers + current = current.next; + currIndex++; + } } /** diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 617dcbd..3d10459 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -15,11 +15,6 @@ class LinkedListTest { */ private final int FIRST_INDEX = 0; - /** - * The item returned by methods if item not found - */ - private final int INVALID_INDEX = -1; - /** * The standard filler item added to container prior to testing method */ @@ -221,7 +216,7 @@ void get_invalidIndexNegative_throwsException() { try { // attempt to get item from invalid index - testLinkedList.get(INVALID_INDEX); + testLinkedList.get(FIRST_INDEX - 1); } catch (IndexOutOfBoundsException e) { @@ -252,7 +247,7 @@ void get_invalidIndexMoreThanSize_throwsException() { @Test void set_hasItem_setItemAtIndex() { - // add initial item to buffer + // add initial item to list testLinkedList.addFront(FILLER_ITEM); // replace item at that index @@ -276,7 +271,7 @@ void set_hasMultipleItems_setItemAtIndex() { @Test void set_empty_setItemAtIndex() { - // add item to index 0 of empty buffer + // add item to index 0 of empty list testLinkedList.set(FIRST_INDEX, TEST_ITEM); // attempt to retrieve expected item @@ -416,8 +411,74 @@ void removeBack_empty_throwsException() { assertTrue(exceptionThrown); } + // + + @Test + void removeItem_hasItem_removedSuccessfully() { + // add item to remove + testLinkedList.addBack(TEST_ITEM); + + // attempt to remove item from list + testLinkedList.remove((Integer) TEST_ITEM); + + // ensure list is now empty + assertTrue(testLinkedList.isEmpty()); + } + + @Test + void removeItem_hasMultipleItems_removedSuccessfully() { + // add several initial items + addMultipleItems(); + + // add item to remove + testLinkedList.add(9, TEST_ITEM); + + // attempt to remove item from list + testLinkedList.remove((Integer) TEST_ITEM); + + // ensure item was removed from list + assertNotEquals(TEST_ITEM, testLinkedList.get(9)); + assertFalse(testLinkedList.contains(TEST_ITEM)); + assertEquals(10, testLinkedList.size()); + } + + @Test + void removeItem_empty_throwsException() { + // setup flag + boolean exceptionThrown = false; + + try { + // attempt to remove item from empty list + testLinkedList.remove((Integer) TEST_ITEM); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); + } + @Test - void removeItem() { + void removeItem_itemNotExists_throwsException() { + // setup flag + boolean exceptionThrown = false; + + // add several initial items + addMultipleItems(); + + try { + // attempt to remove nonexistent item from list + testLinkedList.remove((Integer) TEST_ITEM); + } + + catch (NoSuchElementException e) { + exceptionThrown = true; + } + + // check if exception was thrown + assertTrue(exceptionThrown); } @Test @@ -465,7 +526,7 @@ void removeIndex_removeItemFromEnd_removedSuccessfully() { } @Test - void removeIndex_bufferEmpty_throwsException() { + void removeIndex_empty_throwsException() { // setup flag boolean exceptionThrown = false; @@ -535,7 +596,7 @@ void contains_itemNotExists_returnsFalse() { // add several initial items addMultipleItems(); - // check if item is in buffer + // check if item is in list assertFalse(testLinkedList.contains(TEST_ITEM)); } From ccd5edc456d33f5e70aa984ca5fa66e8a0f8f947 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 01:43:30 -0800 Subject: [PATCH 28/52] Organized project, created Bag interface file --- src/APIs/Bag.java | 5 +++++ src/{ => APIs}/Queue.java | 4 +++- src/{ => APIs}/Stack.java | 4 +++- src/{ => List}/ArrayList.java | 18 ++++++++++-------- src/{ => List}/LinkedList.java | 24 +++++++++++++----------- src/{ => List}/List.java | 4 +++- tests/ArrayListTest.java | 5 +++-- tests/LinkedListTest.java | 5 +++-- 8 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 src/APIs/Bag.java rename src/{ => APIs}/Queue.java (91%) rename src/{ => APIs}/Stack.java (93%) rename src/{ => List}/ArrayList.java (96%) rename src/{ => List}/LinkedList.java (95%) rename src/{ => List}/List.java (96%) diff --git a/src/APIs/Bag.java b/src/APIs/Bag.java new file mode 100644 index 0000000..ae82981 --- /dev/null +++ b/src/APIs/Bag.java @@ -0,0 +1,5 @@ +package APIs; + +public interface Bag { + +} diff --git a/src/Queue.java b/src/APIs/Queue.java similarity index 91% rename from src/Queue.java rename to src/APIs/Queue.java index ab5ca29..a2a5575 100644 --- a/src/Queue.java +++ b/src/APIs/Queue.java @@ -1,5 +1,7 @@ +package APIs; + /** - * FIFO (first-in, first-out) Queue API + * FIFO (first-in, first-out) APIs.Queue API * @param class / data type of the items in the queue */ public interface Queue extends Iterable { diff --git a/src/Stack.java b/src/APIs/Stack.java similarity index 93% rename from src/Stack.java rename to src/APIs/Stack.java index bb1000d..1cfe014 100644 --- a/src/Stack.java +++ b/src/APIs/Stack.java @@ -1,5 +1,7 @@ +package APIs; + /** - * Stack (LIFO: last-in, first-out) API + * APIs.Stack (LIFO: last-in, first-out) API * @param class / data type of the items in the stack */ public interface Stack extends Iterable { diff --git a/src/ArrayList.java b/src/List/ArrayList.java similarity index 96% rename from src/ArrayList.java rename to src/List/ArrayList.java index f529937..1c34ac4 100644 --- a/src/ArrayList.java +++ b/src/List/ArrayList.java @@ -1,9 +1,11 @@ +package List; + import java.util.Iterator; import java.util.NoSuchElementException; public class ArrayList implements List { /** - * An array used to store items placed within the ArrayList + * An array used to store items placed within the List.ArrayList */ private E[] buffer; @@ -13,7 +15,7 @@ public class ArrayList implements List { private int size; /** - * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 items + * Constructs an List.ArrayList with an empty buffer, and a default max capacity of 10 items */ public ArrayList() { // setup buffer with default max capacity of 10 @@ -131,7 +133,7 @@ public E get(int index) { // if buffer contains no items, one cannot be retrieved if(isEmpty()) { - throw new NoSuchElementException("Cannot retrieve item from empty ArrayList"); + throw new NoSuchElementException("Cannot retrieve item from empty List.ArrayList"); } return buffer[index]; @@ -169,7 +171,7 @@ public void set(int index, E item) { public E removeFront() { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); } // get requested item prior to removal @@ -196,7 +198,7 @@ public E removeFront() { public E removeBack() { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); } // get requested item prior to removal, accounting for index @@ -220,7 +222,7 @@ public E removeBack() { public void remove(E item) { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); } // find item in buffer and get its index @@ -228,7 +230,7 @@ public void remove(E item) { // if item is not in buffer if(index == -1) { - throw new NoSuchElementException("Given item is not located in ArrayList"); + throw new NoSuchElementException("Given item is not located in List.ArrayList"); } // otherwise, run through buffer, starting at given index @@ -276,7 +278,7 @@ public E remove(int index) { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); } // get requested item prior to removal diff --git a/src/LinkedList.java b/src/List/LinkedList.java similarity index 95% rename from src/LinkedList.java rename to src/List/LinkedList.java index f64f0c5..c4fb6b3 100644 --- a/src/LinkedList.java +++ b/src/List/LinkedList.java @@ -1,3 +1,5 @@ +package List; + import java.util.Iterator; import java.util.NoSuchElementException; @@ -30,17 +32,17 @@ public Node(E item) { } /** - * The first Node of the current LinkedList + * The first Node of the current List.LinkedList */ private Node head; /** - * The number of Nodes contained within this LinkedList + * The number of Nodes contained within this List.LinkedList */ private int size; /** - * Constructs an empty LinkedList + * Constructs an empty List.LinkedList */ public LinkedList() { // list starts off empty @@ -168,7 +170,7 @@ public E get(int index) { // if list is empty if(isEmpty()) { - throw new NoSuchElementException("Cannot retrieve item from empty LinkedList"); + throw new NoSuchElementException("Cannot retrieve item from empty List.LinkedList"); } // setup trackers @@ -232,7 +234,7 @@ public void set(int index, E item) { public E removeFront() { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); } // get requested item from head @@ -257,7 +259,7 @@ public E removeFront() { public E removeBack() { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); } // setup trackers and run through list till second to last node @@ -288,12 +290,12 @@ public E removeBack() { public void remove(E item) { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); } // check if list contains given item if(!contains(item)) { - throw new NoSuchElementException("Given item is not located in LinkedList"); + throw new NoSuchElementException("Given item is not located in List.LinkedList"); } // setup trackers and flag @@ -333,7 +335,7 @@ public E remove(int index) { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); } // if given index was at front or back, use those methods @@ -434,7 +436,7 @@ public Iterator iterator() { } /** - * Implementation of an Iterator for the LinkedList class + * Implementation of an Iterator for the List.LinkedList class */ private class LinkedListIterator implements Iterator { /** @@ -443,7 +445,7 @@ private class LinkedListIterator implements Iterator { private Node current; /** - * Constructs a LinkedList iterator with the head tracked as the first Node + * Constructs a List.LinkedList iterator with the head tracked as the first Node */ LinkedListIterator() { current = head; diff --git a/src/List.java b/src/List/List.java similarity index 96% rename from src/List.java rename to src/List/List.java index 2bf4aef..cdf12a6 100644 --- a/src/List.java +++ b/src/List/List.java @@ -1,5 +1,7 @@ +package List; + /*** - * List interface (API / abstract data type) + * List.List interface (API / abstract data type) * @param Class or data type of the items in the list. */ public interface List extends Iterable { diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 37c14dc..ec5fb9c 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -1,3 +1,4 @@ +import List.ArrayList; import org.junit.jupiter.api.Test; import java.util.NoSuchElementException; @@ -7,9 +8,9 @@ class ArrayListTest { /** - * Create ArrayList at start for testing + * Create List.ArrayList at start for testing */ - private final ArrayList testArrayList = new ArrayList<>(); + private final ArrayList testArrayList = new ArrayList(); /** * The default max capacity of buffer, at creation diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 3d10459..c136580 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -1,3 +1,4 @@ +import List.LinkedList; import org.junit.jupiter.api.Test; import java.util.NoSuchElementException; @@ -6,9 +7,9 @@ class LinkedListTest { /** - * Create LinkedList at start for testing + * Create List.LinkedList at start for testing */ - private final LinkedList testLinkedList = new LinkedList<>(); + private final LinkedList testLinkedList = new LinkedList(); /** * The first index in list is 0 From c2c9f81d47b54d8e187b28c64ac78547632f2326 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 02:05:13 -0800 Subject: [PATCH 29/52] Created Bag interface --- src/APIs/Bag.java | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/APIs/Bag.java b/src/APIs/Bag.java index ae82981..c4fc851 100644 --- a/src/APIs/Bag.java +++ b/src/APIs/Bag.java @@ -1,5 +1,21 @@ package APIs; -public interface Bag { +public interface Bag extends Iterable { + /** + * Adds the given item to the bag + * @param item the item to be added + */ + void add(E item); -} + /** + * Checks whether the bag is empty, and returns true/false accordingly + * @return true if the bag is empty; otherwise false + */ + boolean isEmpty(); + + /** + * Gets and returns the number of items stored in the bag + * @return the number of items stored in the bag + */ + int size(); +} \ No newline at end of file From 8cce40283901e9ed1a216ba54798b252aba9f6f4 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 02:13:05 -0800 Subject: [PATCH 30/52] Created ResizingArrayStack implementing Stack - Copied over its tester --- src/APIs/ResizingArrayStack.java | 66 ++++++++++++++++++++++++++++++++ src/APIs/StackTestClient.java | 33 ++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 src/APIs/ResizingArrayStack.java create mode 100644 src/APIs/StackTestClient.java diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java new file mode 100644 index 0000000..cee4992 --- /dev/null +++ b/src/APIs/ResizingArrayStack.java @@ -0,0 +1,66 @@ +package APIs; + +import java.util.Iterator; + +public class ResizingArrayStack implements Stack { + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + return null; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return null; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} diff --git a/src/APIs/StackTestClient.java b/src/APIs/StackTestClient.java new file mode 100644 index 0000000..b2a6af6 --- /dev/null +++ b/src/APIs/StackTestClient.java @@ -0,0 +1,33 @@ +package APIs; + +import java.util.Scanner; + +public class StackTestClient { + public static void main(String[] args) { + // setup stack + Stack storage = new ResizingArrayStack(); + + // setup scanner, taking in given string + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + + // run through scanner + while (in.hasNext()) { + // get current item (word) + String item = in.next(); + + // if the current item is a dash + if (item.equals("-")) { + // add a value to stack + storage.push("item"); + } + + // if the stack is not empty + else if (!storage.isEmpty()) { + System.out.println("s.pop()" + " "); + } + } + + // display how many items remain + System.out.println("(" + storage.size() + " left on the stack"); + } +} From 373ffd3d349c0029a393241ff76e0b7c527f4a47 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 02:24:58 -0800 Subject: [PATCH 31/52] Added helper fields to ResizingArrayStack - Created default constructor - Implemented size and isEmpty methods --- src/APIs/ResizingArrayStack.java | 26 ++++++++++++++++++++++++-- src/List/ArrayList.java | 4 ++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index cee4992..9c4f884 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -3,6 +3,28 @@ import java.util.Iterator; public class ResizingArrayStack implements Stack { + /** + * An array used to store items placed within the ResizingArrayStack + */ + private E[] buffer; + + /** + * The number of items stored within buffer + */ + private int size; + + /** + * Constructs a ResizingArrayStack with an empty buffer, + * and a default max capacity of 10 items + */ + public ResizingArrayStack() { + // setup buffer with default max capacity of 10 + buffer = (E[]) new Object[10]; + + // buffer starts off empty + size = 0; + } + /** * Add an item to the stack. * @@ -41,7 +63,7 @@ public E peek() { */ @Override public boolean isEmpty() { - return false; + return size == 0; } /** @@ -51,7 +73,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** diff --git a/src/List/ArrayList.java b/src/List/ArrayList.java index 1c34ac4..acb5b1f 100644 --- a/src/List/ArrayList.java +++ b/src/List/ArrayList.java @@ -5,7 +5,7 @@ public class ArrayList implements List { /** - * An array used to store items placed within the List.ArrayList + * An array used to store items placed within the ArrayList */ private E[] buffer; @@ -15,7 +15,7 @@ public class ArrayList implements List { private int size; /** - * Constructs an List.ArrayList with an empty buffer, and a default max capacity of 10 items + * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 items */ public ArrayList() { // setup buffer with default max capacity of 10 From 266ed4ff98605238395eb4c29b4dd3b075ea2982 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 02:58:40 -0800 Subject: [PATCH 32/52] Implemented ResizingArrayStack's push method - Added doubleMaxCapacity helper for when array is full --- src/APIs/ResizingArrayStack.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index 9c4f884..06ef7c2 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -25,6 +25,25 @@ public ResizingArrayStack() { size = 0; } + /** + * If all slots in buffer are full, double its max capacity + */ + private void doubleMaxCapacity() { + // if all buffer slots are filled + if(size == buffer.length) { + // create a new buffer, with double the capacity of the existing buffer + E[] newBuffer = (E[]) new Object[size * 2]; + + // run through previous buffer and copy over all items + for(int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + + // replace buffer with newBuffer, now with double the length + buffer = newBuffer; + } + } + /** * Add an item to the stack. * @@ -32,7 +51,14 @@ public ResizingArrayStack() { */ @Override public void push(E item) { + // if the buffer is full, double its max capacity + doubleMaxCapacity(); + + // add item to "end/top" of buffer + buffer[size] = item; + // account for new item being added + size++; } /** From 76d20c5c0bae532e2667ab06b373c020fe5ca291 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 03:07:29 -0800 Subject: [PATCH 33/52] Implemented ResizingArrayStack's pop method - Refactored previously added resizing helper --- src/APIs/ResizingArrayStack.java | 48 +++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index 06ef7c2..29234b1 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -26,22 +26,21 @@ public ResizingArrayStack() { } /** - * If all slots in buffer are full, double its max capacity + * If all slots in buffer are full, increase its max capacity by given number + * @param maxCapacity the new max capacity of buffer */ - private void doubleMaxCapacity() { - // if all buffer slots are filled - if(size == buffer.length) { - // create a new buffer, with double the capacity of the existing buffer - E[] newBuffer = (E[]) new Object[size * 2]; - - // run through previous buffer and copy over all items - for(int i = 0; i < buffer.length; i++) { - newBuffer[i] = buffer[i]; - } + private void resizeBuffer(int maxCapacity) { + // create a new buffer, with the given max capacity + E[] newBuffer = (E[]) new Object[maxCapacity]; - // replace buffer with newBuffer, now with double the length - buffer = newBuffer; + // run through current buffer and copy over all items + for(int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; } + + // override existing buffer with the newly created one, + // now with more max capacity + buffer = newBuffer; } /** @@ -52,9 +51,11 @@ private void doubleMaxCapacity() { @Override public void push(E item) { // if the buffer is full, double its max capacity - doubleMaxCapacity(); + if(size == buffer.length) { + resizeBuffer(buffer.length * 2); + } - // add item to "end/top" of buffer + // add given item to "end/top" of buffer buffer[size] = item; // account for new item being added @@ -68,7 +69,22 @@ public void push(E item) { */ @Override public E pop() { - return null; + // get the end/top item from stack + E requestedItem = buffer[size]; + + // clear that slot in buffer + buffer[size] = null; + + // account for item being removed + size--; + + // if the buffer is not empty, and running low on space + if(size > 0 && size == buffer.length / 4) { + // increase its size by half the current max + resizeBuffer(buffer.length / 2); + } + + return requestedItem; } /** From 856b525fa6dbb72ad334d451c0a597aeec0ff7ba Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 03:16:45 -0800 Subject: [PATCH 34/52] Implemented ResizingArrayStack's peek method --- src/APIs/ResizingArrayStack.java | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index 29234b1..636243a 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -1,6 +1,7 @@ package APIs; import java.util.Iterator; +import java.util.NoSuchElementException; public class ResizingArrayStack implements Stack { /** @@ -26,7 +27,7 @@ public ResizingArrayStack() { } /** - * If all slots in buffer are full, increase its max capacity by given number + * Resize the buffer by updating its max capacity by given number * @param maxCapacity the new max capacity of buffer */ private void resizeBuffer(int maxCapacity) { @@ -39,7 +40,7 @@ private void resizeBuffer(int maxCapacity) { } // override existing buffer with the newly created one, - // now with more max capacity + // now with the requested max capacity buffer = newBuffer; } @@ -69,18 +70,18 @@ public void push(E item) { */ @Override public E pop() { - // get the end/top item from stack - E requestedItem = buffer[size]; + // get the end/top item from stack, account for index + E requestedItem = buffer[size - 1]; // clear that slot in buffer - buffer[size] = null; + buffer[size - 1] = null; // account for item being removed size--; - // if the buffer is not empty, and running low on space + // if the buffer is not empty, and too large if(size > 0 && size == buffer.length / 4) { - // increase its size by half the current max + // reduce its current max capacity by half resizeBuffer(buffer.length / 2); } @@ -95,7 +96,13 @@ public E pop() { */ @Override public E peek() { - return null; + // if buffer contains no items, one cannot be retrieved + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty ResizingArrayStack"); + } + + // get and return end/top item in buffer + return buffer[size - 1]; } /** From 07feb50fc9b6ead9abb7718ac48cb020cc6f1629 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 03:17:27 -0800 Subject: [PATCH 35/52] Fixed file migration documentation wonkyness --- src/List/ArrayList.java | 10 +++++----- src/List/LinkedList.java | 22 +++++++++++----------- tests/ArrayListTest.java | 2 +- tests/LinkedListTest.java | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/List/ArrayList.java b/src/List/ArrayList.java index acb5b1f..08522b6 100644 --- a/src/List/ArrayList.java +++ b/src/List/ArrayList.java @@ -171,7 +171,7 @@ public void set(int index, E item) { public E removeFront() { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } // get requested item prior to removal @@ -198,7 +198,7 @@ public E removeFront() { public E removeBack() { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } // get requested item prior to removal, accounting for index @@ -222,7 +222,7 @@ public E removeBack() { public void remove(E item) { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } // find item in buffer and get its index @@ -230,7 +230,7 @@ public void remove(E item) { // if item is not in buffer if(index == -1) { - throw new NoSuchElementException("Given item is not located in List.ArrayList"); + throw new NoSuchElementException("Given item is not located in ArrayList"); } // otherwise, run through buffer, starting at given index @@ -278,7 +278,7 @@ public E remove(int index) { // if buffer contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.ArrayList"); + throw new NoSuchElementException("Cannot remove item from empty ArrayList"); } // get requested item prior to removal diff --git a/src/List/LinkedList.java b/src/List/LinkedList.java index c4fb6b3..e5b188d 100644 --- a/src/List/LinkedList.java +++ b/src/List/LinkedList.java @@ -32,17 +32,17 @@ public Node(E item) { } /** - * The first Node of the current List.LinkedList + * The first Node of the current LinkedList */ private Node head; /** - * The number of Nodes contained within this List.LinkedList + * The number of Nodes contained within this LinkedList */ private int size; /** - * Constructs an empty List.LinkedList + * Constructs an empty LinkedList */ public LinkedList() { // list starts off empty @@ -170,7 +170,7 @@ public E get(int index) { // if list is empty if(isEmpty()) { - throw new NoSuchElementException("Cannot retrieve item from empty List.LinkedList"); + throw new NoSuchElementException("Cannot retrieve item from empty LinkedList"); } // setup trackers @@ -234,7 +234,7 @@ public void set(int index, E item) { public E removeFront() { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); } // get requested item from head @@ -259,7 +259,7 @@ public E removeFront() { public E removeBack() { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); } // setup trackers and run through list till second to last node @@ -290,12 +290,12 @@ public E removeBack() { public void remove(E item) { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); } // check if list contains given item if(!contains(item)) { - throw new NoSuchElementException("Given item is not located in List.LinkedList"); + throw new NoSuchElementException("Given item is not located in LinkedList"); } // setup trackers and flag @@ -335,7 +335,7 @@ public E remove(int index) { // if list contains no items, one cannot be removed if(isEmpty()) { - throw new NoSuchElementException("Cannot remove item from empty List.LinkedList"); + throw new NoSuchElementException("Cannot remove item from empty LinkedList"); } // if given index was at front or back, use those methods @@ -436,7 +436,7 @@ public Iterator iterator() { } /** - * Implementation of an Iterator for the List.LinkedList class + * Implementation of an Iterator for the LinkedList class */ private class LinkedListIterator implements Iterator { /** @@ -445,7 +445,7 @@ private class LinkedListIterator implements Iterator { private Node current; /** - * Constructs a List.LinkedList iterator with the head tracked as the first Node + * Constructs a LinkedList iterator with the head tracked as the first Node */ LinkedListIterator() { current = head; diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index ec5fb9c..c9ba03b 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -8,7 +8,7 @@ class ArrayListTest { /** - * Create List.ArrayList at start for testing + * Create ArrayList at start for testing */ private final ArrayList testArrayList = new ArrayList(); diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index c136580..ddde826 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -7,7 +7,7 @@ class LinkedListTest { /** - * Create List.LinkedList at start for testing + * Create LinkedList at start for testing */ private final LinkedList testLinkedList = new LinkedList(); From 3e1350000867cf061e039897fdeb60999e244280 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 03:37:34 -0800 Subject: [PATCH 36/52] Implemented iterator for ResizingArrayStack --- src/APIs/ResizingArrayStack.java | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index 636243a..195fd74 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -132,6 +132,50 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new ReverseArrayStackIterator(); + } + + /** + * Implementation of an Iterator for the ReverseArrayStack class + */ + private class ReverseArrayStackIterator implements Iterator + { + /** + * The current number of items being tracked by iterator + */ + private int trackedSize; + + /** + * Constructs an ReverseArrayStackIterator iterator, with a tracker for buffer size + */ + ReverseArrayStackIterator() { + trackedSize = size; + } + + /** + * Checks if buffer has another element, and returns true/false accordingly + * @return true if buffer has another element; otherwise false + */ + public boolean hasNext() { + return trackedSize > 0; + } + + /** + * Gets and returns the next/top item in the buffer + * @return the next/top item in the buffer + */ + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get item at end/top of buffer + E currItem = buffer[trackedSize]; + + // move down to the next item + trackedSize--; + + return currItem; + } } } From 02e58a49ab1337308eb5280dcae84473ba7e38eb Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 04:33:00 -0800 Subject: [PATCH 37/52] Fixed and finalized ResizingArrayStack and tester --- src/APIs/ResizingArrayStack.java | 2 +- src/APIs/StackTestClient.java | 30 ++++++++++++++++-------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index 195fd74..e961584 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -35,7 +35,7 @@ private void resizeBuffer(int maxCapacity) { E[] newBuffer = (E[]) new Object[maxCapacity]; // run through current buffer and copy over all items - for(int i = 0; i < buffer.length; i++) { + for(int i = 0; i < size; i++) { newBuffer[i] = buffer[i]; } diff --git a/src/APIs/StackTestClient.java b/src/APIs/StackTestClient.java index b2a6af6..b865d68 100644 --- a/src/APIs/StackTestClient.java +++ b/src/APIs/StackTestClient.java @@ -5,29 +5,31 @@ public class StackTestClient { public static void main(String[] args) { // setup stack - Stack storage = new ResizingArrayStack(); + Stack storage = new ResizingArrayStack<>(); // setup scanner, taking in given string - Scanner in = new Scanner("to be or not to - be - - that - - - is"); + Scanner line = new Scanner("to be or not to - be - - that - - - is"); // run through scanner - while (in.hasNext()) { - // get current item (word) - String item = in.next(); + while (line.hasNext()) { + // get current item + String item = line.next(); - // if the current item is a dash - if (item.equals("-")) { - // add a value to stack - storage.push("item"); + // if the current item is not a dash + if (!item.equals("-")) { + // it is a word, add it to stack + storage.push(item); + System.out.println(); } - // if the stack is not empty + // if it is a dash, and the stack is not empty else if (!storage.isEmpty()) { - System.out.println("s.pop()" + " "); + // pop the topmost item off the stack, and display it + System.out.println(storage.pop()); } } - // display how many items remain - System.out.println("(" + storage.size() + " left on the stack"); + // display how many items remain at end of string + System.out.println("(" + storage.size() + " left on the stack)"); } -} +} \ No newline at end of file From dbeb94f3c887e5a78a6636664e64bca4d6982ad2 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 04:39:07 -0800 Subject: [PATCH 38/52] Created LinkedStack implementing Stack - Setup helper class Node and fields - Implemented size and isEmpty classes --- src/APIs/LinkedStack.java | 105 ++++++++++++++++++++++++++++++++++++++ src/List/LinkedList.java | 2 +- 2 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 src/APIs/LinkedStack.java diff --git a/src/APIs/LinkedStack.java b/src/APIs/LinkedStack.java new file mode 100644 index 0000000..6d10863 --- /dev/null +++ b/src/APIs/LinkedStack.java @@ -0,0 +1,105 @@ +package APIs; + +import List.LinkedList; + +import java.util.Iterator; + +public class LinkedStack implements Stack { + /** + * A container which contains an item and a connection to another Node + */ + private class Node { + /** + * The item stored within the Node + */ + E item; + + /** + * The node this Node is pointing to + */ + Node next; + + /** + * Creates a node, and stores the given item within it + * @param item the item being stored in node + */ + public Node(E item) { + // store given item + this.item = item; + + // node starts off disconnected + next = null; + } + } + + /** + * The top of the stack (most recently added node) + */ + private Node top; + + /** + * The number of items stored in this stack + */ + private int size; + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + return null; + } + + /** + * Returns the item at the top of the stack. + * Does not modify the stack or the item at the top. + * + * @return item at the top of the stack. + */ + @Override + public E peek() { + return null; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return top == null && size == 0; + } + + /** + * Returns a count of the number of items in the stack. + * + * @return the number of items in the stack + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} \ No newline at end of file diff --git a/src/List/LinkedList.java b/src/List/LinkedList.java index e5b188d..549a8d8 100644 --- a/src/List/LinkedList.java +++ b/src/List/LinkedList.java @@ -37,7 +37,7 @@ public Node(E item) { private Node head; /** - * The number of Nodes contained within this LinkedList + * The number of items stored in this LinkedList */ private int size; From d1852d8903535da864bcba5ed4faa1c7c840eb34 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 04:48:47 -0800 Subject: [PATCH 39/52] Implemented LinkedStack's push, pop, and peek methods --- src/APIs/LinkedStack.java | 40 ++++++++++++++++++++++++++++++-- src/APIs/ResizingArrayStack.java | 7 +++++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/APIs/LinkedStack.java b/src/APIs/LinkedStack.java index 6d10863..82d0239 100644 --- a/src/APIs/LinkedStack.java +++ b/src/APIs/LinkedStack.java @@ -3,6 +3,7 @@ import List.LinkedList; import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedStack implements Stack { /** @@ -49,7 +50,21 @@ public Node(E item) { */ @Override public void push(E item) { + // create new node containing given item + Node newNode = new Node(item); + // if the list is not empty + if(!isEmpty()) { + // point new node at top, so it isn't lost + newNode.next = top; + } + + // override top with newly created node + // (making new node first node in stack) + top = newNode; + + // account for new item in stack + size++; } /** @@ -59,7 +74,22 @@ public void push(E item) { */ @Override public E pop() { - return null; + // if stack contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty Stack"); + } + + // get requested item from topmost node + E requestedItem = top.item; + + // overwrite top with node "under it", + // if no more remain, top will become null, making stack empty + top = top.next; + + // account for removal of item from stack + size--; + + return requestedItem; } /** @@ -70,7 +100,13 @@ public E pop() { */ @Override public E peek() { - return null; + // if buffer contains no items, one cannot be retrieved + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty Stack"); + } + + // get and return item stored in topmost node + return top.item; } /** diff --git a/src/APIs/ResizingArrayStack.java b/src/APIs/ResizingArrayStack.java index e961584..abd4b57 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/APIs/ResizingArrayStack.java @@ -70,6 +70,11 @@ public void push(E item) { */ @Override public E pop() { + // if buffer contains no items, one cannot be retrieved + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty Stack"); + } + // get the end/top item from stack, account for index E requestedItem = buffer[size - 1]; @@ -98,7 +103,7 @@ public E pop() { public E peek() { // if buffer contains no items, one cannot be retrieved if(isEmpty()) { - throw new NoSuchElementException("Cannot retrieve item from empty ResizingArrayStack"); + throw new NoSuchElementException("Cannot retrieve item from empty Stack"); } // get and return end/top item in buffer From 74b841c721bff786dfcbf7748d7200b9b6d74416 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 04:54:01 -0800 Subject: [PATCH 40/52] Implemented iterator for LinkedStack - Updated tester to use LinkedStack --- src/APIs/LinkedStack.java | 48 ++++++++++++++++++++++++++++++++--- src/APIs/StackTestClient.java | 3 ++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/APIs/LinkedStack.java b/src/APIs/LinkedStack.java index 82d0239..ea7dda5 100644 --- a/src/APIs/LinkedStack.java +++ b/src/APIs/LinkedStack.java @@ -1,7 +1,5 @@ package APIs; -import List.LinkedList; - import java.util.Iterator; import java.util.NoSuchElementException; @@ -136,6 +134,50 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new LinkedStackIterator(); + } + + /** + * Implementation of an Iterator for the LinkedStack class + */ + private class LinkedStackIterator implements Iterator + { + /** + * The current Node being tracked by the Iterator + */ + private Node current; + + /** + * Constructs an LinkedStackIterator iterator, with the topmost node tracked first + */ + LinkedStackIterator() { + current = top; + } + + /** + * Checks if list contains another element, and returns true/false accordingly + * @return true if list contains another element; otherwise false + */ + public boolean hasNext() { + return current != null; + } + + /** + * Gets and returns the next/top item in the list + * @return the next/top item in the list + */ + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get item at end/top of buffer + E currItem = current.item; + + // move down to the next item + current = current.next; + + return currItem; + } } } \ No newline at end of file diff --git a/src/APIs/StackTestClient.java b/src/APIs/StackTestClient.java index b865d68..2a53b14 100644 --- a/src/APIs/StackTestClient.java +++ b/src/APIs/StackTestClient.java @@ -5,7 +5,8 @@ public class StackTestClient { public static void main(String[] args) { // setup stack - Stack storage = new ResizingArrayStack<>(); + //Stack storage = new ResizingArrayStack<>(); + Stack storage = new LinkedStack<>(); // setup scanner, taking in given string Scanner line = new Scanner("to be or not to - be - - that - - - is"); From e01bac7e83f3bb941f4ffd4d78eb9a0e20303abd Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 04:56:31 -0800 Subject: [PATCH 41/52] Placed all stack related files in package --- src/{APIs => Stack}/LinkedStack.java | 2 +- src/{APIs => Stack}/ResizingArrayStack.java | 2 +- src/{APIs => Stack}/Stack.java | 2 +- src/{APIs => Stack}/StackTestClient.java | 6 +++--- 4 files changed, 6 insertions(+), 6 deletions(-) rename src/{APIs => Stack}/LinkedStack.java (99%) rename src/{APIs => Stack}/ResizingArrayStack.java (99%) rename src/{APIs => Stack}/Stack.java (98%) rename src/{APIs => Stack}/StackTestClient.java (87%) diff --git a/src/APIs/LinkedStack.java b/src/Stack/LinkedStack.java similarity index 99% rename from src/APIs/LinkedStack.java rename to src/Stack/LinkedStack.java index ea7dda5..58076e9 100644 --- a/src/APIs/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -1,4 +1,4 @@ -package APIs; +package Stack; import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/src/APIs/ResizingArrayStack.java b/src/Stack/ResizingArrayStack.java similarity index 99% rename from src/APIs/ResizingArrayStack.java rename to src/Stack/ResizingArrayStack.java index abd4b57..82b947d 100644 --- a/src/APIs/ResizingArrayStack.java +++ b/src/Stack/ResizingArrayStack.java @@ -1,4 +1,4 @@ -package APIs; +package Stack; import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/src/APIs/Stack.java b/src/Stack/Stack.java similarity index 98% rename from src/APIs/Stack.java rename to src/Stack/Stack.java index 1cfe014..7d59c03 100644 --- a/src/APIs/Stack.java +++ b/src/Stack/Stack.java @@ -1,4 +1,4 @@ -package APIs; +package Stack; /** * APIs.Stack (LIFO: last-in, first-out) API diff --git a/src/APIs/StackTestClient.java b/src/Stack/StackTestClient.java similarity index 87% rename from src/APIs/StackTestClient.java rename to src/Stack/StackTestClient.java index 2a53b14..c1c4183 100644 --- a/src/APIs/StackTestClient.java +++ b/src/Stack/StackTestClient.java @@ -1,12 +1,12 @@ -package APIs; +package Stack; import java.util.Scanner; public class StackTestClient { public static void main(String[] args) { // setup stack - //Stack storage = new ResizingArrayStack<>(); - Stack storage = new LinkedStack<>(); + //Stack storage = new ResizingArrayStack(); + Stack storage = new LinkedStack(); // setup scanner, taking in given string Scanner line = new Scanner("to be or not to - be - - that - - - is"); From e5579aafa9d25b6a0feaf333e73f2cc63bbe0122 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 05:11:08 -0800 Subject: [PATCH 42/52] Created LinkedQueue implementing Queue - Setup helpers and fields - Created default constructor for LinkedQueue and LinkedStack --- src/APIs/LinkedQueue.java | 109 +++++++++++++++++++++++++++++++++++++ src/Stack/LinkedStack.java | 9 +++ 2 files changed, 118 insertions(+) create mode 100644 src/APIs/LinkedQueue.java diff --git a/src/APIs/LinkedQueue.java b/src/APIs/LinkedQueue.java new file mode 100644 index 0000000..a0d0cd4 --- /dev/null +++ b/src/APIs/LinkedQueue.java @@ -0,0 +1,109 @@ +package APIs; + +import Stack.LinkedStack; + +import java.util.Iterator; + +public class LinkedQueue implements Queue { + /** + * A container which contains an item and a connection to another Node + */ + private class Node { + /** + * The item stored within the Node + */ + E item; + + /** + * The node this Node is pointing to + */ + Node next; + + /** + * Creates a node, and stores the given item within it + * @param item the item being stored in node + */ + public Node(E item) { + // store given item + this.item = item; + + // node starts off disconnected + next = null; + } + } + + /** + * The front of the queue (least recently added node) + */ + private Node front; + + /** + * The back of the queue (most recently added node) + */ + private Node back; + + /** + * The number of items stored in this stack + */ + private int size; + + /** + * Constructs an empty LinkedStack + */ + public LinkedQueue() { + // queue starts off empty + front = null; + back = null; + size = 0; + } + + /** + * Add an item to the queue. + * + * @param item the item to be added + */ + @Override + public void enqueue(E item) { + + } + + /** + * Remove an item from the queue. + * + * @return the item that was removed + */ + @Override + public E dequeue() { + return null; + } + + /** + * Checks to see if the queue is empty. + * + * @return true if the queue is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Returns a count of the number of items in the queue. + * + * @return the number of items in the queue + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} \ No newline at end of file diff --git a/src/Stack/LinkedStack.java b/src/Stack/LinkedStack.java index 58076e9..2f61f3c 100644 --- a/src/Stack/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -41,6 +41,15 @@ public Node(E item) { */ private int size; + /** + * Constructs an empty LinkedStack + */ + public LinkedStack() { + // stack starts off empty + top = null; + size = 0; + } + /** * Add an item to the stack. * From f660037c6216965ed6e8d1627fdfac561251ef8b Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 05:22:21 -0800 Subject: [PATCH 43/52] Implemented LinkedQueue's enqueue, dequeue, isEmpty, and size methods --- src/APIs/LinkedQueue.java | 44 +++++++++++++++++++++++++++++++++++--- src/Stack/LinkedStack.java | 2 +- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/APIs/LinkedQueue.java b/src/APIs/LinkedQueue.java index a0d0cd4..4dac911 100644 --- a/src/APIs/LinkedQueue.java +++ b/src/APIs/LinkedQueue.java @@ -3,6 +3,7 @@ import Stack.LinkedStack; import java.util.Iterator; +import java.util.NoSuchElementException; public class LinkedQueue implements Queue { /** @@ -64,7 +65,23 @@ public LinkedQueue() { */ @Override public void enqueue(E item) { + // create new node containing given item + Node newNode = new Node(item); + // if the queue is empty + if(isEmpty()) { + // make new node front of queue + front = newNode; + } + + else { + // if queue is not empty, + // add new node to the back of queue + front.next = newNode; + } + + // account for new item in queue + size++; } /** @@ -74,7 +91,28 @@ public void enqueue(E item) { */ @Override public E dequeue() { - return null; + // if queue contains no items, one cannot be removed + if(isEmpty()) { + throw new NoSuchElementException("Cannot retrieve item from empty Queue"); + } + + // get requested item from node at front + E requestedItem = front.item; + + // overwrite current node at front with next in line + // if no more remain, front will become null + front = front.next; + + // in the event that queue is now empty and front is null, + // overwrite last item + if(isEmpty()) { + back = null; + } + + // account for removal of item from stack + size--; + + return requestedItem; } /** @@ -84,7 +122,7 @@ public E dequeue() { */ @Override public boolean isEmpty() { - return false; + return size == 0 && front == null; } /** @@ -94,7 +132,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** diff --git a/src/Stack/LinkedStack.java b/src/Stack/LinkedStack.java index 2f61f3c..1d7351d 100644 --- a/src/Stack/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -60,7 +60,7 @@ public void push(E item) { // create new node containing given item Node newNode = new Node(item); - // if the list is not empty + // if the stack is not empty if(!isEmpty()) { // point new node at top, so it isn't lost newNode.next = top; From 8927d5afefd090aea0c669714cfa2433c9b9987f Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Thu, 15 Feb 2024 05:26:56 -0800 Subject: [PATCH 44/52] Implemented iterator for LinkedQueue --- src/APIs/LinkedQueue.java | 46 +++++++++++++++++++++++++++++++++++++- src/Stack/LinkedStack.java | 4 ++-- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/APIs/LinkedQueue.java b/src/APIs/LinkedQueue.java index 4dac911..923ba36 100644 --- a/src/APIs/LinkedQueue.java +++ b/src/APIs/LinkedQueue.java @@ -142,6 +142,50 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new LinkedQueueIterator(); + } + + /** + * Implementation of an Iterator for the LinkedQueue class + */ + private class LinkedQueueIterator implements Iterator + { + /** + * The current Node being tracked by the Iterator + */ + private Node current; + + /** + * Constructs a LinkedQueue iterator, with the front node tracked first + */ + LinkedQueueIterator() { + current = front; + } + + /** + * Checks if list contains another element, and returns true/false accordingly + * @return true if list contains another element; otherwise false + */ + public boolean hasNext() { + return current != null; + } + + /** + * Gets and returns the start/front item in the list + * @return the start/front item in the list + */ + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get item at start/front of list + E currItem = current.item; + + // bring the next item over + current = current.next; + + return currItem; + } } } \ No newline at end of file diff --git a/src/Stack/LinkedStack.java b/src/Stack/LinkedStack.java index 1d7351d..99c1b5e 100644 --- a/src/Stack/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -157,7 +157,7 @@ private class LinkedStackIterator implements Iterator private Node current; /** - * Constructs an LinkedStackIterator iterator, with the topmost node tracked first + * Constructs a LinkedStack iterator, with the topmost node tracked first */ LinkedStackIterator() { current = top; @@ -180,7 +180,7 @@ public E next() { throw new NoSuchElementException(); } - // get item at end/top of buffer + // get item at end/top of list E currItem = current.item; // move down to the next item From 09affd6df2a9caea5963a7f2c4bb20ddb9a55c48 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sat, 17 Feb 2024 19:23:02 -0800 Subject: [PATCH 45/52] Setup tester for LinkedQueue - Fixed enqueue method using tester reaction --- src/APIs/LinkedQueue.java | 17 ++++++++++------- src/APIs/QueueTestClient.java | 35 ++++++++++++++++++++++++++++++++++ src/Stack/StackTestClient.java | 6 +++--- 3 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 src/APIs/QueueTestClient.java diff --git a/src/APIs/LinkedQueue.java b/src/APIs/LinkedQueue.java index 923ba36..dedc6eb 100644 --- a/src/APIs/LinkedQueue.java +++ b/src/APIs/LinkedQueue.java @@ -65,19 +65,22 @@ public LinkedQueue() { */ @Override public void enqueue(E item) { - // create new node containing given item - Node newNode = new Node(item); + // track the item currently at back of queue + Node currBack = back; + + // replace back node with new node containing given item + back = new Node(item); // if the queue is empty if(isEmpty()) { - // make new node front of queue - front = newNode; + // move back node to front + front = back; } + // if queue is not empty else { - // if queue is not empty, - // add new node to the back of queue - front.next = newNode; + // add given item to end of queue + currBack.next = back; } // account for new item in queue diff --git a/src/APIs/QueueTestClient.java b/src/APIs/QueueTestClient.java new file mode 100644 index 0000000..0a9950e --- /dev/null +++ b/src/APIs/QueueTestClient.java @@ -0,0 +1,35 @@ +package APIs; + +import java.util.Scanner; + +public class QueueTestClient { + public static void main(String[] args) { + // setup storage queue + Queue storage = new LinkedQueue<>(); + + // setup scanner, taking in given string + Scanner line = new Scanner("to be or not to - be - - that - - - is"); + + // run through line + while (line.hasNext()) { + // get current item + String item = line.next(); + + // if the current item is not a dash + if (!item.equals("-")) { + // it is a word, add it to queue + storage.enqueue(item); + System.out.println(); + } + + // if it is a dash, and the stack is not empty + else if (!storage.isEmpty()) { + // get the item at front of queue, and display it + System.out.println(storage.dequeue()); + } + } + + // display how many items remain in queue at end of line + System.out.println("(" + storage.size() + " left in the queue)"); + } +} diff --git a/src/Stack/StackTestClient.java b/src/Stack/StackTestClient.java index c1c4183..7cd74f1 100644 --- a/src/Stack/StackTestClient.java +++ b/src/Stack/StackTestClient.java @@ -4,14 +4,14 @@ public class StackTestClient { public static void main(String[] args) { - // setup stack + // setup storage stack //Stack storage = new ResizingArrayStack(); Stack storage = new LinkedStack(); // setup scanner, taking in given string Scanner line = new Scanner("to be or not to - be - - that - - - is"); - // run through scanner + // run through line while (line.hasNext()) { // get current item String item = line.next(); @@ -30,7 +30,7 @@ else if (!storage.isEmpty()) { } } - // display how many items remain at end of string + // display how many items remain in stack at end of line System.out.println("(" + storage.size() + " left on the stack)"); } } \ No newline at end of file From 8be52e084e088a7d21f762b29159d427af84cfe3 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sat, 17 Feb 2024 20:19:03 -0800 Subject: [PATCH 46/52] Created LinkedBag implementing Bag and added helpers and constructor --- src/APIs/LinkedBag.java | 97 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/APIs/LinkedBag.java diff --git a/src/APIs/LinkedBag.java b/src/APIs/LinkedBag.java new file mode 100644 index 0000000..aecf941 --- /dev/null +++ b/src/APIs/LinkedBag.java @@ -0,0 +1,97 @@ +package APIs; + +import Stack.LinkedStack; + +import java.util.Iterator; + +/** + * + * @param + */ +public class LinkedBag implements Bag { + /** + * A container which contains an item and a connection to another Node + */ + private class Node { + /** + * The item stored within the Node + */ + E item; + + /** + * The node this Node is pointing to + */ + Node next; + + /** + * Creates a node, and stores the given item within it + * @param item the item being stored in node + */ + public Node(E item) { + // store given item + this.item = item; + + // node starts off disconnected + next = null; + } + } + + /** + * The first node in nag + */ + private Node first; + + /** + * The number of items stored in this bag + */ + private int size; + + /** + * Constructs an empty LinkedBag + */ + public LinkedBag() { + // bag starts off empty + first = null; + size = 0; + } + + /** + * Adds the given item to the bag + * + * @param item the item to be added + */ + @Override + public void add(E item) { + + } + + /** + * Checks whether the bag is empty, and returns true/false accordingly + * + * @return true if the bag is empty; otherwise false + */ + @Override + public boolean isEmpty() { + return false; + } + + /** + * Gets and returns the number of items stored in the bag + * + * @return the number of items stored in the bag + */ + @Override + public int size() { + return 0; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } +} From 3c8237ed852616c41018bf39ecd7431886a50a98 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 00:15:37 -0800 Subject: [PATCH 47/52] Implemented all methods + iterator for LinkedBag implementation --- src/APIs/LinkedBag.java | 65 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) diff --git a/src/APIs/LinkedBag.java b/src/APIs/LinkedBag.java index aecf941..10b0101 100644 --- a/src/APIs/LinkedBag.java +++ b/src/APIs/LinkedBag.java @@ -3,6 +3,7 @@ import Stack.LinkedStack; import java.util.Iterator; +import java.util.NoSuchElementException; /** * @@ -62,7 +63,21 @@ public LinkedBag() { */ @Override public void add(E item) { + // create new node containing given item + Node newNode = new Node(item); + // if the bag is not empty + if(!isEmpty()) { + // point new node at first, so it isn't lost + newNode.next = first; + } + + // override first with newly created node + // (making new node first node in bag) + first = newNode; + + // account for new item in bag + size++; } /** @@ -72,7 +87,7 @@ public void add(E item) { */ @Override public boolean isEmpty() { - return false; + return size == 0 && first == null; } /** @@ -82,7 +97,7 @@ public boolean isEmpty() { */ @Override public int size() { - return 0; + return size; } /** @@ -92,6 +107,50 @@ public int size() { */ @Override public Iterator iterator() { - return null; + return new LinkedBagIterator(); + } + + /** + * Implementation of an Iterator for the LinkedBag class + */ + private class LinkedBagIterator implements Iterator + { + /** + * The current Node being tracked by the Iterator + */ + private Node current; + + /** + * Constructs a LinkedBag iterator, with the first node tracked first + */ + LinkedBagIterator() { + current = first; + } + + /** + * Checks if list contains another element, and returns true/false accordingly + * @return true if list contains another element; otherwise false + */ + public boolean hasNext() { + return current != null; + } + + /** + * Gets and returns the first item in the list + * @return the first item in the list + */ + public E next() { + if(!hasNext()) { + throw new NoSuchElementException(); + } + + // get first item of list + E currItem = current.item; + + // move on to the next item + current = current.next; + + return currItem; + } } } From 6e6010799abc4500e7ea55987d52e71431efeda0 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 00:29:32 -0800 Subject: [PATCH 48/52] Implemented tester for LinkedBag --- src/APIs/LinkedBag.java | 2 +- src/APIs/Stats.java | 44 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/APIs/Stats.java diff --git a/src/APIs/LinkedBag.java b/src/APIs/LinkedBag.java index 10b0101..27abee8 100644 --- a/src/APIs/LinkedBag.java +++ b/src/APIs/LinkedBag.java @@ -38,7 +38,7 @@ public Node(E item) { } /** - * The first node in nag + * The first node in bag */ private Node first; diff --git a/src/APIs/Stats.java b/src/APIs/Stats.java new file mode 100644 index 0000000..168d430 --- /dev/null +++ b/src/APIs/Stats.java @@ -0,0 +1,44 @@ +package APIs; + +import java.util.Scanner; + +public class Stats +{ + public static void main(String[] args) { + // setup storage bag + Bag numbers = new LinkedBag<>(); + + // setup scanner, taking in given string + Scanner line = new Scanner("100 99 101 120 98 107 109 81 101 90"); + + // run through line + while (line.hasNextDouble()) { + // get current item and add to bag + numbers.add(line.nextDouble()); + } + + // track number of items in bag + int size = numbers.size(); + + // run through bag and add up all items + double sum = 0.0; + for (double currNum : numbers) { + sum += currNum; + } + + // get the mean of all numbers in bag + double mean = sum / size; + + // clear total tracker and use again to calculate standard dev + sum = 0.0; + for (double currNum : numbers) { + sum += (currNum - mean) * (currNum - mean); + } + + double std = Math.sqrt(sum / (size - 1)); + + // display calculation results + System.out.printf("Mean: %.2f\n", mean); + System.out.printf("Std dev: %.2f\n", std); + } +} \ No newline at end of file From d0a581fb1fbddc92ebd77a516d78ca4de746a742 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 01:41:40 -0800 Subject: [PATCH 49/52] Conducted runtime analysis of ArrayList and LinkedList implementations --- src/List/ArrayList.java | 51 ++++++++++++++++++++++++++++++++++++++-- src/List/LinkedList.java | 49 +++++++++++++++++++++++++++++++++++--- 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/List/ArrayList.java b/src/List/ArrayList.java index 08522b6..a83d66b 100644 --- a/src/List/ArrayList.java +++ b/src/List/ArrayList.java @@ -3,6 +3,11 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of an ArrayList using the List interface for generics + * @param Class may store various types of values + * @author Zalman I. + */ public class ArrayList implements List { /** * An array used to store items placed within the ArrayList @@ -16,6 +21,8 @@ public class ArrayList implements List { /** * Constructs an ArrayList with an empty buffer, and a default max capacity of 10 items + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public ArrayList() { // setup buffer with default max capacity of 10 @@ -27,6 +34,9 @@ public ArrayList() { /** * If all slots in buffer are full, double its max capacity + * + * Runtime: O(n) as it has to run through the buffer to copy over all items into a new array, + * therefore the run time depends on the prior buffer's length */ private void doubleMaxCapacity() { // if all buffer slots are filled @@ -47,6 +57,10 @@ private void doubleMaxCapacity() { /** * Add item to the front. * + * Runtime: O(1) in the case that buffer is empty, as only storage at index 0 occurs. + * Otherwise O(n) as the runtime depends on the number of items already in the list. If we need to resize, + * the buffer gets run through to copy over items. If not, we are still shifting over all existing items to the right. + * * @param item the item to be added */ @Override @@ -54,7 +68,7 @@ public void addFront(E item) { // if the buffer already contains items if(!isEmpty()) { // if the buffer is full, increase max capacity - doubleMaxCapacity(); + doubleMaxCapacity(); // O(n) // run through buffer backwards for (int i = size; i > 0; i--) { @@ -74,6 +88,9 @@ public void addFront(E item) { /** * Add item to the back. * + * Runtime: O(1) if array is empty or not full, as we are only accessing the final index to add an item. + * If the array is full O(n) as we need to run through buffer to copy over all items during resize. + * * @param item the item to be added */ @Override @@ -91,6 +108,9 @@ public void addBack(E item) { /** * Add an item at specified index (position). * + * Runtime: O(1) if the array is empty. Otherwise, O(n) as runtime will depend on array size, + * either for resizing or shifting over items to free up given index + * * @param index the index where the item should be added * @param item the item to be added */ @@ -121,6 +141,9 @@ public void add(int index, E item) { /** * Get the item at a specified index. * + * Runtime: O(1) as we can instantly access the given index to retrieve item, + * assuming index exists/buffer not empty. + * * @param index the index where the item should be retrieved * @return the item located at that index */ @@ -143,6 +166,9 @@ public E get(int index) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * + * Runtime: O(1) as we can instantly access the given index to replace its item, + * assuming index exists/buffer not empty. + * * @param index the index where the item should be saved * @param item the item to be saved */ @@ -165,6 +191,8 @@ public void set(int index, E item) { /** * Remove item at the front of the list. * + * Runtime: O(n) as we are shifting over all items in buffer left to "remove" the item at index 0. + * * @return the item that was removed */ @Override @@ -192,6 +220,8 @@ public E removeFront() { /** * Remove item at the back of the list * + * Runtime: O(1) as we can instantly access the final index and set its value to null. + * * @return the item that was removed */ @Override @@ -216,6 +246,9 @@ public E removeBack() { /** * Remove item from the list * + * Runtime: O(n) as searching for the given item via indexOf results in running through buffer once. + * If searching reveals it exists, we then also have to shift over all items after it left to remove it. + * * @param item the item to be removed */ @Override @@ -226,7 +259,7 @@ public void remove(E item) { } // find item in buffer and get its index - int index = indexOf(item); + int index = indexOf(item); // O(n) // if item is not in buffer if(index == -1) { @@ -246,6 +279,9 @@ public void remove(E item) { /** * Gets and returns the index of the given item in buffer, if exists + * + * Runtime: O(n) as we could potentially run through the entire buffer when searching for the given item. + * * @param item the item being searched for in buffer * @return the index of given item in buffer if exists; otherwise -1 */ @@ -266,6 +302,8 @@ private int indexOf(E item) { /** * Remove item at a specified index. * + * Runtime: O(n) as runtime will depend on the number of items after given index which we need to shift left. + * * @param index the index where the item should be removed * @return the item that was removed */ @@ -300,6 +338,9 @@ public E remove(int index) { /** * Checks if an item is in the list. * + * Runtime O(n) as searching for the item requires running through buffer, and depends on its location + * within/number of items before and after it. + * * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -321,6 +362,9 @@ public boolean contains(E item) { /** * Checks if the list is empty. * + * Runtime: O(1) as we are instantly accessing a variable and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the list is empty, false otherwise */ @Override @@ -331,6 +375,9 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return number of items in the list */ @Override diff --git a/src/List/LinkedList.java b/src/List/LinkedList.java index 549a8d8..d828a26 100644 --- a/src/List/LinkedList.java +++ b/src/List/LinkedList.java @@ -3,6 +3,11 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a LinkedList using the List interface for generics + * @param Class may store various types of values + * @author Zalman I. + */ public class LinkedList implements List { /** * A container which contains an item and a connection to another Node @@ -20,6 +25,9 @@ private class Node { /** * Creates a node, and stores the given item within it + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation + * * @param item the item being stored in node */ public Node(E item) { @@ -43,6 +51,8 @@ public Node(E item) { /** * Constructs an empty LinkedList + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public LinkedList() { // list starts off empty @@ -53,6 +63,9 @@ public LinkedList() { /** * Add item to the front. * + * Runtime: O(1) as we can quickly access the front of list via the head variable, + * regardless of how many nodes follow it. + * * @param item the item to be added */ @Override @@ -77,6 +90,9 @@ public void addFront(E item) { /** * Add item to the back. * + * Runtime: O(1) if the list is empty. Otherwise, O(n), as runtime depends on the number of nodes already + * in list we need to pass to reach the end. + * * @param item the item to be added */ @Override @@ -110,6 +126,9 @@ public void addBack(E item) { /** * Add an item at specified index (position). * + * Runtime: O(1) if the given index is 0 as we use the addFront method. + * Otherwise O(n) as we may potentially have to traverse all the way to middle or end of list. + * * @param index the index where the item should be added * @param item the item to be added */ @@ -122,11 +141,11 @@ public void add(int index, E item) { // if given index was at front or back, use those methods else if(index == 0) { - addFront(item); + addFront(item); // O(1) } else if(index == size) { - addBack(item); + addBack(item); // O(n) } else { @@ -158,6 +177,8 @@ else if(index == size) { /** * Get the item at a specified index. * + * Runtime: O(n) as runtime will depend on how many nodes we need to pass in list to reach the requested index. + * * @param index the index where the item should be retrieved * @return the item located at that index */ @@ -192,6 +213,9 @@ public E get(int index) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * + * Runtime: O(1) if the list is empty as we use the addFront method. + * Otherwise, O(n) as runtime will depend on how many nodes we need to pass in list to reach the requested index. + * * @param index the index where the item should be saved * @param item the item to be saved */ @@ -228,6 +252,9 @@ public void set(int index, E item) { /** * Remove item at the front of the list. * + * Runtime: O(1) as we can quickly access the front of list via the head variable, + * regardless of how many nodes follow it. + * * @return the item that was removed */ @Override @@ -253,6 +280,8 @@ public E removeFront() { /** * Remove item at the back of the list * + * Runtime: O(n), as runtime depends on the number of nodes already in list we need to pass to reach the end. + * * @return the item that was removed */ @Override @@ -284,6 +313,9 @@ public E removeBack() { /** * Remove item from the list * + * Runtime: O(n) as we use the contains method to check if item exists, which may require running through entire list. + * If it does exist, we then use the remove method, which in the worse case also runs at O(n). + * * @param item the item to be removed */ @Override @@ -293,7 +325,7 @@ public void remove(E item) { throw new NoSuchElementException("Cannot remove item from empty LinkedList"); } - // check if list contains given item + // check if list contains given item - O(n) if(!contains(item)) { throw new NoSuchElementException("Given item is not located in LinkedList"); } @@ -323,6 +355,9 @@ public void remove(E item) { /** * Remove item at a specified index. * + * Runtime: O(1) if the given index is 0 as we use the addFront method. + * Otherwise, O(n) as we may potentially have to traverse all the way to middle or end of list to unlink a node. + * * @param index the index where the item should be removed * @return the item that was removed */ @@ -378,6 +413,8 @@ else if(index == size) { /** * Checks if an item is in the list. * + * Runtime: O(n) as searching for the item may require running through most of, or the entire list + * * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -408,6 +445,9 @@ public boolean contains(E item) { /** * Checks if the list is empty. * + * Runtime: O(1) as we are instantly accessing some variables and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the list is empty, false otherwise */ @Override @@ -418,6 +458,9 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return number of items in the list */ @Override From dd770c21465edcce6d821baf6371a9fd44582d74 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 01:59:57 -0800 Subject: [PATCH 50/52] Conducted runtime analysis of ResizingArrayStack and LinkedStack --- src/List/ArrayList.java | 4 ++-- src/Stack/LinkedStack.java | 24 ++++++++++++++++++++++++ src/Stack/ResizingArrayStack.java | 27 ++++++++++++++++++++++++++- 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/src/List/ArrayList.java b/src/List/ArrayList.java index a83d66b..0f9db11 100644 --- a/src/List/ArrayList.java +++ b/src/List/ArrayList.java @@ -36,7 +36,7 @@ public ArrayList() { * If all slots in buffer are full, double its max capacity * * Runtime: O(n) as it has to run through the buffer to copy over all items into a new array, - * therefore the run time depends on the prior buffer's length + * therefore the runtime depends on the prior buffer's length */ private void doubleMaxCapacity() { // if all buffer slots are filled @@ -88,7 +88,7 @@ public void addFront(E item) { /** * Add item to the back. * - * Runtime: O(1) if array is empty or not full, as we are only accessing the final index to add an item. + * Runtime: O(1) if buffer is empty/not full, as we are only accessing the final index to add an item. * If the array is full O(n) as we need to run through buffer to copy over all items during resize. * * @param item the item to be added diff --git a/src/Stack/LinkedStack.java b/src/Stack/LinkedStack.java index 99c1b5e..b618cf0 100644 --- a/src/Stack/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -3,6 +3,11 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a Stack using a "list" of nodes and a Stack interface for generics + * @param Class may store various types of values + * @author Zalman I. + */ public class LinkedStack implements Stack { /** * A container which contains an item and a connection to another Node @@ -20,6 +25,9 @@ private class Node { /** * Creates a node, and stores the given item within it + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation + * * @param item the item being stored in node */ public Node(E item) { @@ -43,6 +51,8 @@ public Node(E item) { /** * Constructs an empty LinkedStack + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public LinkedStack() { // stack starts off empty @@ -53,6 +63,9 @@ public LinkedStack() { /** * Add an item to the stack. * + * Runtime: O(1) as we can instantly add a new node to front/top via the top variable, + * regardless of how many nodes exist in stack. + * * @param item the item to be added */ @Override @@ -77,6 +90,9 @@ public void push(E item) { /** * Removes the most recently added item from the stack. * + * Runtime: O(1) as we can instantly access the top variable to retrieve item + * and overwrite it with the next node in stack. + * * @return the item that was removed */ @Override @@ -103,6 +119,8 @@ public E pop() { * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. * + * Runtime: O(1) as we can instantly access the top variable to retrieve item. + * * @return item at the top of the stack. */ @Override @@ -119,6 +137,9 @@ public E peek() { /** * Checks to see if the stack is empty. * + * Runtime: O(1) as we are instantly accessing a variable and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the stack is empty, false otherwise */ @Override @@ -129,6 +150,9 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the stack. * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return the number of items in the stack */ @Override diff --git a/src/Stack/ResizingArrayStack.java b/src/Stack/ResizingArrayStack.java index 82b947d..9f824a8 100644 --- a/src/Stack/ResizingArrayStack.java +++ b/src/Stack/ResizingArrayStack.java @@ -3,9 +3,14 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a Stack using an array and a Stack interface for generics + * @param Class may store various types of values + * @author Zalman I. + */ public class ResizingArrayStack implements Stack { /** - * An array used to store items placed within the ResizingArrayStack + * An array used to store items placed within the stack */ private E[] buffer; @@ -17,6 +22,8 @@ public class ResizingArrayStack implements Stack { /** * Constructs a ResizingArrayStack with an empty buffer, * and a default max capacity of 10 items + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public ResizingArrayStack() { // setup buffer with default max capacity of 10 @@ -28,6 +35,10 @@ public ResizingArrayStack() { /** * Resize the buffer by updating its max capacity by given number + * + * Runtime: O(n) as it has to run through the buffer to copy over all items into a new array, + * therefore the runtime depends on the prior buffer's length + * * @param maxCapacity the new max capacity of buffer */ private void resizeBuffer(int maxCapacity) { @@ -47,6 +58,9 @@ private void resizeBuffer(int maxCapacity) { /** * Add an item to the stack. * + * Runtime: O(1) if buffer is empty/not full, as we are only accessing the final index to add an item. + * If the array is full O(n) as we need to run through buffer to copy over all items during resize. + * * @param item the item to be added */ @Override @@ -66,6 +80,9 @@ public void push(E item) { /** * Removes the most recently added item from the stack. * + * Runtime: O(1) if buffer is not too large, as we are only accessing the final index to "pop off" an item. + * Otherwise, O(n) as we need to run through buffer to copy over all items during resize. + * * @return the item that was removed */ @Override @@ -97,6 +114,8 @@ public E pop() { * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. * + * Runtime: O(1) as we can instantly access the final index to retrieve item. + * * @return item at the top of the stack. */ @Override @@ -113,6 +132,9 @@ public E peek() { /** * Checks to see if the stack is empty. * + * Runtime: O(1) as we are instantly accessing a variable and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the stack is empty, false otherwise */ @Override @@ -123,6 +145,9 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the stack. * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return the number of items in the stack */ @Override From d7ce59e58e8d1914ed3feac374cc382f55f5c212 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 02:04:50 -0800 Subject: [PATCH 51/52] Organized project layout by grouping interfaces w/ their implementations and corresponding testers --- src/{APIs => Bag}/Bag.java | 2 +- src/{APIs => Bag}/LinkedBag.java | 4 +--- src/{APIs => Bag}/Stats.java | 4 ++-- src/{ => Examples}/Deque.java | 2 ++ src/{ => Examples}/MathSet.java | 2 ++ src/Main.java | 10 ---------- src/{APIs => Queue}/LinkedQueue.java | 4 +--- src/{APIs => Queue}/Queue.java | 2 +- src/{APIs => Queue}/QueueTestClient.java | 4 ++-- 9 files changed, 12 insertions(+), 22 deletions(-) rename src/{APIs => Bag}/Bag.java (97%) rename src/{APIs => Bag}/LinkedBag.java (98%) rename src/{APIs => Bag}/Stats.java (94%) rename src/{ => Examples}/Deque.java (97%) rename src/{ => Examples}/MathSet.java (99%) delete mode 100644 src/Main.java rename src/{APIs => Queue}/LinkedQueue.java (99%) rename src/{APIs => Queue}/Queue.java (97%) rename src/{APIs => Queue}/QueueTestClient.java (93%) diff --git a/src/APIs/Bag.java b/src/Bag/Bag.java similarity index 97% rename from src/APIs/Bag.java rename to src/Bag/Bag.java index c4fc851..0c34b7c 100644 --- a/src/APIs/Bag.java +++ b/src/Bag/Bag.java @@ -1,4 +1,4 @@ -package APIs; +package Bag; public interface Bag extends Iterable { /** diff --git a/src/APIs/LinkedBag.java b/src/Bag/LinkedBag.java similarity index 98% rename from src/APIs/LinkedBag.java rename to src/Bag/LinkedBag.java index 27abee8..55c534b 100644 --- a/src/APIs/LinkedBag.java +++ b/src/Bag/LinkedBag.java @@ -1,6 +1,4 @@ -package APIs; - -import Stack.LinkedStack; +package Bag; import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/src/APIs/Stats.java b/src/Bag/Stats.java similarity index 94% rename from src/APIs/Stats.java rename to src/Bag/Stats.java index 168d430..31a8df3 100644 --- a/src/APIs/Stats.java +++ b/src/Bag/Stats.java @@ -1,4 +1,4 @@ -package APIs; +package Bag; import java.util.Scanner; @@ -6,7 +6,7 @@ public class Stats { public static void main(String[] args) { // setup storage bag - Bag numbers = new LinkedBag<>(); + Bag numbers = new LinkedBag(); // setup scanner, taking in given string Scanner line = new Scanner("100 99 101 120 98 107 109 81 101 90"); diff --git a/src/Deque.java b/src/Examples/Deque.java similarity index 97% rename from src/Deque.java rename to src/Examples/Deque.java index 0107f47..a4d7714 100644 --- a/src/Deque.java +++ b/src/Examples/Deque.java @@ -1,3 +1,5 @@ +package Examples; + /** * Deque: double-ended queue API * Supports adding and removing items at both ends. diff --git a/src/MathSet.java b/src/Examples/MathSet.java similarity index 99% rename from src/MathSet.java rename to src/Examples/MathSet.java index 5b87e0d..d5b6ea0 100644 --- a/src/MathSet.java +++ b/src/Examples/MathSet.java @@ -1,3 +1,5 @@ +package Examples; + /** * MathSet API (interface / abstract data type) * represents a mathematical set. Sets in mathematics diff --git a/src/Main.java b/src/Main.java deleted file mode 100644 index 8f8f984..0000000 --- a/src/Main.java +++ /dev/null @@ -1,10 +0,0 @@ -//TIP To Run code, press or -// click the icon in the gutter. -public class Main { - public static void main(String[] args) { - //TIP Press with your caret at the highlighted text - // to see how IntelliJ IDEA suggests fixing it. - System.out.println("Hello and welcome!"); - - } -} \ No newline at end of file diff --git a/src/APIs/LinkedQueue.java b/src/Queue/LinkedQueue.java similarity index 99% rename from src/APIs/LinkedQueue.java rename to src/Queue/LinkedQueue.java index dedc6eb..dc62f50 100644 --- a/src/APIs/LinkedQueue.java +++ b/src/Queue/LinkedQueue.java @@ -1,6 +1,4 @@ -package APIs; - -import Stack.LinkedStack; +package Queue; import java.util.Iterator; import java.util.NoSuchElementException; diff --git a/src/APIs/Queue.java b/src/Queue/Queue.java similarity index 97% rename from src/APIs/Queue.java rename to src/Queue/Queue.java index a2a5575..0dbc73b 100644 --- a/src/APIs/Queue.java +++ b/src/Queue/Queue.java @@ -1,4 +1,4 @@ -package APIs; +package Queue; /** * FIFO (first-in, first-out) APIs.Queue API diff --git a/src/APIs/QueueTestClient.java b/src/Queue/QueueTestClient.java similarity index 93% rename from src/APIs/QueueTestClient.java rename to src/Queue/QueueTestClient.java index 0a9950e..06e0904 100644 --- a/src/APIs/QueueTestClient.java +++ b/src/Queue/QueueTestClient.java @@ -1,11 +1,11 @@ -package APIs; +package Queue; import java.util.Scanner; public class QueueTestClient { public static void main(String[] args) { // setup storage queue - Queue storage = new LinkedQueue<>(); + Queue storage = new LinkedQueue(); // setup scanner, taking in given string Scanner line = new Scanner("to be or not to - be - - that - - - is"); From 2e7771a9b5a551fa354661552b5ca115b6ba5be7 Mon Sep 17 00:00:00 2001 From: theZalmanian Date: Sun, 18 Feb 2024 02:16:59 -0800 Subject: [PATCH 52/52] Conducted a runtime analysis of LinkedQueue and LinkedBag --- src/Bag/Bag.java | 5 +++++ src/Bag/LinkedBag.java | 19 +++++++++++++++++-- src/Queue/LinkedQueue.java | 23 +++++++++++++++++++++++ src/Stack/LinkedStack.java | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/Bag/Bag.java b/src/Bag/Bag.java index 0c34b7c..4acb2e4 100644 --- a/src/Bag/Bag.java +++ b/src/Bag/Bag.java @@ -1,5 +1,10 @@ package Bag; +/** + * An implementation of the Bag data structure + * @param Class may store various types of values + * @author Zalman I. + */ public interface Bag extends Iterable { /** * Adds the given item to the bag diff --git a/src/Bag/LinkedBag.java b/src/Bag/LinkedBag.java index 55c534b..7aa66a1 100644 --- a/src/Bag/LinkedBag.java +++ b/src/Bag/LinkedBag.java @@ -4,8 +4,9 @@ import java.util.NoSuchElementException; /** - * - * @param + * Implementation of a Bag using a "list" of nodes and a Bag interface for generics + * @param Class may store various types of values + * @author Zalman I. */ public class LinkedBag implements Bag { /** @@ -24,6 +25,9 @@ private class Node { /** * Creates a node, and stores the given item within it + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation + * * @param item the item being stored in node */ public Node(E item) { @@ -47,6 +51,8 @@ public Node(E item) { /** * Constructs an empty LinkedBag + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public LinkedBag() { // bag starts off empty @@ -57,6 +63,9 @@ public LinkedBag() { /** * Adds the given item to the bag * + * Runtime: O(1) as we can instantly add a new item to start of list via the first variable, + * regardless of how many nodes exist in bag. + * * @param item the item to be added */ @Override @@ -81,6 +90,9 @@ public void add(E item) { /** * Checks whether the bag is empty, and returns true/false accordingly * + * Runtime: O(1) as we are instantly accessing variables and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the bag is empty; otherwise false */ @Override @@ -91,6 +103,9 @@ public boolean isEmpty() { /** * Gets and returns the number of items stored in the bag * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return the number of items stored in the bag */ @Override diff --git a/src/Queue/LinkedQueue.java b/src/Queue/LinkedQueue.java index dc62f50..a7acfda 100644 --- a/src/Queue/LinkedQueue.java +++ b/src/Queue/LinkedQueue.java @@ -3,6 +3,12 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * Implementation of a Queue using a "list" of nodes and a Queue interface for generics + * + * @param Class may store various types of values + * @author Zalman I. + */ public class LinkedQueue implements Queue { /** * A container which contains an item and a connection to another Node @@ -20,6 +26,9 @@ private class Node { /** * Creates a node, and stores the given item within it + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation + * * @param item the item being stored in node */ public Node(E item) { @@ -48,6 +57,8 @@ public Node(E item) { /** * Constructs an empty LinkedStack + * + * Runtime: O(1) as it always takes the same runtime to conduct this operation */ public LinkedQueue() { // queue starts off empty @@ -59,6 +70,9 @@ public LinkedQueue() { /** * Add an item to the queue. * + * Runtime: O(1) as we can instantly add a new item to back of queue via the back variable, + * regardless of how many nodes exist in list. + * * @param item the item to be added */ @Override @@ -88,6 +102,9 @@ public void enqueue(E item) { /** * Remove an item from the queue. * + * Runtime: O(1) as we can instantly access the front variable to retrieve item + * and overwrite it with the next node in queue. + * * @return the item that was removed */ @Override @@ -119,6 +136,9 @@ public E dequeue() { /** * Checks to see if the queue is empty. * + * Runtime: O(1) as we are instantly accessing variables and checking a condition. + * This operation would always take the same runtime to conduct. + * * @return true if the queue is empty, false otherwise */ @Override @@ -129,6 +149,9 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the queue. * + * Runtime: O(1) as we are instantly retrieving a variable. + * This operation would always take the same runtime to conduct. + * * @return the number of items in the queue */ @Override diff --git a/src/Stack/LinkedStack.java b/src/Stack/LinkedStack.java index b618cf0..50e0519 100644 --- a/src/Stack/LinkedStack.java +++ b/src/Stack/LinkedStack.java @@ -63,7 +63,7 @@ public LinkedStack() { /** * Add an item to the stack. * - * Runtime: O(1) as we can instantly add a new node to front/top via the top variable, + * Runtime: O(1) as we can instantly add a new item to front/top via the top variable, * regardless of how many nodes exist in stack. * * @param item the item to be added