From 31d012f643a4f575a9dc9cf0636552b741a24bde Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sat, 10 Feb 2024 15:57:53 -0800 Subject: [PATCH 01/13] Created ArrayList --- .idea/inspectionProfiles/Project_Default.xml | 297 +++++++++++++++++++ .idea/misc.xml | 1 - .idea/vcs.xml | 6 + src/ArrayList.java | 292 ++++++++++++++++++ 4 files changed, 595 insertions(+), 1 deletion(-) create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/vcs.xml create mode 100644 src/ArrayList.java diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..997def5 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,297 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 6f29fee..5af9c98 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,3 @@ - diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..1ff2e64 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,292 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size; + private E[] buffer; + + /** + * Constructor for an ArrayIntList object + */ + public ArrayList() { + size = 0; + this.buffer = (E[]) new Object[10]; + } + + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + if (size == buffer.length) { + resize(size * 2); + } + + for (int i = size; i >= 1; i--) { + buffer[i] = buffer[i - 1]; + } + + buffer[0] = item; + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + if (size == buffer.length) { + resize(size * 2); + } + buffer[size] = item; + size++; + } + + /** + * 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) { + if (i == 0 && size == 0) { + size++; + } + else if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("index is out of bounds"); + } + if (size == buffer.length) { + resize(size * 2); + } + + for (int j = size; j >= i; j--) { + buffer[j] = buffer[j - 1]; + } + + buffer[i] = item; + size++; + } + + /** + * 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) { + if (i >= size) { + throw new IndexOutOfBoundsException("Index cannot be greater than size"); + } + else if (i < 0) { + throw new IndexOutOfBoundsException("Index cannot be less than zero"); + } + return buffer[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 item the item to be saved + */ + @Override + public void set(int i, E item) { + //if index is out of bounds + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("index is out of bounds"); + } + + buffer[i] = item; + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + if (size != 0) { + + E frontItem = null; + for (int i = 0; i <= size; i++) { + frontItem = buffer[i]; + buffer[i] = buffer[i+1]; + } + size--; + return frontItem; + } + return null; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + if (size != 0) { + E frontItem = buffer[size]; + buffer[size] = null; + size--; + return frontItem; + } + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + for (int i = 0; i < size; i++) { + if (buffer[i] == item) { + buffer[i] = buffer[i+1]; + size--; + } + } + + } + + /** + * 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) { + //validate index + if (i >= size) { + throw new IndexOutOfBoundsException("Index cannot be greater than size"); + } + else if (i < 0) { + throw new IndexOutOfBoundsException("Index cannot be less than zero"); + } + + //save a copy of the value to be removed, so we can return it latter + E copyOfRemovedValue = buffer[i]; + + //shift values to the left + for (int j = i; j <= size - 1; j++) { + buffer[j] = buffer[j + 1]; + } + + buffer[size - 1] = null; + + //don't forget to decrease size + size--; + + return copyOfRemovedValue; + } + + /** + * 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) { + for (int i = 0; i < size; i++) { + if (buffer[i] == item) { + return true; + } + } + 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; + } + + + private void resize(int newSize) { + //create new space, separate from the old space + E[] newBuffer = (E[]) new Object[newSize]; + + //copy everything over from buffer into newBuffer + for (int i = 0; i < buffer.length; i++) { + newBuffer[i] = buffer[i]; + } + buffer = newBuffer; + + } + + /** + * Returns an iterator over elements of type {@code E}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new ArrayListIterator(); + } + + private class ArrayListIterator implements Iterator { + //private fields + private int i; + + private ArrayListIterator() { + i = 0; + } + + /** + * Returns true if the iteration has more elements. + * + * @return true if the iteration has more elements + */ + @Override + public boolean hasNext() { + return i < 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 (i >= size) { + throw new NoSuchElementException("i is now out of bounds"); + } + + E currentValue = buffer[i]; + i++; + + return currentValue; + } + } + + +} From 293a7b7c1fa1aa4ac0e81276c96a4618e7d41d05 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sat, 10 Feb 2024 16:45:19 -0800 Subject: [PATCH 02/13] Created LinkedList --- src/LinkedList.java | 340 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 340 insertions(+) create mode 100644 src/LinkedList.java diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..114506d --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,340 @@ +import java.util.Iterator; +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + //define what a Node is + private class Node { + E data; + Node next; + } + + private Node head; + private int size; + + public LinkedList() { + head = null; + size = 0; + } + + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + //set up a new node + Node theNewOne = new Node(); + theNewOne.data = item; + + if (head == null) { + // the list is currently empty + head = theNewOne; + } + else { + //the list currently has some nodes in it + theNewOne.next = head; + head = theNewOne; + } + size++; + } + + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) { + Node theNewOne = new Node(); + theNewOne.data = item; + Node current = head; + + if (head == null) { + head = theNewOne; + } + else { + while (current.next != null) { + current = current.next; + } + current.next = theNewOne; + } + size++; + } + + /** + * 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) { + + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); + } + else { + Node theNewOne = new Node(); + theNewOne.data = item; + Node current = head; + + if (size == 0) { + head = theNewOne; + } + else { + while (i != 0) { + if (i != 1) { + current = current.next; + } + i--; + } + + if (current.next == null) { + addBack(item); + } + else { + theNewOne.next = current.next; + current.next = theNewOne; + } + } + + size++; + } + + } + + /** + * 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) { + Node current = head; + i++; + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + else { + while (i != 1) { + current = current.next; + i--; + } + return current.data; + } + } + + /** + * 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) { + Node current = head; + + //if index is out of bounds + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("index is out of bounds"); + } + + for (int j = i; j > 0; j--) { + current = current.next; + } + + current.data = item; + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + E removedData = head.data; + if (!isEmpty()) { + head = head.next; + size--; + } + return removedData; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + Node current = head; + + if (current != null) { + if (current.next != null && current.next.next != null) { + while (current.next.next != null) { + current = current.next; + } + } + E removedData = current.next.data; + current.next = null; + size--; + return removedData; + } + return null; + } + + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + Node current = head; + + while (current.next != null) { + if (current.data == item) { + current = current.next; + } + + current = current.next; + } + size--; + + } + + /** + * 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) { + Node current = head; + i++; + + if (i < 0 || i > size) { + throw new IndexOutOfBoundsException("Index out of bounds"); + } + else { + + while (i != 0) { + if (i != 1) { + current = current.next; + } + i--; + } + + E value = current.next.data; + + if (current.next.next != null) { + //if the node to remove is not the last in the list + current.next = current.next.next; + } + else { + //if the node to remove is the last in the list + current.next = null; + } + + size--; + return value; + } + } + + /** + * 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) { + Node current = head; + + if (current != null) { + if (current.next != null) { + while (current.next != null) { + if (current.data == item) { + return true; + } + current = current.next; + } + } + return current.data == item; + } + + return false; + } + + /** + * Checks if the list is empty. + * + * @return true if the list is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return head == null; + } + + /** + * 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 new LinkedListIterator(); + } + + private class LinkedListIterator implements Iterator { + + + private Node current; + + public 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() { + E item = current.data; + current = current.next; + return item; + } + } +} From eafba5b18d9ed35c5543f8856bf5c668313a0afb Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sat, 10 Feb 2024 16:55:54 -0800 Subject: [PATCH 03/13] Created JUnit tests for ArrayList --- tests/ArrayListTest.java | 199 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 199 insertions(+) create mode 100644 tests/ArrayListTest.java diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java new file mode 100644 index 0000000..5ecd383 --- /dev/null +++ b/tests/ArrayListTest.java @@ -0,0 +1,199 @@ +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; + +public class ArrayListTest { + + + @Test + void addFront() { + ArrayList array = new ArrayList<>(); + + for (int i = 0; i < 20; i++) { + array.addFront(i); + assertEquals(array.get(0), i); + } + + + } + + @Test + void addBack() { + ArrayList array = new ArrayList<>(); + + for (int i = 0; i < 20; i++) { + array.addBack(i); + assertEquals(array.get(array.size()-1), i); + } + + } + + @Test + void add() { + ArrayList array = new ArrayList<>(); + + assertThrows(IndexOutOfBoundsException.class, () -> array.add(0, 8)); + + for (int i = 1; i < 20; i++) { + array.add(i, i+1); + assertEquals(array.get(i), i+1); + } + + } + + @Test + void removeFront() { + ArrayList array = new ArrayList<>(); + + array.removeFront(); + assertEquals(array.size(), 0); + + array.addFront(6); + array.removeFront(); + assertEquals(array.size(), 0); + + array.addFront(0); + array.addFront(9); + array.removeFront(); + assertEquals(array.size(), 1); + + array.addFront(0); + array.addFront(9); + array.removeFront(); + assertEquals(array.size(), 2); + + } + + @Test + void removeBack() { + ArrayList array = new ArrayList<>(); + + array.removeBack(); + assertEquals(array.size(), 0); + + array.addFront(6); + array.removeBack(); + assertEquals(array.size(), 0); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 1); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 2); + + } + + @Test + void removeItem() { + ArrayList array = new ArrayList<>(); + + assertThrows(IndexOutOfBoundsException.class, () -> array.remove(0)); + + array.addFront(6); + array.removeBack(); + assertEquals(array.size(), 0); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 1); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 2); + } + + + @Test + void removeIndex() { + ArrayList array = new ArrayList<>(); + + assertThrows(IndexOutOfBoundsException.class, () -> array.remove(0)); + + array.addFront(6); + array.removeBack(); + assertEquals(array.size(), 0); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 1); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(array.size(), 2); + } + @Test + void get() { + ArrayList array = new ArrayList<>(); + + assertThrows(IndexOutOfBoundsException.class, () -> array.get(0)); + + for (int i = 0; i < 20; i++) { + array.addFront(i); + assertEquals(array.get(0), i); + } + } + + @Test + void set() { + ArrayList array = new ArrayList<>(); + + } + + @Test + void contains() { + ArrayList array = new ArrayList<>(); + + + assertFalse(array.contains(4)); + + array.addFront(3); + assertTrue(array.contains(3)); + assertFalse(array.contains(9)); + + + array.addFront(8); + assertTrue(array.contains(3)); + assertFalse(array.contains(0)); + + for (int i = 0; i < 4; i++) { + array.addFront(i); + } + assertTrue(array.contains(2)); + assertFalse(array.contains(100)); + } + + + + @Test + void isEmpty() { + ArrayList array = new ArrayList<>(); + + assertTrue(array.isEmpty()); + + for (int i = 0; i < 20; i++) { + array.addBack(i); + assertFalse(array.isEmpty()); + } + } + + @Test + void size() { + ArrayList array = new ArrayList<>(); + + assertEquals(array.size(), 0); + + for (int i = 1; i < 20; i++) { + array.addBack(i); + assertEquals(array.size(), i); + } + } + + +} From afb74d5193922e10917073a7027ed26cd10f6c96 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sun, 11 Feb 2024 18:07:46 -0800 Subject: [PATCH 04/13] Created JUnit tests for LinkedList --- SDEV333-Term-Project.iml | 27 ++++++ src/ArrayList.java | 2 +- src/LinkedList.java | 70 +++++++------- tests/ArrayListTest.java | 52 +++++----- tests/LinkedListTest.java | 196 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 291 insertions(+), 56 deletions(-) create mode 100644 tests/LinkedListTest.java diff --git a/SDEV333-Term-Project.iml b/SDEV333-Term-Project.iml index c90834f..9c8bf10 100644 --- a/SDEV333-Term-Project.iml +++ b/SDEV333-Term-Project.iml @@ -4,8 +4,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/ArrayList.java b/src/ArrayList.java index 1ff2e64..2474f6a 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -101,7 +101,7 @@ else if (i < 0) { @Override public void set(int i, E item) { //if index is out of bounds - if (i < 0 || i > size) { + if (i <= 0 || i > size) { throw new IndexOutOfBoundsException("index is out of bounds"); } diff --git a/src/LinkedList.java b/src/LinkedList.java index 114506d..5db0ccb 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -136,20 +136,20 @@ public E get(int i) { */ @Override public void set(int i, E item) { - Node current = head; - - //if index is out of bounds - if (i < 0 || i > size) { - throw new IndexOutOfBoundsException("index is out of bounds"); + // if index is out of bounds + if (i < 0 || i >= size) { + throw new IndexOutOfBoundsException("Index is out of bounds"); } + Node current = head; - for (int j = i; j > 0; j--) { + for (int j = 0; j < i; j++) { current = current.next; } current.data = item; } + /** * Remove item at the front of the list. * @@ -157,12 +157,14 @@ public void set(int i, E item) { */ @Override public E removeFront() { - E removedData = head.data; - if (!isEmpty()) { + if (head != null) { + E removedData = head.data; head = head.next; size--; + return removedData; } - return removedData; + + throw new IndexOutOfBoundsException("Index is out of bounds"); } /** @@ -180,12 +182,13 @@ public E removeBack() { current = current.next; } } - E removedData = current.next.data; + E removedData = current.data; current.next = null; size--; return removedData; } - return null; + throw new IndexOutOfBoundsException("Index is out of bounds"); + } /** @@ -216,35 +219,36 @@ public void remove(E item) { */ @Override public E remove(int i) { - Node current = head; - i++; - - if (i < 0 || i > size) { + //validate index + if (i < 0 || i >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } - else { - - while (i != 0) { - if (i != 1) { - current = current.next; - } - i--; - } - - E value = current.next.data; - if (current.next.next != null) { - //if the node to remove is not the last in the list - current.next = current.next.next; - } - else { - //if the node to remove is the last in the list - current.next = null; - } + Node current = head; + // if the index is the first element + if (i == 0) { + E value = head.data; + head = head.next; size--; return value; } + + for (int j = 0; j < i - 1; j++) { + current = current.next; + } + + E value = current.next.data; + + if (current.next.next != null) { + current.next = current.next.next; + } + else { + current.next = null; + } + + size--; + return value; } /** diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index 5ecd383..111a708 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -88,46 +88,46 @@ void removeBack() { @Test void removeItem() { - ArrayList array = new ArrayList<>(); - - assertThrows(IndexOutOfBoundsException.class, () -> array.remove(0)); + ArrayList array = new ArrayList<>(); - array.addFront(6); - array.removeBack(); + array.addFront("test1"); + array.remove("test1"); assertEquals(array.size(), 0); - array.addFront(0); - array.addFront(9); - array.removeBack(); + array.addFront("test2"); + array.addFront("test3"); + array.remove("test3"); assertEquals(array.size(), 1); - array.addFront(0); - array.addFront(9); - array.removeBack(); + array.addFront("test4"); + array.addFront("test5"); + array.remove("test4"); assertEquals(array.size(), 2); } @Test void removeIndex() { - ArrayList array = new ArrayList<>(); + ArrayList array = new ArrayList<>(); assertThrows(IndexOutOfBoundsException.class, () -> array.remove(0)); - array.addFront(6); - array.removeBack(); + array.addFront("test1"); + array.remove(0); assertEquals(array.size(), 0); - array.addFront(0); - array.addFront(9); - array.removeBack(); + array.addFront("test2"); + array.addFront("test3"); + array.remove(1); assertEquals(array.size(), 1); - array.addFront(0); - array.addFront(9); - array.removeBack(); + array.addFront("test4"); + array.addFront("test5"); + array.remove(2); assertEquals(array.size(), 2); } + + @Test void get() { ArrayList array = new ArrayList<>(); @@ -143,6 +143,16 @@ void get() { @Test void set() { ArrayList array = new ArrayList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.set(0, 3)); + array.addFront(1); + + for (int i = 1; i < 10; i++) { + array.addFront(i); + } + + array.set(4, 7); + assertEquals(7, array.get(4)); + } @@ -169,8 +179,6 @@ void contains() { assertFalse(array.contains(100)); } - - @Test void isEmpty() { ArrayList array = new ArrayList<>(); diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java new file mode 100644 index 0000000..449d08a --- /dev/null +++ b/tests/LinkedListTest.java @@ -0,0 +1,196 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class LinkedListTest { + + @Test + void addFront() { + LinkedList array = new LinkedList<>(); + + for (int i = 0; i < 20; i++) { + array.addFront(i); + assertEquals(array.get(0), i); + } + + } + + @Test + void addBack() { + LinkedList array = new LinkedList<>(); + + for (int i = 0; i < 20; i++) { + array.addBack(i); + assertEquals(array.get(array.size()-1), i); + } + + } + + @Test + void add() { + LinkedList array = new LinkedList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.add(1, 8)); + + for (int i = 0; i < 20; i++) { + array.add(i, i+1); + assertEquals(array.get(i), i+1); + } + + } + + @Test + void removeFront() { + LinkedList array = new LinkedList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.removeFront()); + assertEquals(array.size(), 0); + + array.addFront("test1"); + array.removeFront(); + assertEquals(array.size(), 0); + + array.addFront("test2"); + array.addFront("test3"); + array.removeFront(); + assertEquals(array.size(), 1); + + array.addFront("test4"); + array.addFront("test5"); + array.removeFront(); + assertEquals(array.size(), 2); + + } + + @Test + void removeBack() { + LinkedList array = new LinkedList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.removeBack()); + assertEquals(array.size(), 0); + + array.addFront(6); + array.removeBack(); + assertEquals(0, array.size()); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(1, array.size()); + + array.addFront(0); + array.addFront(9); + array.removeBack(); + assertEquals(2, array.size()); + + } + + @Test + void removeItem() { + LinkedList array = new LinkedList<>(); + + array.addFront("test1"); + array.remove("test1"); + assertEquals(array.size(), 0); + + array.addFront("test2"); + array.addFront("test3"); + array.remove("test3"); + assertEquals(array.size(), 1); + + array.addFront("test4"); + array.addFront("test5"); + array.remove("test4"); + assertEquals(array.size(), 2); + } + + @Test + void removeIndex() { + LinkedList array = new LinkedList<>(); + + assertThrows(IndexOutOfBoundsException.class, () -> array.remove(0)); + + array.addFront("test1"); + array.remove(0); + assertEquals(array.size(), 0); + + array.addFront("test2"); + array.addFront("test3"); + array.remove(1); + assertEquals(array.size(), 1); + + array.addFront("test4"); + array.addFront("test5"); + array.remove(2); + assertEquals(array.size(), 2); + } + + @Test + void get() { + LinkedList array = new LinkedList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.get(0)); + + for (int i = 0; i < 20; i++) { + array.addFront(i); + assertEquals(array.get(0), i); + } + } + + @Test + void set() { + LinkedList array = new LinkedList<>(); + assertThrows(IndexOutOfBoundsException.class, () -> array.set(0, 3)); + array.addFront(1); + + for (int i = 1; i < 10; i++) { + array.addFront(i); + } + + array.set(4, 7); + assertEquals(7, array.get(4)); + + } + + @Test + void contains() { + LinkedList array = new LinkedList<>(); + + assertFalse(array.contains(4)); + + array.addFront(3); + assertTrue(array.contains(3)); + assertFalse(array.contains(9)); + + + array.addFront(8); + assertTrue(array.contains(3)); + assertFalse(array.contains(0)); + + for (int i = 0; i < 4; i++) { + array.addFront(i); + } + assertTrue(array.contains(2)); + assertFalse(array.contains(100)); + } + + @Test + void isEmpty() { + LinkedList array = new LinkedList<>(); + assertTrue(array.isEmpty()); + + for (int i = 0; i < 20; i++) { + array.addBack(i); + assertFalse(array.isEmpty()); + } + } + + @Test + void size() { + LinkedList array = new LinkedList<>(); + assertEquals(array.size(), 0); + + for (int i = 1; i < 20; i++) { + array.addBack(i); + assertEquals(array.size(), i); + } + } + + +} From 896e2ceb82cd81aad4fd809a074c35afe110a5e4 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sun, 11 Feb 2024 18:38:12 -0800 Subject: [PATCH 05/13] Created runtime analyses for most ArrayList methods --- src/ArrayList.java | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 2474f6a..7428171 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -8,6 +8,8 @@ public class ArrayList implements List { /** * Constructor for an ArrayIntList object + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same */ public ArrayList() { size = 0; @@ -17,7 +19,8 @@ public ArrayList() { /** * Add item to the front. - * + * This method runs in O(n) or linear time in the worst case + * because of the time needed to resize and shift elements * @param item the item to be added */ @Override @@ -36,7 +39,7 @@ public void addFront(E item) { /** * Add item to the back. - * + * This method runs in O(n) or linear time in the worst case because of the time needed to resize * @param item the item to be added */ @Override @@ -50,7 +53,7 @@ public void addBack(E item) { /** * Add an item at specified index (position). - * + * This method runs in O(n) or linear time in the worst case because of the time needed to resize and shift elements * @param i the index where the item should be added * @param item the item to be added */ @@ -76,7 +79,8 @@ else if (i < 0 || i > size) { /** * Get the item at a specified index. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param i the index where the item should be retrieved * @return the item located at that index */ @@ -94,7 +98,8 @@ else if (i < 0) { /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param i the index where the item should be saved * @param item the item to be saved */ @@ -110,6 +115,7 @@ public void set(int i, E item) { /** * Remove item at the front of the list. + * This method runs in O(n) or linear time in the worst case because of the time needed to shift elements * * @return the item that was removed */ @@ -130,7 +136,8 @@ public E removeFront() { /** * Remove item at the back of the list - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the item that was removed */ @Override @@ -146,7 +153,7 @@ public E removeBack() { /** * Remove item from the list - * + * This method runs in O(n) or linear time in the worst case because of the time needed to shift elements * @param item the item to be removed */ @Override @@ -162,7 +169,7 @@ public void remove(E item) { /** * Remove item at a specified index. - * + * This method runs in O(n) or linear time in the worst case because of the time needed to shift elements * @param i the index where the item should be removed * @return the item that was removed */ @@ -176,17 +183,14 @@ else if (i < 0) { throw new IndexOutOfBoundsException("Index cannot be less than zero"); } - //save a copy of the value to be removed, so we can return it latter E copyOfRemovedValue = buffer[i]; - //shift values to the left for (int j = i; j <= size - 1; j++) { buffer[j] = buffer[j + 1]; } buffer[size - 1] = null; - //don't forget to decrease size size--; return copyOfRemovedValue; @@ -194,7 +198,8 @@ else if (i < 0) { /** * Checks if an item is in the list. - * + * This method runs in O(n) or linear time in the worst case because if + * the item is at the end you'd need to search through all the elements in the list * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -210,7 +215,8 @@ public boolean contains(E item) { /** * Checks if the list is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the list is empty, false otherwise */ @Override @@ -220,7 +226,8 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return number of items in the list */ @Override @@ -243,7 +250,8 @@ private void resize(int newSize) { /** * Returns an iterator over elements of type {@code E}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override From 154b39ecce6979c85acf840ce18545daae42a3c0 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Sun, 11 Feb 2024 20:33:51 -0800 Subject: [PATCH 06/13] Created runtime analyses for most LinkedList methods --- src/LinkedList.java | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/src/LinkedList.java b/src/LinkedList.java index 5db0ccb..98f6d21 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -12,6 +12,11 @@ private class Node { private Node head; private int size; + /** + * constructor for the LinkedList + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same + */ public LinkedList() { head = null; size = 0; @@ -19,7 +24,8 @@ public LinkedList() { /** * Add item to the front. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param item the item to be added */ @Override @@ -42,7 +48,7 @@ public void addFront(E item) { /** * Add item to the back. - * + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param item the item to be added */ @Override @@ -65,8 +71,7 @@ public void addBack(E item) { /** * Add an item at specified index (position). - * - * @param i the index where the item should be added + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param item the item to be added */ @Override @@ -107,7 +112,7 @@ public void add(int i, E item) { /** * Get the item at a specified index. - * + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param i the index where the item should be retrieved * @return the item located at that index */ @@ -130,7 +135,7 @@ public E get(int i) { /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. - * + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param i the index where the item should be saved * @param item the item to be saved */ @@ -152,7 +157,8 @@ public void set(int i, E item) { /** * Remove item at the front of the list. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the item that was removed */ @Override @@ -169,7 +175,8 @@ public E removeFront() { /** * Remove item at the back of the list - * + * item at that index is overwritten. + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @return the item that was removed */ @Override @@ -193,7 +200,8 @@ public E removeBack() { /** * Remove item from the list - * + * item at that index is overwritten. + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param item the item to be removed */ @Override @@ -213,7 +221,7 @@ public void remove(E item) { /** * Remove item at a specified index. - * + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param i the index where the item should be removed * @return the item that was removed */ @@ -253,7 +261,7 @@ public E remove(int i) { /** * Checks if an item is in the list. - * + * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -278,7 +286,8 @@ public boolean contains(E item) { /** * Checks if the list is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the list is empty, false otherwise */ @Override @@ -288,7 +297,8 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return number of items in the list */ @Override @@ -298,7 +308,8 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override From 54b4fd3192c5f6e3c61fec66c2697ae415284ef0 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Mon, 12 Feb 2024 17:51:27 -0800 Subject: [PATCH 07/13] Created the Bag interface --- src/Bag.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/Bag.java diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..9f9f9f6 --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,23 @@ +/** + * Bag API + * @param class / data type of the items in the stack + */ +public interface Bag extends Iterable { + /** + * Add an item to the bag. + * @param item the item to be added + */ + void add(E item); + + /** + * Checks to see if the bag is empty. + * @return true if the bag is empty, false otherwise + */ + boolean isEmpty(); + + /** + * Returns a count of the number of items in the bag. + * @return the number of items in the bag + */ + int size(); +} From aefa361a410f65dd0631c9fc80012a5563c5e232 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 08:59:33 -0800 Subject: [PATCH 08/13] Created ResizingArrayStack and tested it in main --- src/ResizingArrayStack.java | 117 ++++++++++++++++++++++++++++++++++++ src/StackTestClient.java | 22 +++++++ 2 files changed, 139 insertions(+) create mode 100644 src/ResizingArrayStack.java create mode 100644 src/StackTestClient.java diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..24541bc --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,117 @@ +import java.util.Iterator; + +public class ResizingArrayStack implements Stack { + + private E[] array; + private int size; + + + /** + * a constructor for the ResizingArrayStack class + */ + public ResizingArrayStack() { + this.array = (E[]) new Object[1]; + this.size = 0; + } + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + //add item to top of stack + if (size == array.length) { + resize(2*array.length); + } + System.out.println(item); + array[size] = item; + size++; + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + //remove item from top of stack + size--; + E item = array[size]; + array[size] = null; //avoid loitering + if (size > 0 && size == array.length/4) { + resize(array.length/2); + } + return item; + } + + /** + * 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 array[size - 1]; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return 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; + } + + + private void resize(int max) { + //move stack to a new array of size max + E[] temp = (E[]) new Object[max]; + for (int i = 0; i < size; i++) { + temp[i] = array[i]; + } + array = temp; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new ReverseArrayIterator(); + } + + private class ReverseArrayIterator implements Iterator { + //support LIFO iteration + private int i = size-1; + + + public boolean hasNext() { + return i >= 0; + } + + public E next() { + i--; + return array[i]; + } + + + } +} diff --git a/src/StackTestClient.java b/src/StackTestClient.java new file mode 100644 index 0000000..557cf79 --- /dev/null +++ b/src/StackTestClient.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class StackTestClient { + + public static void main(String[] args) { + Stack s = new ResizingArrayStack(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + + while (in.hasNext()) { + String item = in.next(); + if (!item.equals("-")) { + s.push(item); + } + else if (!s.isEmpty()) { + System.out.println(s.pop() + " "); + } + } + + System.out.println("(" + s.size() + " left on the stack)"); + + } +} From acb008e5b165f035677f738ba321c5d53ab0f852 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 09:28:52 -0800 Subject: [PATCH 09/13] Created LinkedStack and tested it in main --- src/LinkedStack.java | 114 +++++++++++++++++++++++++++++++++++++++ src/StackTestClient.java | 2 +- 2 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 src/LinkedStack.java diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..296e33e --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,114 @@ +import java.util.Iterator; + +public class LinkedStack implements Stack { + + private Node head; + private int size; + + private class Node { + E item; + Node next; + } + + /** + * Add an item to the stack. + * + * @param item the item to be added + */ + @Override + public void push(E item) { + Node oldFirst = head; + head = new Node(); + head.item = item; + head.next = oldFirst; + size++; + } + + /** + * Removes the most recently added item from the stack. + * + * @return the item that was removed + */ + @Override + public E pop() { + E item = head.item; + head = head.next; + size--; + return item; + } + + /** + * 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 head.item; + } + + /** + * Checks to see if the stack is empty. + * + * @return true if the stack is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return head == null; + } + + /** + * 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 new ListIterator(); + } + + private class ListIterator implements Iterator { + + private Node current; + + public ListIterator() { + this.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 + */ + @Override + public E next() { + E item = current.item; + current = current.next; + return item; + } + } +} diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 557cf79..f23f4a9 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -3,7 +3,7 @@ public class StackTestClient { public static void main(String[] args) { - Stack s = new ResizingArrayStack(); + Stack s = new LinkedStack(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); while (in.hasNext()) { From b515d5c39c9890d246f3f0d6bcf4d2bc247445b5 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 09:41:57 -0800 Subject: [PATCH 10/13] Created LinkedQueue and tested it in QueueTestClient --- src/LinkedQueue.java | 113 +++++++++++++++++++++++++++++++++++++++ src/QueueTestClient.java | 22 ++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/LinkedQueue.java create mode 100644 src/QueueTestClient.java diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..8f1a6a7 --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,113 @@ +import java.util.Iterator; + +public class LinkedQueue implements Queue { + + private Node head; + private Node tail; + private int size; + + private class Node { + E item; + Node next; + } + + /** + * Add an item to the queue. + * + * @param item the item to be added + */ + @Override + public void enqueue(E item) { + Node oldLast = tail; + tail = new Node(); + tail.item = item; + tail.next = null; + if (isEmpty()) { + head = tail; + } + else { + oldLast.next = tail; + } + size++; + } + + /** + * Remove an item from the queue. + * + * @return the item that was removed + */ + @Override + public E dequeue() { + E item = head.item; + head = head.next; + size--; + if (isEmpty()) { + tail = null; + } + return item; + } + + /** + * Checks to see if the queue is empty. + * + * @return true if the queue is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return head == null; + } + + /** + * Returns a count of the number of items in the queue. + * + * @return the number of items in the queue + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new LinkedQueue.ListIterator(); + } + + private class ListIterator implements Iterator { + + private LinkedQueue.Node current; + + public ListIterator() { + this.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 + */ + @Override + public E next() { + E item = (E) current.item; + current = current.next; + return item; + } + } +} diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java new file mode 100644 index 0000000..3bb4b26 --- /dev/null +++ b/src/QueueTestClient.java @@ -0,0 +1,22 @@ +import java.util.Scanner; + +public class QueueTestClient { + + public static void main(String[] args) { + Queue q = new LinkedQueue(); + Scanner in = new Scanner("to be or not to - be - - that - - - is"); + + while (in.hasNext()) { + String item = in.next(); + if (!item.equals("-")) { + q.enqueue(item); + } + else if (!q.isEmpty()) { + System.out.println(q.dequeue() + " "); + } + } + + System.out.println("(" + q.size() + " left on the stack)"); + + } +} From 63b7cc2357552e94c6e0ab1e64ad8c6600f1b373 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 10:08:50 -0800 Subject: [PATCH 11/13] Created LinkedBag and tested it in Stats --- src/LinkedBag.java | 90 ++++++++++++++++++++++++++++++++++++++++++++++ src/Stats.java | 31 ++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 src/LinkedBag.java create mode 100644 src/Stats.java diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..e3b97d0 --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,90 @@ +import java.util.Iterator; + +public class LinkedBag implements Bag { + + private Node head; + private int size; + + private class Node { + E item; + Node next; + } + + /** + * Add an item to the bag. + * + * @param item the item to be added + */ + @Override + public void add(E item) { + Node oldHead = head; + head = new Node(); + head.item = item; + head.next = oldHead; + size++; + } + + /** + * Checks to see if the bag is empty. + * + * @return true if the bag is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return head == null; + } + + /** + * Returns a count of the number of items in the bag. + * + * @return the number of items in the bag + */ + @Override + public int size() { + return size; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return new LinkedBag.ListIterator(); + } + + private class ListIterator implements Iterator { + + private LinkedBag.Node current; + + public ListIterator() { + this.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 + */ + @Override + public E next() { + E item = (E) current.item; + current = current.next; + return item; + } + } +} diff --git a/src/Stats.java b/src/Stats.java new file mode 100644 index 0000000..532cf98 --- /dev/null +++ b/src/Stats.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +public class Stats { + public static void main(String[] args) { + Bag numbers = new LinkedBag(); + Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); + + while (in.hasNextDouble()) { + numbers.add(in.nextDouble()); + } + + int size = numbers.size(); + double sum = 0.0; + for (double num : numbers) { + sum += num; + } + double mean = sum/size; + sum = 0.0; + + for (double num : numbers) { + sum += (num - mean) * (num - mean); + } + double stddev = Math.sqrt(sum/(size-1)); + + System.out.printf("Mean: %.2f\n", mean); + System.out.printf("Stddev: %.2f\n", stddev); + } + + + +} From 4db9c3583ed7018920db625f7ad11d99629fb8e0 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 10:54:41 -0800 Subject: [PATCH 12/13] Added runtime analyses for LinkedBag, LinkedQueue, LinkedStack, and ResizingArrayStack --- src/LinkedBag.java | 12 ++++++++---- src/LinkedQueue.java | 15 ++++++++++----- src/LinkedStack.java | 18 ++++++++++++------ src/ResizingArrayStack.java | 21 ++++++++++++++------- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/LinkedBag.java b/src/LinkedBag.java index e3b97d0..6621664 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -12,7 +12,8 @@ private class Node { /** * Add an item to the bag. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param item the item to be added */ @Override @@ -26,7 +27,8 @@ public void add(E item) { /** * Checks to see if the bag is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the bag is empty, false otherwise */ @Override @@ -36,7 +38,8 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the bag. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the number of items in the bag */ @Override @@ -46,7 +49,8 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 8f1a6a7..5ad11f8 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -13,7 +13,8 @@ private class Node { /** * Add an item to the queue. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param item the item to be added */ @Override @@ -33,7 +34,8 @@ public void enqueue(E item) { /** * Remove an item from the queue. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the item that was removed */ @Override @@ -49,7 +51,8 @@ public E dequeue() { /** * Checks to see if the queue is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the queue is empty, false otherwise */ @Override @@ -59,7 +62,8 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the queue. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the number of items in the queue */ @Override @@ -69,7 +73,8 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 296e33e..5ba9c2c 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -12,7 +12,8 @@ private class Node { /** * Add an item to the stack. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @param item the item to be added */ @Override @@ -26,7 +27,8 @@ public void push(E item) { /** * Removes the most recently added item from the stack. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the item that was removed */ @Override @@ -40,7 +42,8 @@ public E pop() { /** * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return item at the top of the stack. */ @Override @@ -50,7 +53,8 @@ public E peek() { /** * Checks to see if the stack is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the stack is empty, false otherwise */ @Override @@ -60,7 +64,8 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the stack. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the number of items in the stack */ @Override @@ -70,7 +75,8 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index 24541bc..d2570bd 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -7,7 +7,9 @@ public class ResizingArrayStack implements Stack { /** - * a constructor for the ResizingArrayStack class + * A constructor for the ResizingArrayStack class + * * This method runs in O(1) or constant time in the worst case because + * * regardless of the size of the array, the number of operations executed remains the same */ public ResizingArrayStack() { this.array = (E[]) new Object[1]; @@ -16,7 +18,7 @@ public ResizingArrayStack() { /** * Add an item to the stack. - * + * This method runs in O(n) or linear time in the worst case because of the potential need to resize * @param item the item to be added */ @Override @@ -32,7 +34,8 @@ public void push(E item) { /** * Removes the most recently added item from the stack. - * + * This method runs in O(n) or linear time in the worst case + * because of the time it takes to loop through the array * @return the item that was removed */ @Override @@ -50,7 +53,8 @@ public E pop() { /** * Returns the item at the top of the stack. * Does not modify the stack or the item at the top. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return item at the top of the stack. */ @Override @@ -60,7 +64,8 @@ public E peek() { /** * Checks to see if the stack is empty. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return true if the stack is empty, false otherwise */ @Override @@ -70,7 +75,8 @@ public boolean isEmpty() { /** * Returns a count of the number of items in the stack. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return the number of items in the stack */ @Override @@ -90,7 +96,8 @@ private void resize(int max) { /** * Returns an iterator over elements of type {@code T}. - * + * This method runs in O(1) or constant time in the worst case because + * regardless of the size of the array, the number of operations executed remains the same * @return an Iterator. */ @Override From 5d59f8fefa6423257ca81b3763fb7b59742f80b5 Mon Sep 17 00:00:00 2001 From: Lois Lanctot Date: Thu, 15 Feb 2024 11:13:03 -0800 Subject: [PATCH 13/13] Added class JavaDocs to all classes --- src/ArrayList.java | 60 ++++++++++++++++++++----------------- src/LinkedBag.java | 7 +++++ src/LinkedList.java | 56 ++++++++++++++++++---------------- src/LinkedQueue.java | 6 ++++ src/LinkedStack.java | 6 ++++ src/Main.java | 11 +++++-- src/QueueTestClient.java | 25 +++++++++++----- src/ResizingArrayStack.java | 16 +++++++--- src/StackTestClient.java | 25 +++++++++++----- src/Stats.java | 17 ++++++++--- 10 files changed, 150 insertions(+), 79 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 7428171..08673f7 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,6 +1,12 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * A generic implementation of an array list + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class ArrayList implements List { private int size; @@ -54,26 +60,26 @@ public void addBack(E item) { /** * Add an item at specified index (position). * This method runs in O(n) or linear time in the worst case because of the time needed to resize and shift elements - * @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) { - if (i == 0 && size == 0) { + public void add(int index, E item) { + if (index == 0 && size == 0) { size++; } - else if (i < 0 || i > size) { + else if (index < 0 || index > size) { throw new IndexOutOfBoundsException("index is out of bounds"); } if (size == buffer.length) { resize(size * 2); } - for (int j = size; j >= i; j--) { + for (int j = size; j >= index; j--) { buffer[j] = buffer[j - 1]; } - buffer[i] = item; + buffer[index] = item; size++; } @@ -81,18 +87,18 @@ else if (i < 0 || i > size) { * Get the item at a specified index. * This method runs in O(1) or constant time in the worst case because * regardless of the size of the array, the number of operations executed remains the same - * @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) { - if (i >= size) { + public E get(int index) { + if (index >= size) { throw new IndexOutOfBoundsException("Index cannot be greater than size"); } - else if (i < 0) { + else if (index < 0) { throw new IndexOutOfBoundsException("Index cannot be less than zero"); } - return buffer[i]; + return buffer[index]; } /** @@ -100,17 +106,17 @@ else if (i < 0) { * item at that index is overwritten. * This method runs in O(1) or constant time in the worst case because * regardless of the size of the array, the number of operations executed remains the same - * @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) { //if index is out of bounds - if (i <= 0 || i > size) { + if (index <= 0 || index > size) { throw new IndexOutOfBoundsException("index is out of bounds"); } - buffer[i] = item; + buffer[index] = item; } /** @@ -170,22 +176,22 @@ public void remove(E item) { /** * Remove item at a specified index. * This method runs in O(n) or linear time in the worst case because of the time needed to shift elements - * @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) { //validate index - if (i >= size) { + if (index >= size) { throw new IndexOutOfBoundsException("Index cannot be greater than size"); } - else if (i < 0) { + else if (index < 0) { throw new IndexOutOfBoundsException("Index cannot be less than zero"); } - E copyOfRemovedValue = buffer[i]; + E copyOfRemovedValue = buffer[index]; - for (int j = i; j <= size - 1; j++) { + for (int j = index; j <= size - 1; j++) { buffer[j] = buffer[j + 1]; } @@ -261,10 +267,10 @@ public Iterator iterator() { private class ArrayListIterator implements Iterator { //private fields - private int i; + private int index; private ArrayListIterator() { - i = 0; + index = 0; } /** @@ -274,7 +280,7 @@ private ArrayListIterator() { */ @Override public boolean hasNext() { - return i < size; + return index < size; } /** @@ -285,12 +291,12 @@ public boolean hasNext() { */ @Override public E next() { - if (i >= size) { + if (index >= size) { throw new NoSuchElementException("i is now out of bounds"); } - E currentValue = buffer[i]; - i++; + E currentValue = buffer[index]; + index++; return currentValue; } diff --git a/src/LinkedBag.java b/src/LinkedBag.java index 6621664..e8c2fa7 100644 --- a/src/LinkedBag.java +++ b/src/LinkedBag.java @@ -1,5 +1,12 @@ import java.util.Iterator; + +/** + * A generic implementation of a linked bag + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class LinkedBag implements Bag { private Node head; diff --git a/src/LinkedList.java b/src/LinkedList.java index 98f6d21..ff401f5 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,6 +1,12 @@ import java.util.Iterator; import java.util.NoSuchElementException; +/** + * A generic implementation of a linked list + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class LinkedList implements List { //define what a Node is @@ -34,15 +40,12 @@ public void addFront(E item) { Node theNewOne = new Node(); theNewOne.data = item; - if (head == null) { - // the list is currently empty - head = theNewOne; - } - else { + if (head != null) { //the list currently has some nodes in it theNewOne.next = head; - head = theNewOne; } + head = theNewOne; + size++; } @@ -72,12 +75,13 @@ public void addBack(E item) { /** * Add an item at specified index (position). * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes + * @param index the specified index * @param item the item to be added */ @Override - public void add(int i, E item) { + public void add(int index, E item) { - if (i < 0 || i > size) { + if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index is out of bounds"); } else { @@ -89,11 +93,11 @@ public void add(int i, E item) { head = theNewOne; } else { - while (i != 0) { - if (i != 1) { + while (index != 0) { + if (index != 1) { current = current.next; } - i--; + index--; } if (current.next == null) { @@ -113,20 +117,20 @@ public void add(int i, E item) { /** * Get the item at a specified index. * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes - * @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) { Node current = head; - i++; - if (i < 0 || i > size) { + index++; + if (index < 0 || index > size) { throw new IndexOutOfBoundsException("Index out of bounds"); } else { - while (i != 1) { + while (index != 1) { current = current.next; - i--; + index--; } return current.data; } @@ -136,18 +140,18 @@ public E get(int i) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes - * @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) { // if index is out of bounds - if (i < 0 || i >= size) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index is out of bounds"); } Node current = head; - for (int j = 0; j < i; j++) { + for (int j = 0; j < index; j++) { current = current.next; } @@ -222,27 +226,27 @@ public void remove(E item) { /** * Remove item at a specified index. * This method runs in O(n) or linear time in the worst case because of the time it takes to traverse the nodes - * @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) { //validate index - if (i < 0 || i >= size) { + if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of bounds"); } Node current = head; // if the index is the first element - if (i == 0) { + if (index == 0) { E value = head.data; head = head.next; size--; return value; } - for (int j = 0; j < i - 1; j++) { + for (int j = 0; j < index - 1; j++) { current = current.next; } diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 5ad11f8..21db529 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,5 +1,11 @@ import java.util.Iterator; +/** + * A generic implementation of a linked queue + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class LinkedQueue implements Queue { private Node head; diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 5ba9c2c..1840502 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,5 +1,11 @@ import java.util.Iterator; +/** + * A generic implementation of a linked stack + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class LinkedStack implements Stack { private Node head; diff --git a/src/Main.java b/src/Main.java index 8f8f984..491ccbb 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,6 +1,13 @@ -//TIP To Run code, press or -// click the icon in the gutter. +/** + * main class for testing if the project forked properly + * @author Lois Lanctot + * @version 1.0 + */ public class Main { + /** + * prints a welcome message to the console + * @param args command line arguments passed to the program + */ public static void main(String[] args) { //TIP Press with your caret at the highlighted text // to see how IntelliJ IDEA suggests fixing it. diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java index 3bb4b26..420a10a 100644 --- a/src/QueueTestClient.java +++ b/src/QueueTestClient.java @@ -1,22 +1,31 @@ import java.util.Scanner; +/** + * A test for the linked queue + * @author Lois Lanctot + * @version 1.0 + */ public class QueueTestClient { + /** + * runs tests for the linked queue + * @param args command line arguments passed to the program + */ public static void main(String[] args) { - Queue q = new LinkedQueue(); - Scanner in = new Scanner("to be or not to - be - - that - - - is"); + Queue queue = new LinkedQueue<>(); + Scanner input = new Scanner("to be or not to - be - - that - - - is"); - while (in.hasNext()) { - String item = in.next(); + while (input.hasNext()) { + String item = input.next(); if (!item.equals("-")) { - q.enqueue(item); + queue.enqueue(item); } - else if (!q.isEmpty()) { - System.out.println(q.dequeue() + " "); + else if (!queue.isEmpty()) { + System.out.println(queue.dequeue() + " "); } } - System.out.println("(" + q.size() + " left on the stack)"); + System.out.println("(" + queue.size() + " left on the stack)"); } } diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index d2570bd..8bf9e36 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,5 +1,11 @@ import java.util.Iterator; +/** + * A generic implementation of a resizing array stack + * @param generic type + * @author Lois Lanctot + * @version 1.0 + */ public class ResizingArrayStack implements Stack { private E[] array; @@ -107,16 +113,18 @@ public Iterator iterator() { private class ReverseArrayIterator implements Iterator { //support LIFO iteration - private int i = size-1; + private int iterator = size-1; + @Override public boolean hasNext() { - return i >= 0; + return iterator >= 0; } + @Override public E next() { - i--; - return array[i]; + iterator--; + return array[iterator]; } diff --git a/src/StackTestClient.java b/src/StackTestClient.java index f23f4a9..8cc560d 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -1,22 +1,31 @@ import java.util.Scanner; +/** + * A test for the linked stack + * @author Lois Lanctot + * @version 1.0 + */ public class StackTestClient { + /** + * runs tests for the linked stack + * @param args command line arguments passed to the program + */ public static void main(String[] args) { - Stack s = new LinkedStack(); - Scanner in = new Scanner("to be or not to - be - - that - - - is"); + Stack stack = new LinkedStack<>(); + Scanner input = new Scanner("to be or not to - be - - that - - - is"); - while (in.hasNext()) { - String item = in.next(); + while (input.hasNext()) { + String item = input.next(); if (!item.equals("-")) { - s.push(item); + stack.push(item); } - else if (!s.isEmpty()) { - System.out.println(s.pop() + " "); + else if (!stack.isEmpty()) { + System.out.println(stack.pop() + " "); } } - System.out.println("(" + s.size() + " left on the stack)"); + System.out.println("(" + stack.size() + " left on the stack)"); } } diff --git a/src/Stats.java b/src/Stats.java index 532cf98..89db875 100644 --- a/src/Stats.java +++ b/src/Stats.java @@ -1,12 +1,21 @@ import java.util.Scanner; +/** + * A test for the linked bag + * @author Lois Lanctot + * @version 1.0 + */ public class Stats { + /** + * runs tests for the linked bag + * @param args command line arguments passed to the program + */ public static void main(String[] args) { - Bag numbers = new LinkedBag(); - Scanner in = new Scanner("100 99 101 120 98 107 109 81 101 90"); + Bag numbers = new LinkedBag<>(); + Scanner input = new Scanner("100 99 101 120 98 107 109 81 101 90"); - while (in.hasNextDouble()) { - numbers.add(in.nextDouble()); + while (input.hasNextDouble()) { + numbers.add(input.nextDouble()); } int size = numbers.size();