From 929aeb0683cc08873d52cc8d993eb9c5d32f58ac Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Fri, 16 Feb 2024 20:28:47 -0800 Subject: [PATCH 1/6] added ArrayList and LinkedList methods and tests for linked list --- .idea/misc.xml | 1 - SDEV333-Term-Project.iml | 17 ++ src/ArrayList.java | 226 +++++++++++++++++++++++++++ src/LinkedList.java | 319 ++++++++++++++++++++++++++++++++++++++ src/List.java | 6 +- src/Main.java | 26 ++++ tests/LinkedListTest.java | 192 +++++++++++++++++++++++ 7 files changed, 784 insertions(+), 3 deletions(-) create mode 100644 src/ArrayList.java create mode 100644 src/LinkedList.java create mode 100644 tests/LinkedListTest.java 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/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/src/ArrayList.java b/src/ArrayList.java new file mode 100644 index 0000000..b941d60 --- /dev/null +++ b/src/ArrayList.java @@ -0,0 +1,226 @@ +import java.util.Arrays; +import java.util.Iterator; +import java.util.ListIterator; + +public class ArrayList implements List{ + + private int size; + private E[] buffer; + + public ArrayList(){ + buffer = (E[]) new Object[10]; + size = 0; + } + /** + * Add item to the front. + * + * @param item the item to be added + */ + @Override + public void addFront(E item) { + if(size == buffer.length){ + resize(buffer.length * 2); + } + for (int i = size - 1; i >= 0; i--) { + buffer[i+1] = buffer[i]; + } + buffer[0] = item; + size++; + } + /** + * Add item to the back. + * + * @param item the item to be added + */ + @Override + public void addBack(E item) throws IllegalArgumentException{ + 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){ + throw new IndexOutOfBoundsException("Index can not be negative"); + } + if(size == buffer.length){ + buffer[i+1] = buffer[i]; + } + 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 < 0){ + throw new IndexOutOfBoundsException("Index can not be negative"); + } + if (i >= size){ + throw new IndexOutOfBoundsException("Index is higher than size"); + } + 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 + * @return E item that was removed + */ + @Override + public E set(int i, E item) { + if (i < 0) { + throw new IndexOutOfBoundsException("Index can not be negative"); + } + if (size == buffer.length) { + resize(size + 2); + } + E removed = buffer[i]; + buffer[i] = item; + return removed; + } + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + for (int i = 0; i < size - 1; i++) { + buffer[i] = buffer[i + 1]; + } + E removed = buffer[0]; + buffer[size - 1] = null; + size--; + return removed; + } + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + E removed = buffer[size - 1]; + buffer[size - 1] = null; + size--; + return removed; + } + /** + * 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) { + if (i < 0){ + throw new IndexOutOfBoundsException("Index can not be negative"); + } + if(i >= size){ + throw new IndexOutOfBoundsException("Index is higher than size"); + } + E removed = buffer[i]; + for (int j = i; j <= size-1 ; j++) { + buffer[i] = buffer[1 + i]; + } + buffer[size - 1] = null; + return removed; + } + /** + * 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() { + if(size < 0){ + throw new IndexOutOfBoundsException("size can not be negative"); + } + return size; + } + + private void resize(int newSize){ + // create a new space separate from the old buffer + E[] temp = (E[]) new Object[(int) newSize]; + // copy everything over from the buffer into new buffer + for (int i = 0; i < buffer.length; i++) { + temp[i] = buffer[i]; + } + // reassign temp data back to buffer with bigger array + buffer = temp; + } + + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + ArrayList theIterator = new ArrayList(); + return (Iterator) theIterator; + } + + private class ArrayListIterator implements Iterable{ + /** + * Returns an iterator over elements of type {@code T}. + * + * @return an Iterator. + */ + @Override + public Iterator iterator() { + return null; + } + } + @Override + public String toString() { + return "ArrayList{" + + "size=" + size + + ", buffer=" + Arrays.toString(buffer) + + '}'; + } +} diff --git a/src/LinkedList.java b/src/LinkedList.java new file mode 100644 index 0000000..efc96a0 --- /dev/null +++ b/src/LinkedList.java @@ -0,0 +1,319 @@ +import java.util.Iterator; +public class LinkedList implements List { + // define what a node is + private class Node { + E data; + Node next; + + @Override + public String toString() { + return data + + ", " + next; + } + } + // set up the head + private Node head; + + // set up the size field + private int size; + + // add a constructor to initialize the fields + 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 new node + Node theNewOne = new Node(); + theNewOne.data = item; + + if (head == null) { + //this list is currently empty + head = theNewOne; + size++; + } else { + //this list currently has some nodes in it + theNewOne.next = head; + //everything from the right gets saved to the left + 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; + if (size == 0) { + head = theNewOne; + size++; + } else { + // keeps track of where you are in the list + Node temp = head; + // if temp. next is not null + while (temp.next != null) { + // then iterate through the list + temp = temp.next; + } + // if temp.next is null then theNewOne is temp.next + temp.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) { + Node theNewOne = new Node(); + theNewOne.data = item; + if (i > size || i < 0) { + throw new IndexOutOfBoundsException(); + } else { + if (size == 0) { + head = theNewOne; + } else { + Node prev = null; + Node temp = head; + for (int j = 0; j < i; j++) { + prev = temp; + temp = temp.next; + } + theNewOne.next = temp; + if (prev == null) { + head = theNewOne; + } else { + prev.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 temp; + E result = null; + if (i >= size || i < 0) { + throw new IndexOutOfBoundsException(); + } else { + temp = head; + for (int j = 0; j < size; j++) { + // then iterate through the list + if (j == i) { + result = temp.data; + } else { + temp = temp.next; + } + } + } + return result; + } + /** + * 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 + * @return + */ + @Override + public E set(int i, E item) { + return null; + } + + /** + * Remove item at the front of the list. + * + * @return the item that was removed + */ + @Override + public E removeFront() { + E removed = (E) head; + if (head == null) { + //this list is currently empty + System.out.println("this list is empty"); + } else { + //this list currently has some nodes in it + head = head.next; + //everything from the right gets saved to the left + size--; + } + return removed; + } + + /** + * Remove item at the back of the list + * + * @return the item that was removed + */ + @Override + public E removeBack() { + Node temp = null; + if (head == null) { + //this list is currently empty + System.out.println("this list is empty"); + } else { + // keeps track of where you are in the list + temp = head; + // if temp. next .next is not null + while (temp.next != null) { + // then iterate through the list + temp = temp.next; + } + // if temp.next is null then theNewOne is temp.next + size--; + } + return (E) temp; + } + /** + * Remove item from the list + * + * @param item the item to be removed + */ + @Override + public void remove(E item) { + Node prev = null; + Node temp = head; + for (int i = 0; i < size; i++) { + if(temp.data.equals(item)){ + prev = temp; + temp = temp.next; + } + } + if (prev == null) { + head = temp.next; + } else { + prev.next = temp.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) { + if (i >= size || i < 0) { + throw new IndexOutOfBoundsException(); + } + Node prev = null; + Node temp = head; + for (int j = 0; j < i; j++) { + prev = temp; + temp = temp.next; + } + if (prev == null) { + head = temp.next; + } else { + prev.next = temp.next; + } + size--; + return temp.data; + } + + /** + * 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 temp = head; + // if temp. next is not null + while (temp != null) { + // then iterate through the list + if (temp.data.equals(item)) { + return true; + } else { + temp = temp.next; + } + } + 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 new LinkedIterator(); + } + + private class LinkedIterator implements Iterator { + + // private int i; // index of the item I am on in the arraylist + private Node current; + + public LinkedIterator() { + current = head; + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + E item = current.data; + current = current.next; + return item; + } + } + + @Override + public String toString() { + return "LinkedList{" + head + + ", size=" + size + + '}'; + } + + + +} \ No newline at end of file diff --git a/src/List.java b/src/List.java index 2bf4aef..4c7ad88 100644 --- a/src/List.java +++ b/src/List.java @@ -32,10 +32,12 @@ public interface List extends Iterable { /** * 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 i the index where the item should be saved * @param item the item to be saved + * @return */ - void set(int i, E item); + E set(int i, E item); /** * Remove item at the front of the list. diff --git a/src/Main.java b/src/Main.java index 8f8f984..7290fba 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,10 +1,36 @@ +import java.util.Iterator; +import java.util.ListIterator; + //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!"); + List obj = new ArrayList<>(); + obj.addBack("hi"); + obj.addBack("bye"); + obj.addFront("first"); + System.out.println(obj); + + // System.out.println(obj.iterator()); + + LinkedList linked = new LinkedList<>(); + linked.addFront("hello"); + linked.addBack("dance"); + linked.add(0,"55.5"); + + + Iterator iterating = linked.iterator(); + while (iterating.hasNext()){ + System.out.println(iterating.next()); + } + + } } \ No newline at end of file diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java new file mode 100644 index 0000000..2bc109e --- /dev/null +++ b/tests/LinkedListTest.java @@ -0,0 +1,192 @@ +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +class LinkedListTest { + @Test + void addFront() { + + LinkedList linked = new LinkedList<>(); + linked.addFront(5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + LinkedList oneElement = new LinkedList<>(); + oneElement.addFront("test1"); + oneElement.addFront("test2"); + + String expectedOneElement = "test2"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement,actualOneElement); + + LinkedList tenElement = new LinkedList<>(); + tenElement.addFront(6.6); + tenElement.addFront(4.6); + tenElement.addFront(3.6); + tenElement.addFront(39.8); + tenElement.addFront(45.7); + tenElement.addFront(3.0); + tenElement.addFront(4.7); + tenElement.addFront(12.12); + tenElement.addFront(43.1); + tenElement.addFront(1.1); + + Double tenElementsExpected = 1.1; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected,tenElementsActual); + + + } + + @Test + void addBack() { + + LinkedList linked = new LinkedList<>(); + linked.addBack(5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + LinkedList oneElement = new LinkedList<>(); + oneElement.addBack("test1"); + oneElement.addBack("test2"); + + String expectedOneElement = "test2"; + String actualOneElement = oneElement.get(1); + Assertions.assertEquals(expectedOneElement,actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.addFront(6.6); + tenElement.addFront(4.6); + tenElement.addFront(3.6); + tenElement.addFront(39.8); + tenElement.addFront(45.7); + tenElement.addFront(3.0); + tenElement.addFront(4.7); + tenElement.addFront(12.12); + tenElement.addFront(43.1); + tenElement.addFront(1.1); + + Double tenElementsExpected = 6.6; + Double tenElementsActual = tenElement.get(9); + + Assertions.assertEquals(tenElementsExpected,tenElementsActual); + } + + @Test + void add() { + LinkedList linked = new LinkedList<>(); + linked.add(0,5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0,"test1"); + oneElement.add(1,"test2"); + + String expectedOneElement = "test2"; + String actualOneElement = oneElement.get(1); + Assertions.assertEquals(expectedOneElement,actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0,1.1); + tenElement.add(1,1.2); + tenElement.add(2,1.3); + tenElement.add(3,1.4); + tenElement.add(4,1.5); + tenElement.add(5,1.6); + tenElement.add(6,1.7); + tenElement.add(7,1.8); + tenElement.add(8,1.9); + tenElement.add(9,2.0); + + + Double tenElementsExpected = 1.5; + Double tenElementsActual = tenElement.get(4); + + Assertions.assertEquals(tenElementsExpected,tenElementsActual); + } + + @Test + void get() { + + LinkedList linked = new LinkedList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer expected = null; + Integer actual = linked.get(0); + Assertions.assertEquals(expected, actual); + }); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0,"test1"); + oneElement.add(1,"test2"); + oneElement.get(0); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement,actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0,1.1); + tenElement.add(1,1.2); + tenElement.add(2,1.3); + tenElement.add(3,1.4); + tenElement.add(4,1.5); + tenElement.add(5,1.6); + tenElement.add(6,1.7); + tenElement.add(7,1.8); + tenElement.add(8,1.9); + tenElement.add(9,2.0); + + + Double tenElementsExpected = 1.5; + Double tenElementsActual = tenElement.get(4); + + Assertions.assertEquals(tenElementsExpected,tenElementsActual); + } + + @Test + void set() { + } + + @Test + void removeFront() { + } + + @Test + void removeBack() { + } + + @Test + void remove() { + } + + @Test + void testRemove() { + } + + @Test + void contains() { + } + + @Test + void isEmpty() { + } + + @Test + void size() { + } + + @Test + void iterator() { + } +} \ No newline at end of file From bc994753badcdfe76ba5e2bd26a6aac5087fa56e Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Sat, 17 Feb 2024 15:23:11 -0800 Subject: [PATCH 2/6] added linkedlist tests --- src/ArrayList.java | 16 +- src/LinkedList.java | 42 +++- src/Main.java | 5 + tests/ArrayListTest.java | 475 ++++++++++++++++++++++++++++++++++++++ tests/LinkedListTest.java | 366 ++++++++++++++++++++++++++--- 5 files changed, 863 insertions(+), 41 deletions(-) create mode 100644 tests/ArrayListTest.java diff --git a/src/ArrayList.java b/src/ArrayList.java index b941d60..572b9e7 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,6 +1,5 @@ import java.util.Arrays; import java.util.Iterator; -import java.util.ListIterator; public class ArrayList implements List{ @@ -91,6 +90,7 @@ public E set(int i, E item) { } E removed = buffer[i]; buffer[i] = item; + size++; return removed; } /** @@ -127,8 +127,15 @@ public E removeBack() { */ @Override public void remove(E item) { + for (int i = 0; i < size; i++) { + if(item.equals(buffer[i])){ + for (int j = i; j < size; j++) { + buffer[j] = buffer[j + 1]; + } + size--; - + } + } } /** * Remove item at a specified index. @@ -159,6 +166,11 @@ public E remove(int i) { */ @Override public boolean contains(E item) { + for (int i = 0; i < size; i++) { + if(item.equals(buffer[i])){ + return true; + } + } return false; } /** diff --git a/src/LinkedList.java b/src/LinkedList.java index efc96a0..947ad27 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,4 +1,5 @@ import java.util.Iterator; + public class LinkedList implements List { // define what a node is private class Node { @@ -7,10 +8,11 @@ private class Node { @Override public String toString() { - return data + + return data + ", " + next; } } + // set up the head private Node head; @@ -23,6 +25,7 @@ public LinkedList() { size = 0; } + /** * Add item to the front. * @@ -46,6 +49,7 @@ public void addFront(E item) { size++; } } + /** * Add item to the back. * @@ -71,6 +75,7 @@ public void addBack(E item) { size++; } } + /** * Add an item at specified index (position). * @@ -103,6 +108,7 @@ public void add(int i, E item) { size++; } } + /** * Get the item at a specified index. * @@ -113,7 +119,7 @@ public void add(int i, E item) { public E get(int i) { Node temp; E result = null; - if (i >= size || i < 0) { + if (i > size || i < 0) { throw new IndexOutOfBoundsException(); } else { temp = head; @@ -128,6 +134,7 @@ public E get(int i) { } return result; } + /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. @@ -138,6 +145,27 @@ public E get(int i) { */ @Override public E set(int i, E item) { + Node theNewOne = new Node(); + theNewOne.data = item; + if (i > size) { + throw new IndexOutOfBoundsException(); + } else { + if (size == 0) { + head = theNewOne; + } else { + Node temp = head; + for (int j = 0; j < size; j++) { + if (j == i) { + temp.data = theNewOne.data; + } else { + temp = temp.next; + } + + } + + } + } + size++; return null; } @@ -148,6 +176,9 @@ public E set(int i, E item) { */ @Override public E removeFront() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } E removed = (E) head; if (head == null) { //this list is currently empty @@ -168,6 +199,9 @@ public E removeFront() { */ @Override public E removeBack() { + if (size <= 0) { + throw new IndexOutOfBoundsException(); + } Node temp = null; if (head == null) { //this list is currently empty @@ -185,6 +219,7 @@ public E removeBack() { } return (E) temp; } + /** * Remove item from the list * @@ -195,7 +230,7 @@ public void remove(E item) { Node prev = null; Node temp = head; for (int i = 0; i < size; i++) { - if(temp.data.equals(item)){ + if (temp.data.equals(item)) { prev = temp; temp = temp.next; } @@ -315,5 +350,4 @@ public String toString() { } - } \ No newline at end of file diff --git a/src/Main.java b/src/Main.java index 7290fba..e89ea62 100644 --- a/src/Main.java +++ b/src/Main.java @@ -16,6 +16,9 @@ public static void main(String[] args) { obj.addBack("hi"); obj.addBack("bye"); obj.addFront("first"); + obj.set(1,"set"); + obj.remove("bye"); + System.out.println(obj.contains("B")); System.out.println(obj); // System.out.println(obj.iterator()); @@ -24,6 +27,8 @@ public static void main(String[] args) { linked.addFront("hello"); linked.addBack("dance"); linked.add(0,"55.5"); + linked.set(2,"set"); + System.out.println(linked); Iterator iterating = linked.iterator(); diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java new file mode 100644 index 0000000..e4ebd07 --- /dev/null +++ b/tests/ArrayListTest.java @@ -0,0 +1,475 @@ +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + + +class ArrayListTest { + + @Test + void addFront() { + ArrayList linked = new ArrayList<>(); + linked.addFront(5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + ArrayList oneElement = new ArrayList<>(); + oneElement.addFront("test1"); + oneElement.addFront("test2"); + + String expectedOneElement = "test2"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + ArrayList tenElement = new ArrayList<>(); + tenElement.addFront(6.6); + tenElement.addFront(4.6); + tenElement.addFront(3.6); + tenElement.addFront(39.8); + tenElement.addFront(45.7); + tenElement.addFront(3.0); + tenElement.addFront(4.7); + tenElement.addFront(12.12); + tenElement.addFront(43.1); + tenElement.addFront(1.1); + + Double tenElementsExpected = 1.1; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + + } + + @Test + void addBack() { + ArrayList linked = new ArrayList<>(); + linked.addBack(5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + ArrayList oneElement = new ArrayList<>(); + oneElement.addBack("test1"); + oneElement.addBack("test2"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + ArrayList tenElement = new ArrayList<>(); + tenElement.addBack(6.6); + tenElement.addBack(4.6); + tenElement.addBack(3.6); + tenElement.addBack(39.8); + tenElement.addBack(45.7); + tenElement.addBack(3.0); + tenElement.addBack(4.7); + tenElement.addBack(12.12); + tenElement.addBack(43.1); + tenElement.addBack(1.1); + + Double tenElementsExpected = 6.6; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void add() { + ArrayList linked = new ArrayList<>(); + linked.add(0,5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(1,"test1"); + oneElement.add(0,"test2"); + + String expectedOneElement = "test2"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0,6.6); + tenElement.add(1,4.6); + tenElement.add(2,3.6); + tenElement.add(3,39.8); + tenElement.add(4,5.7); + tenElement.add(5,3.0); + tenElement.add(6,4.7); + tenElement.add(7,12.12); + tenElement.add(8,43.1); + tenElement.add(9,1.1); + + Double tenElementsExpected = 6.6; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void get() { + + ArrayList linked = new ArrayList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.get(0); + Assertions.assertNull(actual); + }); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + Double tenElementsExpected = 1.5; + Double tenElementsActual = tenElement.get(4); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void set() { + + + ArrayList linked = new ArrayList<>(); + linked.set(0,5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0,"test1"); + oneElement.set(1,"test2"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + ArrayList tenElement = new ArrayList<>(); + tenElement.addFront(6.6); + tenElement.addFront(4.6); + tenElement.addFront(3.6); + tenElement.addFront(39.8); + tenElement.addFront(45.7); + tenElement.addFront(3.0); + tenElement.addFront(4.7); + tenElement.addFront(12.12); + tenElement.addFront(43.1); + tenElement.addFront(1.1); + tenElement.set(3,99.9); + + Double tenElementsExpected = 99.9; + Double tenElementsActual = tenElement.get(3); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + + } + + @Test + void removeFront() { + ArrayList linked = new ArrayList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.removeFront(); + Assertions.assertNull(actual); + }); + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.removeFront(); + + + Double tenElementsExpected = 1.2; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + + @Test + void removeBack() { + + ArrayList linked = new ArrayList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.removeBack(); + Assertions.assertNull(actual); + }); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + oneElement.removeBack(); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.removeBack(); + + + Double tenElementsExpected = 1.9; + Double tenElementsActual = tenElement.get(8); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void remove() { + + ArrayList linked = new ArrayList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.remove(0); + Assertions.assertNull(actual); + }); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.remove(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + Double tenElementsExpected = 1.4; + Double tenElementsActual = tenElement.remove(3); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void testRemove() { + + ArrayList linked = new ArrayList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.remove(9); + Assertions.assertNull(actual); + }); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + oneElement.remove("test1"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.remove(1.5); + + + Double tenElementsExpected = 1.5; + Double tenElementsActual = tenElement.get(4); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void contains() { + + ArrayList linked = new ArrayList<>(); + + + boolean expected = false; + boolean actual = linked.contains(5); + Assertions.assertEquals(expected, actual); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + + + boolean expectedOneElement = false; + boolean actualOneElement = oneElement.contains("nothere"); + ; + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + boolean tenElementsExpected = true; + boolean tenElementsActual = tenElement.contains(1.7); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void isEmpty() { + ArrayList linked = new ArrayList<>(); + + + boolean expected = false; + boolean actual = linked.contains(5); + Assertions.assertEquals(expected, actual); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + + + boolean expectedOneElement = false; + boolean actualOneElement = oneElement.contains("nothere"); + ; + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + boolean tenElementsExpected = false; + boolean tenElementsActual = tenElement.contains(1.7); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + + } + + @Test + void size() { + + ArrayList linked = new ArrayList<>(); + + int expected = 0; + int actual = linked.size(); + Assertions.assertEquals(expected, actual); + + + ArrayList oneElement = new ArrayList<>(); + oneElement.add(0, "test1"); + + + int expectedOneElement = 1; + int actualOneElement = oneElement.size(); + + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + ArrayList tenElement = new ArrayList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + int tenElementsExpected = 10; + int tenElementsActual = tenElement.size(); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } + + @Test + void iterator() { + } +} \ No newline at end of file diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 2bc109e..ff6b185 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -1,5 +1,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + +import java.util.Iterator; + class LinkedListTest { @Test void addFront() { @@ -17,7 +20,7 @@ void addFront() { String expectedOneElement = "test2"; String actualOneElement = oneElement.get(0); - Assertions.assertEquals(expectedOneElement,actualOneElement); + Assertions.assertEquals(expectedOneElement, actualOneElement); LinkedList tenElement = new LinkedList<>(); tenElement.addFront(6.6); @@ -34,7 +37,7 @@ void addFront() { Double tenElementsExpected = 1.1; Double tenElementsActual = tenElement.get(0); - Assertions.assertEquals(tenElementsExpected,tenElementsActual); + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @@ -55,7 +58,7 @@ void addBack() { String expectedOneElement = "test2"; String actualOneElement = oneElement.get(1); - Assertions.assertEquals(expectedOneElement,actualOneElement); + Assertions.assertEquals(expectedOneElement, actualOneElement); LinkedList tenElement = new LinkedList<>(); @@ -73,44 +76,44 @@ void addBack() { Double tenElementsExpected = 6.6; Double tenElementsActual = tenElement.get(9); - Assertions.assertEquals(tenElementsExpected,tenElementsActual); + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void add() { LinkedList linked = new LinkedList<>(); - linked.add(0,5); + linked.add(0, 5); int expected = 5; int actual = linked.get(0); Assertions.assertEquals(expected, actual); LinkedList oneElement = new LinkedList<>(); - oneElement.add(0,"test1"); - oneElement.add(1,"test2"); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); String expectedOneElement = "test2"; String actualOneElement = oneElement.get(1); - Assertions.assertEquals(expectedOneElement,actualOneElement); + Assertions.assertEquals(expectedOneElement, actualOneElement); LinkedList tenElement = new LinkedList<>(); - tenElement.add(0,1.1); - tenElement.add(1,1.2); - tenElement.add(2,1.3); - tenElement.add(3,1.4); - tenElement.add(4,1.5); - tenElement.add(5,1.6); - tenElement.add(6,1.7); - tenElement.add(7,1.8); - tenElement.add(8,1.9); - tenElement.add(9,2.0); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); Double tenElementsExpected = 1.5; Double tenElementsActual = tenElement.get(4); - Assertions.assertEquals(tenElementsExpected,tenElementsActual); + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test @@ -119,74 +122,367 @@ void get() { LinkedList linked = new LinkedList<>(); Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { - Integer expected = null; Integer actual = linked.get(0); - Assertions.assertEquals(expected, actual); + Assertions.assertNull(actual); }); LinkedList oneElement = new LinkedList<>(); - oneElement.add(0,"test1"); - oneElement.add(1,"test2"); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); oneElement.get(0); String expectedOneElement = "test1"; String actualOneElement = oneElement.get(0); - Assertions.assertEquals(expectedOneElement,actualOneElement); + Assertions.assertEquals(expectedOneElement, actualOneElement); LinkedList tenElement = new LinkedList<>(); - tenElement.add(0,1.1); - tenElement.add(1,1.2); - tenElement.add(2,1.3); - tenElement.add(3,1.4); - tenElement.add(4,1.5); - tenElement.add(5,1.6); - tenElement.add(6,1.7); - tenElement.add(7,1.8); - tenElement.add(8,1.9); - tenElement.add(9,2.0); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); Double tenElementsExpected = 1.5; Double tenElementsActual = tenElement.get(4); - Assertions.assertEquals(tenElementsExpected,tenElementsActual); + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void set() { + + + LinkedList linked = new LinkedList<>(); + linked.set(0,5); + + int expected = 5; + int actual = linked.get(0); + Assertions.assertEquals(expected, actual); + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0,"test1"); + oneElement.set(1,"test2"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + LinkedList tenElement = new LinkedList<>(); + tenElement.addFront(6.6); + tenElement.addFront(4.6); + tenElement.addFront(3.6); + tenElement.addFront(39.8); + tenElement.addFront(45.7); + tenElement.addFront(3.0); + tenElement.addFront(4.7); + tenElement.addFront(12.12); + tenElement.addFront(43.1); + tenElement.addFront(1.1); + tenElement.set(3,99.9); + + Double tenElementsExpected = 99.9; + Double tenElementsActual = tenElement.get(3); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } @Test void removeFront() { + + LinkedList linked = new LinkedList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.removeFront(); + Assertions.assertNull(actual); + }); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.removeFront(); + + + Double tenElementsExpected = 1.2; + Double tenElementsActual = tenElement.get(0); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void removeBack() { + + LinkedList linked = new LinkedList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.removeBack(); + Assertions.assertNull(actual); + }); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + oneElement.removeBack(); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.removeBack(); + + + Double tenElementsExpected = 1.9; + Double tenElementsActual = tenElement.get(8); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void remove() { + LinkedList linked = new LinkedList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.remove(0); + Assertions.assertNull(actual); + }); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.remove(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + Double tenElementsExpected = 1.4; + Double tenElementsActual = tenElement.remove(3); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void testRemove() { + LinkedList linked = new LinkedList<>(); + + Assertions.assertThrows(IndexOutOfBoundsException.class, () -> { + Integer actual = linked.remove(9); + Assertions.assertNull(actual); + }); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + oneElement.remove("test1"); + + String expectedOneElement = "test1"; + String actualOneElement = oneElement.get(0); + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + tenElement.remove(1.5); + + + Double tenElementsExpected = 1.6; + Double tenElementsActual = tenElement.get(4); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void contains() { + LinkedList linked = new LinkedList<>(); + + + boolean expected = false; + boolean actual = linked.contains(5); + Assertions.assertEquals(expected, actual); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + oneElement.add(1, "test2"); + + + boolean expectedOneElement = false; + boolean actualOneElement = oneElement.contains("nothere"); + ; + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + boolean tenElementsExpected = true; + boolean tenElementsActual = tenElement.contains(1.7); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); + } @Test void isEmpty() { + LinkedList linked = new LinkedList<>(); + + + boolean expected = false; + boolean actual = linked.contains(5); + Assertions.assertEquals(expected, actual); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + + + boolean expectedOneElement = false; + boolean actualOneElement = oneElement.contains("nothere"); + ; + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + boolean tenElementsExpected = true; + boolean tenElementsActual = tenElement.contains(1.7); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void size() { + LinkedList linked = new LinkedList<>(); + + int expected = 0; + int actual = linked.size(); + Assertions.assertEquals(expected, actual); + + + LinkedList oneElement = new LinkedList<>(); + oneElement.add(0, "test1"); + + + int expectedOneElement = 1; + int actualOneElement = oneElement.size(); + + Assertions.assertEquals(expectedOneElement, actualOneElement); + + + LinkedList tenElement = new LinkedList<>(); + tenElement.add(0, 1.1); + tenElement.add(1, 1.2); + tenElement.add(2, 1.3); + tenElement.add(3, 1.4); + tenElement.add(4, 1.5); + tenElement.add(5, 1.6); + tenElement.add(6, 1.7); + tenElement.add(7, 1.8); + tenElement.add(8, 1.9); + tenElement.add(9, 2.0); + + + int tenElementsExpected = 10; + int tenElementsActual = tenElement.size(); + + Assertions.assertEquals(tenElementsExpected, tenElementsActual); } @Test void iterator() { + +// LinkedList linked = new LinkedList<>(); +// +// Iterator iterating = linked.iterator(); +// while (iterating.hasNext()){ +// System.out.println(iterating.next()); +// } +// +// +// Assertions.assertEquals(linked.iterator(),iterating); } } \ No newline at end of file From 8b4775485c6731e3d2bf5d3916dc7bff5fe1c461 Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Sat, 17 Feb 2024 17:24:46 -0800 Subject: [PATCH 3/6] Added runtime analysis fixed some test and bugs --- src/ArrayList.java | 78 +++++++++++++++++++-------------------- src/LinkedList.java | 32 +++++++++------- src/Main.java | 24 +----------- tests/ArrayListTest.java | 9 ++--- tests/LinkedListTest.java | 14 ------- 5 files changed, 61 insertions(+), 96 deletions(-) diff --git a/src/ArrayList.java b/src/ArrayList.java index 572b9e7..fa66369 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,3 +1,6 @@ +/** + * Author: Levi Miller + */ import java.util.Arrays; import java.util.Iterator; @@ -13,52 +16,57 @@ public ArrayList(){ /** * Add item to the front. * + * Runtime analysis: the O level is O(n) so linear time + * The program has to run over the size(n) of the list + * + * * @param item the item to be added */ @Override public void addFront(E item) { - if(size == buffer.length){ - resize(buffer.length * 2); + if(size == buffer.length){ // +1 + resize(buffer.length * 2); // +1 } for (int i = size - 1; i >= 0; i--) { - buffer[i+1] = buffer[i]; + buffer[i+1] = buffer[i]; // +2 } - buffer[0] = item; - size++; + buffer[0] = item; // + 1 + size++; // + 2 } /** * Add item to the back. - * + * Runtime analysis: the O level is worst case is O(1) it is constant f(n) = n + 5 + * The program checks the size of list then adds to back of list * @param item the item to be added */ @Override public void addBack(E item) throws IllegalArgumentException{ - if(size == buffer.length){ - resize(size * 2); + if(size == buffer.length){ // + 1 + resize(size * 2); // +1 } - buffer[size] = item; - size++; + buffer[size] = item; //+ 1 + size++; // +2 } /** * Add an item at specified index (position). - * + * RunTime analysis: is constant or O(1) there are no loops * @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){ + if(i < 0){ // +1 throw new IndexOutOfBoundsException("Index can not be negative"); } - if(size == buffer.length){ - buffer[i+1] = buffer[i]; + if(size == buffer.length){ // + 1 + buffer[i+1] = buffer[i]; // + 2 } - buffer[i] = item; - size++; + buffer[i] = item; // +1 + size++; // +2 } /** * Get the item at a specified index. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @param i the index where the item should be retrieved * @return the item located at that index */ @@ -75,8 +83,8 @@ 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 + * RunTime analysis: O(1) constant no loops worst case constant + * @param i the index where the item should be saved * @param item the item to be saved * @return E item that was removed */ @@ -95,7 +103,7 @@ public E set(int i, E item) { } /** * Remove item at the front of the list. - * + * RunTime analysis: one loop so linear time worse cast O(n) * @return the item that was removed */ @Override @@ -110,7 +118,7 @@ public E removeFront() { } /** * Remove item at the back of the list - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return the item that was removed */ @Override @@ -122,7 +130,7 @@ public E removeBack() { } /** * Remove item from the list - * + * RunTime analysis: nested loop worse cast O(n^2) * @param item the item to be removed */ @Override @@ -130,16 +138,19 @@ public void remove(E item) { for (int i = 0; i < size; i++) { if(item.equals(buffer[i])){ for (int j = i; j < size; j++) { + if(size == buffer.length){ + resize(size * 2); + } buffer[j] = buffer[j + 1]; } size--; - + i--; } } } /** * Remove item at a specified index. - * + * RunTime analysis: one loop so linear time worse cast O(n) * @param i the index where the item should be removed * @return the item that was removed */ @@ -160,7 +171,7 @@ public E remove(int i) { } /** * Checks if an item is in the list. - * + * RunTime analysis: one loop so linear time worse cast O(n) * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -175,7 +186,7 @@ public boolean contains(E item) { } /** * Checks if the list is empty. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return true if the list is empty, false otherwise */ @Override @@ -184,7 +195,7 @@ public boolean isEmpty() { } /** * Provides a count of the number of items in the list. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return number of items in the list */ @Override @@ -208,7 +219,7 @@ private void resize(int newSize){ /** * Returns an iterator over elements of type {@code T}. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return an Iterator. */ @Override @@ -217,17 +228,6 @@ public Iterator iterator() { return (Iterator) theIterator; } - private class ArrayListIterator implements Iterable{ - /** - * Returns an iterator over elements of type {@code T}. - * - * @return an Iterator. - */ - @Override - public Iterator iterator() { - return null; - } - } @Override public String toString() { return "ArrayList{" + diff --git a/src/LinkedList.java b/src/LinkedList.java index 947ad27..983a8b6 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,3 +1,7 @@ +/** + * Author: Levi Miller + */ + import java.util.Iterator; public class LinkedList implements List { @@ -28,7 +32,7 @@ public LinkedList() { /** * Add item to the front. - * + * RunTime analysis: is constant or O(1) there are no loops * @param item the item to be added */ @Override @@ -52,7 +56,7 @@ public void addFront(E item) { /** * Add item to the back. - * + * RunTime analysis: one loop so linear time worse cast O(n) * @param item the item to be added */ @Override @@ -78,7 +82,7 @@ public void addBack(E item) { /** * Add an item at specified index (position). - * + * RunTime analysis: one loop so linear time worse cast O(n) * @param i the index where the item should be added * @param item the item to be added */ @@ -111,7 +115,7 @@ public void add(int i, E item) { /** * Get the item at a specified index. - * + * RunTime analysis: one loop so linear time worse cast O(n) * @param i the index where the item should be retrieved * @return the item located at that index */ @@ -119,7 +123,7 @@ public void add(int i, E item) { public E get(int i) { Node temp; E result = null; - if (i > size || i < 0) { + if (i >= size || i < 0) { throw new IndexOutOfBoundsException(); } else { temp = head; @@ -138,7 +142,7 @@ public E get(int i) { /** * Set (save) an item at a specified index. Previous * item at that index is overwritten. - * + * RunTime analysis: one loop so linear time worse case O(n) * @param i the index where the item should be saved * @param item the item to be saved * @return @@ -171,7 +175,7 @@ public E set(int i, E item) { /** * Remove item at the front of the list. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return the item that was removed */ @Override @@ -194,7 +198,7 @@ public E removeFront() { /** * Remove item at the back of the list - * + * RunTime analysis: one loop so linear time worse case O(n) * @return the item that was removed */ @Override @@ -222,7 +226,7 @@ public E removeBack() { /** * Remove item from the list - * + * RunTime analysis: one loop so linear time worse case O(n) * @param item the item to be removed */ @Override @@ -245,7 +249,7 @@ public void remove(E item) { /** * Remove item at a specified index. - * + * RunTime analysis: one loop so linear time worse case O(n) * @param i the index where the item should be removed * @return the item that was removed */ @@ -271,7 +275,7 @@ public E remove(int i) { /** * Checks if an item is in the list. - * + * RunTime analysis: one loop so linear time worse case O(n) * @param item the item to search for * @return true if the item is in the list, false otherwise */ @@ -292,7 +296,7 @@ public boolean contains(E item) { /** * Checks if the list is empty. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return true if the list is empty, false otherwise */ @Override @@ -302,7 +306,7 @@ public boolean isEmpty() { /** * Provides a count of the number of items in the list. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return number of items in the list */ @Override @@ -312,7 +316,7 @@ public int size() { /** * Returns an iterator over elements of type {@code T}. - * + * RunTime analysis: constant O(1) no loops worse case in constant * @return an Iterator. */ @Override diff --git a/src/Main.java b/src/Main.java index e89ea62..d56ae31 100644 --- a/src/Main.java +++ b/src/Main.java @@ -12,30 +12,8 @@ public static void main(String[] args) { // to see how IntelliJ IDEA suggests fixing it. System.out.println("Hello and welcome!"); - List obj = new ArrayList<>(); - obj.addBack("hi"); - obj.addBack("bye"); - obj.addFront("first"); - obj.set(1,"set"); - obj.remove("bye"); - System.out.println(obj.contains("B")); - System.out.println(obj); - - // System.out.println(obj.iterator()); - - LinkedList linked = new LinkedList<>(); - linked.addFront("hello"); - linked.addBack("dance"); - linked.add(0,"55.5"); - linked.set(2,"set"); - System.out.println(linked); - - - Iterator iterating = linked.iterator(); - while (iterating.hasNext()){ - System.out.println(iterating.next()); + } } -} \ No newline at end of file diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index e4ebd07..e10342a 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -324,7 +324,7 @@ void testRemove() { oneElement.add(1, "test2"); oneElement.remove("test1"); - String expectedOneElement = "test1"; + String expectedOneElement = "test2"; String actualOneElement = oneElement.get(0); Assertions.assertEquals(expectedOneElement, actualOneElement); @@ -340,7 +340,7 @@ void testRemove() { tenElement.add(7, 1.8); tenElement.add(8, 1.9); tenElement.add(9, 2.0); - tenElement.remove(1.5); + tenElement.remove(2.0); Double tenElementsExpected = 1.5; @@ -423,7 +423,7 @@ void isEmpty() { tenElement.add(9, 2.0); - boolean tenElementsExpected = false; + boolean tenElementsExpected = true; boolean tenElementsActual = tenElement.contains(1.7); Assertions.assertEquals(tenElementsExpected, tenElementsActual); @@ -469,7 +469,4 @@ void size() { Assertions.assertEquals(tenElementsExpected, tenElementsActual); } - @Test - void iterator() { - } } \ No newline at end of file diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index ff6b185..147ff39 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -471,18 +471,4 @@ void size() { Assertions.assertEquals(tenElementsExpected, tenElementsActual); } - - @Test - void iterator() { - -// LinkedList linked = new LinkedList<>(); -// -// Iterator iterating = linked.iterator(); -// while (iterating.hasNext()){ -// System.out.println(iterating.next()); -// } -// -// -// Assertions.assertEquals(linked.iterator(),iterating); - } } \ No newline at end of file From e934d929e5e88a2a623c5568a4cf4fd5d37cbc79 Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Mon, 19 Feb 2024 15:53:35 -0800 Subject: [PATCH 4/6] Adding data structures finished resizingArrayStack, LinkedStack and bag interface --- src/Bag.java | 26 +++++++++++++ src/LinkedQueue.java | 2 + src/LinkedStack.java | 73 ++++++++++++++++++++++++++++++++++ src/ResizingArrayStack.java | 78 +++++++++++++++++++++++++++++++++++++ src/StackTestClient.java | 20 ++++++++++ 5 files changed, 199 insertions(+) create mode 100644 src/Bag.java create mode 100644 src/LinkedQueue.java create mode 100644 src/LinkedStack.java create mode 100644 src/ResizingArrayStack.java create mode 100644 src/StackTestClient.java diff --git a/src/Bag.java b/src/Bag.java new file mode 100644 index 0000000..de2dcbe --- /dev/null +++ b/src/Bag.java @@ -0,0 +1,26 @@ +/** + * Author : Levi Miller + */ +/** + * Collect and iterate over items Bag API + * @param > class / data type of the items in the bag + */ +public interface Bag extends Iterable { + /** + * Add an item to the bag. + * @param item the item to be added + */ + void add(Item 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 stack. + * @return the number of items in the stack + */ + int size(); +} diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java new file mode 100644 index 0000000..28aa148 --- /dev/null +++ b/src/LinkedQueue.java @@ -0,0 +1,2 @@ +public class LinkedQueue { +} diff --git a/src/LinkedStack.java b/src/LinkedStack.java new file mode 100644 index 0000000..9d34f28 --- /dev/null +++ b/src/LinkedStack.java @@ -0,0 +1,73 @@ +/** + * Author : Levi Miller + */ +import java.util.Iterator; +public class LinkedStack implements Stack { + + private Node head; // top of stack (most recently added node) + private int size; // number of items + + + private class Node { // nested class to define nodes + E item; + Node next; + } + + public boolean isEmpty() { + return head == null; + } // Or: N == 0. + + public int size() { + return size; + } + + public void push(E item) { // Add item to top of stack. + Node oldfirst = head; + head = new Node(); + head.item = item; + head.next = oldfirst; + size++; + } + + public E pop() { // Remove item from top of stack. + E item = head.item; + head = head.next; + size--; + return item; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + private Node current = head; + + public boolean hasNext() { + return current != null; + } + + public void remove() { + } + + public E next() { + E item = current.item; + current = current.next; + 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 null; + } + +} + + diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java new file mode 100644 index 0000000..bbcf563 --- /dev/null +++ b/src/ResizingArrayStack.java @@ -0,0 +1,78 @@ +/** + * Author : Levi Miller + */ +import java.util.Iterator; + +public class ResizingArrayStack implements Stack { + // fields + private E[] buffer; // stack items + private int size; // number of items + + // constructor initializing fields + public ResizingArrayStack() { + this.buffer = (E[]) new Object[1]; + this.size = 0; + } + + public boolean isEmpty() { + return size == 0; + } + + 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] = buffer[i]; + buffer = temp; + } + + public void push(E item) { // Add item to top of stack. + if (size == buffer.length) resize(2 * buffer.length); + buffer[size] = item; + size++; + // or buffer[size++] = item; + + } + + public E pop() { // Remove item from top of stack. + size--; + E item = buffer[size]; // or E item = buffer[size--]; + buffer[size] = null; // Avoid loitering (see text). + if (size > 0 && size == buffer.length / 4) resize(buffer.length / 2); + return item; + } + + @Override + public E peek() { + return null; + } + + public Iterator iterator() { + return new ReverseArrayIterator(); + } + + private class ReverseArrayIterator implements Iterator { // Support LIFO iteration. + private int i = size; + + public boolean hasNext() { + return i > 0; + } + + public E next() { + + i--; + return buffer[i]; // or buffer[i--] + + + } + + public void remove() { + } + } + +} + + diff --git a/src/StackTestClient.java b/src/StackTestClient.java new file mode 100644 index 0000000..253aec9 --- /dev/null +++ b/src/StackTestClient.java @@ -0,0 +1,20 @@ +/** + * Author : Levi Miller + */ +import java.util.Scanner; +public class StackTestClient { + + public static void main(String[] args) { + Stack s = new LinkedStack<>(); + 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 0ad3c4173274361f36062478a22dba4f82ae1cc2 Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Mon, 19 Feb 2024 16:25:09 -0800 Subject: [PATCH 5/6] Added linked queue and queue client test --- src/LinkedQueue.java | 68 +++++++++++++++++++++++++++++++++++++++- src/QueueTestClient.java | 16 ++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 src/QueueTestClient.java diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 28aa148..80b5ae7 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,2 +1,68 @@ -public class LinkedQueue { +import java.util.Iterator; + +public class LinkedQueue implements Queue { + private Node head; // link to least recently added node + private Node tail; // link to most recently added node + private int size; // number of items on the queue + + private class Node { // nested class to define nodes + E item; + Node next; + } + + public LinkedQueue() { + this.head = head; + this.tail = tail; + this.size = size; + } + + public boolean isEmpty() { + return head == null; + } // Or: N == 0. + + public int size() { + return size; + } + + public void enqueue(E item) { // Add item to the end of the list. + Node oldlast = tail; + tail = new Node(); + tail.item = item; + tail.next = null; + if (isEmpty()) head = tail; + else oldlast.next = tail; + size++; + } + + public E dequeue() { // Remove item from the beginning of the list. + E item = head.item; + head = head.next; + if (isEmpty()) tail = null; + size--; + return item; + } + + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + private Node current = head; + + public boolean hasNext() { + return current != null; + } + + public void remove() { + } + + public E next() { + E item = current.item; + current = current.next; + return item; + } + } + + // See page 150 for test client main(). } + diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java new file mode 100644 index 0000000..47e8216 --- /dev/null +++ b/src/QueueTestClient.java @@ -0,0 +1,16 @@ +import java.util.Scanner; +public class QueueTestClient { + public static void main(String[] args) + { // Create a queue and enqueue/dequeue strings. + 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.print(q.dequeue() + " "); + } + System.out.print(("(" + q.size() + " left on queue)")); + } +} From a7895546fcff8eb35d7d6d24fd59eb7b9661de06 Mon Sep 17 00:00:00 2001 From: LeviAdamMiller Date: Tue, 20 Feb 2024 11:35:52 -0800 Subject: [PATCH 6/6] Added headings and took care of warnings --- src/ArrayList.java | 16 +++---- src/Bag.java | 12 +++-- src/Deque.java | 4 ++ src/LinkedBag.java | 88 +++++++++++++++++++++++++++++++++++++ src/LinkedList.java | 35 +++++++-------- src/LinkedQueue.java | 55 ++++++++++++++++++++--- src/LinkedStack.java | 59 ++++++++++++++++++++----- src/List.java | 7 ++- src/MathSet.java | 1 - src/Queue.java | 4 ++ src/QueueTestClient.java | 11 +++-- src/ResizingArrayStack.java | 56 ++++++++++++++++++++--- src/Stack.java | 4 ++ src/StackTestClient.java | 3 ++ src/Stats.java | 35 +++++++++++++++ tests/ArrayListTest.java | 8 ++-- tests/LinkedListTest.java | 10 ++--- 17 files changed, 338 insertions(+), 70 deletions(-) create mode 100644 src/LinkedBag.java create mode 100644 src/Stats.java diff --git a/src/ArrayList.java b/src/ArrayList.java index fa66369..eb1cafe 100644 --- a/src/ArrayList.java +++ b/src/ArrayList.java @@ -1,5 +1,6 @@ /** * Author: Levi Miller + * File: ArrayList.java implements arraylist */ import java.util.Arrays; import java.util.Iterator; @@ -15,11 +16,8 @@ public ArrayList(){ } /** * Add item to the front. - * * Runtime analysis: the O level is O(n) so linear time * The program has to run over the size(n) of the list - * - * * @param item the item to be added */ @Override @@ -84,12 +82,12 @@ public E get(int i) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * RunTime analysis: O(1) constant no loops worst case constant - * @param i the index where the item should be saved + * + * @param i the index where the item should be saved * @param item the item to be saved - * @return E item that was removed */ @Override - public E set(int i, E item) { + public void set(int i, E item) { if (i < 0) { throw new IndexOutOfBoundsException("Index can not be negative"); } @@ -99,7 +97,6 @@ public E set(int i, E item) { E removed = buffer[i]; buffer[i] = item; size++; - return removed; } /** * Remove item at the front of the list. @@ -205,7 +202,10 @@ public int size() { } return size; } - + /** + * RunTime analysis: one loop so linear time worse cast O(n) + * resizes array when the array is full and copy over previous array + * */ private void resize(int newSize){ // create a new space separate from the old buffer E[] temp = (E[]) new Object[(int) newSize]; diff --git a/src/Bag.java b/src/Bag.java index de2dcbe..cae104f 100644 --- a/src/Bag.java +++ b/src/Bag.java @@ -1,26 +1,32 @@ /** * Author : Levi Miller + * file: Bag.java, Bag interface */ + /** * Collect and iterate over items Bag API + * * @param > class / data type of the items in the bag */ -public interface Bag extends Iterable { +public interface Bag extends Iterable { /** * Add an item to the bag. + * * @param item the item to be added */ - void add(Item item); + void add(Item 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 stack. - * @return the number of items in the stack + * + * @return the number of items in the bag */ int size(); } diff --git a/src/Deque.java b/src/Deque.java index 0107f47..d9f9352 100644 --- a/src/Deque.java +++ b/src/Deque.java @@ -1,3 +1,7 @@ +/** + * Author : Levi Miller + * file: Deque, deque interface + */ /** * Deque: double-ended queue API * Supports adding and removing items at both ends. diff --git a/src/LinkedBag.java b/src/LinkedBag.java new file mode 100644 index 0000000..b08d0eb --- /dev/null +++ b/src/LinkedBag.java @@ -0,0 +1,88 @@ +/** + * Author : Levi Miller + * file: LinkedBag.java, implements bag interface + */ +import java.util.Iterator; + +public class LinkedBag implements Bag { + private Node first; // first node in list + private int size; + + public class Node { + E item; + Node next; + } + /** + * default constructor: initializing fields + * RunTime analysis: constant O(1) no loops worse case in constant + */ + public LinkedBag() { + this.first = null; + this.size = 0; + } + + /** + * Adds an item to the front of the bag + * RunTime analysis: constant O(1) no loops worse case in constant + */ + public void add(E item) { // same as push() in Stack + Node first = this.first; + this.first = new Node(); + this.first.item = item; + this.first.next = first; + size++; + } + + /** + * Checks to see if the bag is empty. + * RunTime analysis: constant O(1) no loops worse case in constant + * + * @return true if the bag is empty, false otherwise + */ + @Override + public boolean isEmpty() { + return size == 0; + } + + /** + * Returns a count of the number of items in the stack. + * RunTime analysis: constant O(1) no loops worse case in constant + * + * @return the number of items in the bag + */ + @Override + public int size() { + return size; + } + + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns items from bag + */ + public Iterator iterator() { + return new ListIterator(); + } + + private class ListIterator implements Iterator { + private Node current = first; + + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns if there is a next element in bag is ,so it's true if not it's false + */ + public boolean hasNext() { + return current != null; + } + + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns next element in list + */ + public E next() { + E item = current.item; + current = current.next; + return item; + + } + } +} diff --git a/src/LinkedList.java b/src/LinkedList.java index 983a8b6..b573298 100644 --- a/src/LinkedList.java +++ b/src/LinkedList.java @@ -1,7 +1,8 @@ /** * Author: Levi Miller + * file: LinkedList.java, implements Linked-list interface + * only errors are from unchecked cast */ - import java.util.Iterator; public class LinkedList implements List { @@ -23,7 +24,10 @@ public String toString() { // set up the size field private int size; - // add a constructor to initialize the fields + /** + * default constructor: initializing fields + * RunTime analysis: constant O(1) no loops worse case in constant + */ public LinkedList() { head = null; @@ -41,17 +45,14 @@ public void addFront(E item) { Node theNewOne = new Node(); theNewOne.data = item; - if (head == null) { - //this list is currently empty - head = theNewOne; - size++; - } else { + if (head != null) { //this list currently has some nodes in it theNewOne.next = head; //everything from the right gets saved to the left - head = theNewOne; - size++; - } + } //this list is currently empty + + head = theNewOne; + size++; } /** @@ -63,10 +64,7 @@ public void addFront(E item) { public void addBack(E item) { Node theNewOne = new Node(); theNewOne.data = item; - if (size == 0) { - head = theNewOne; - size++; - } else { + if (size != 0) { // keeps track of where you are in the list Node temp = head; // if temp. next is not null @@ -76,8 +74,10 @@ public void addBack(E item) { } // if temp.next is null then theNewOne is temp.next temp.next = theNewOne; - size++; + } else { + head = theNewOne; } + size++; } /** @@ -143,12 +143,12 @@ public E get(int i) { * Set (save) an item at a specified index. Previous * item at that index is overwritten. * RunTime analysis: one loop so linear time worse case O(n) + * * @param i the index where the item should be saved * @param item the item to be saved - * @return */ @Override - public E set(int i, E item) { + public void set(int i, E item) { Node theNewOne = new Node(); theNewOne.data = item; if (i > size) { @@ -170,7 +170,6 @@ public E set(int i, E item) { } } size++; - return null; } /** diff --git a/src/LinkedQueue.java b/src/LinkedQueue.java index 80b5ae7..6c1bd50 100644 --- a/src/LinkedQueue.java +++ b/src/LinkedQueue.java @@ -1,3 +1,7 @@ +/** + * Author: Levi Miller + * file: LinkedQueue.java, implements Queue interface + */ import java.util.Iterator; public class LinkedQueue implements Queue { @@ -10,30 +14,54 @@ private class Node { // nested class to define nodes Node next; } + /** + * default constructor: initializing fields + * RunTime analysis: constant O(1) no loops worse case in constant + */ public LinkedQueue() { - this.head = head; - this.tail = tail; - this.size = size; + this.head = null; + this.tail = null; + this.size = 0; } + /** + * Checks if the queue is empty. + * RunTime analysis: constant O(1) no loops worse case in constant + * + * @return true if the list is empty, false otherwise + */ public boolean isEmpty() { return head == null; } // Or: N == 0. + /** + * Provides a count of the number of items in the queue + * RunTime analysis: constant O(1) no loops worse case in constant + * + * @return number of items in the queue + */ public int size() { return size; } + /** + * Add an item at the end of the queue + * RunTime analysis: constant O(1) no loops worse case in constant + */ public void enqueue(E item) { // Add item to the end of the list. - Node oldlast = tail; + Node outlast = tail; tail = new Node(); tail.item = item; tail.next = null; if (isEmpty()) head = tail; - else oldlast.next = tail; + else outlast.next = tail; size++; } + /** + * removes an item at the front of the queue + * RunTime analysis: constant O(1) no loops worse case in constant + */ public E dequeue() { // Remove item from the beginning of the list. E item = head.item; head = head.next; @@ -42,6 +70,10 @@ public E dequeue() { // Remove item from the beginning of the list. return item; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns iterated nodes + */ public Iterator iterator() { return new ListIterator(); } @@ -49,20 +81,29 @@ public Iterator iterator() { private class ListIterator implements Iterator { private Node current = head; + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns if there is a next element in queue is ,so it's true if not it's false + */ public boolean hasNext() { return current != null; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + */ public void remove() { } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns next element in list + */ public E next() { E item = current.item; current = current.next; return item; } } - - // See page 150 for test client main(). } diff --git a/src/LinkedStack.java b/src/LinkedStack.java index 9d34f28..b452ded 100644 --- a/src/LinkedStack.java +++ b/src/LinkedStack.java @@ -1,34 +1,61 @@ /** * Author : Levi Miller + * file: LinkedStack.java, implements LinkedStack interface */ import java.util.Iterator; public class LinkedStack implements Stack { private Node head; // top of stack (most recently added node) - private int size; // number of items + private int size; - - private class Node { // nested class to define nodes + public class Node { // nested class to define nodes E item; Node next; } + /** + * default constructor: initializing fields + * RunTime analysis: constant O(1) no loops worse case in constant + */ + public LinkedStack() { + this.head = null; + this.size = 0; + } + + /** + * Checks if the stack is empty. + * RunTime analysis: constant O(1) no loops worse case in constant + * @return true if the list is empty, false otherwise + */ public boolean isEmpty() { return head == null; } // Or: N == 0. + /** + * Provides a count of the number of items in the stack + * RunTime analysis: constant O(1) no loops worse case in constant + * @return number of items in the stack + */ public int size() { return size; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * push item on stack + * */ public void push(E item) { // Add item to top of stack. - Node oldfirst = head; + Node first = head; head = new Node(); head.item = item; - head.next = oldfirst; + head.next = first; size++; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * pop item off stack + * */ public E pop() { // Remove item from top of stack. E item = head.item; head = head.next; @@ -36,20 +63,33 @@ public E pop() { // Remove item from top of stack. return item; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns iterated nodes + * */ public Iterator iterator() { return new ListIterator(); } private class ListIterator implements Iterator { private Node current = head; - + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns if there is a next element in stack is ,so it's true if not it's false + * */ public boolean hasNext() { return current != null; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * */ public void remove() { } - + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns next element in list + * */ public E next() { E item = current.item; current = current.next; @@ -58,10 +98,7 @@ public E next() { } /** - * 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. + * RunTime analysis: no loop so constant time worse cast O(1) */ @Override public E peek() { diff --git a/src/List.java b/src/List.java index 4c7ad88..5691b1a 100644 --- a/src/List.java +++ b/src/List.java @@ -1,3 +1,7 @@ +/** + * Author : Levi Miller + * file: List.java, list interface + */ /*** * List interface (API / abstract data type) * @param Class or data type of the items in the list. @@ -35,9 +39,8 @@ public interface List extends Iterable { * * @param i the index where the item should be saved * @param item the item to be saved - * @return */ - E set(int i, E item); + void set(int i, E item); /** * Remove item at the front of the list. diff --git a/src/MathSet.java b/src/MathSet.java index 5b87e0d..5b61a44 100644 --- a/src/MathSet.java +++ b/src/MathSet.java @@ -2,7 +2,6 @@ * MathSet API (interface / abstract data type) * represents a mathematical set. Sets in mathematics * have unique elements (keys) and there are no duplicate keys. - * * In this MathSet API, we have an additional constraint that * traditional mathematical sets do not have. In mathematical sets, * elements are unordered, order of keys does not matter. In this diff --git a/src/Queue.java b/src/Queue.java index ab5ca29..491f41b 100644 --- a/src/Queue.java +++ b/src/Queue.java @@ -1,3 +1,7 @@ +/** + * Author : Levi Miller + * file: Queue.java, Queue interface + */ /** * FIFO (first-in, first-out) Queue API * @param class / data type of the items in the queue diff --git a/src/QueueTestClient.java b/src/QueueTestClient.java index 47e8216..226ab43 100644 --- a/src/QueueTestClient.java +++ b/src/QueueTestClient.java @@ -1,11 +1,14 @@ +/** + * Author : Levi Miller + * file: QueueTestClient.java, Tests queues + */ import java.util.Scanner; + public class QueueTestClient { - public static void main(String[] args) - { // Create a queue and enqueue/dequeue strings. + public static void main(String[] args) { // Create a queue and enqueue/dequeue strings. Queue q = new LinkedQueue<>(); Scanner in = new Scanner("to be or not to - be - - that - - - is"); - while (in.hasNext()) - { + while (in.hasNext()) { String item = in.next(); if (!item.equals("-")) q.enqueue(item); diff --git a/src/ResizingArrayStack.java b/src/ResizingArrayStack.java index bbcf563..0ac7ce0 100644 --- a/src/ResizingArrayStack.java +++ b/src/ResizingArrayStack.java @@ -1,6 +1,9 @@ /** * Author : Levi Miller + * file: ResizingArrayStack.java implements stack interface + * only errors are from casting and Resizing isn't being used in the test client per instruction */ + import java.util.Iterator; public class ResizingArrayStack implements Stack { @@ -8,27 +11,46 @@ public class ResizingArrayStack implements Stack { private E[] buffer; // stack items private int size; // number of items - // constructor initializing fields + /** + * default constructor: initializing fields + * RunTime analysis: constant O(1) no loops worse case in constant + */ public ResizingArrayStack() { - this.buffer = (E[]) new Object[1]; + this.buffer = ((E[]) new Object[10]); this.size = 0; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns if array is empty + */ public boolean isEmpty() { return size == 0; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns size + */ public int size() { return size; } + + /** + * RunTime analysis: one loop so linear time worse cast O(n) + * resizes array when the array is full and copy over previous array + */ 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] = buffer[i]; + if (size >= 0) System.arraycopy(buffer, 0, temp, 0, size); buffer = temp; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * push item on stack and checks if array is full + */ public void push(E item) { // Add item to top of stack. if (size == buffer.length) resize(2 * buffer.length); buffer[size] = item; @@ -37,6 +59,10 @@ public void push(E item) { // Add item to top of stack. } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * pop item off stack and checks if array is not utilising the space + */ public E pop() { // Remove item from top of stack. size--; E item = buffer[size]; // or E item = buffer[size--]; @@ -45,30 +71,46 @@ public E pop() { // Remove item from top of stack. return item; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + */ @Override public E peek() { return null; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns reversed array + */ public Iterator iterator() { return new ReverseArrayIterator(); } + private class ReverseArrayIterator implements Iterator { // Support LIFO iteration. private int i = size; + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns if there is a next element in array + */ public boolean hasNext() { return i > 0; } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + * returns next element in array and decrement to index + */ public E next() { - i--; return buffer[i]; // or buffer[i--] - - } + /** + * RunTime analysis: no loop so constant time worse cast O(1) + */ public void remove() { } } diff --git a/src/Stack.java b/src/Stack.java index bb1000d..dd8730c 100644 --- a/src/Stack.java +++ b/src/Stack.java @@ -1,3 +1,7 @@ +/** + * Author : Levi Miller + * file: Stack.java, stack interface + */ /** * Stack (LIFO: last-in, first-out) API * @param class / data type of the items in the stack diff --git a/src/StackTestClient.java b/src/StackTestClient.java index 253aec9..0bb498f 100644 --- a/src/StackTestClient.java +++ b/src/StackTestClient.java @@ -1,7 +1,10 @@ /** * Author : Levi Miller + * file: StackTestClient.java testing out stack implementations */ + import java.util.Scanner; + public class StackTestClient { public static void main(String[] args) { diff --git a/src/Stats.java b/src/Stats.java new file mode 100644 index 0000000..aed613e --- /dev/null +++ b/src/Stats.java @@ -0,0 +1,35 @@ +/** + * Author : Levi Miller + * file: Stats.java testing out bag implementation + */ +import java.util.Scanner; + +public class Stats { + + public static void main(String[] args) { + + Scanner in = new Scanner("90 101 81 109 107 98 120 101 99 100"); + + Bag numbers = new LinkedBag<>(); + while (in.hasNextDouble()) { + numbers.add(in.nextDouble()); + } + System.out.println("% java Stats"); + for (double x : numbers) { + String noZero = String.format("%.0f", x); + System.out.println(noZero); + } + int size = numbers.size(); + double sum = 0.0; + for (double x : numbers) + sum += x; + double mean = sum / size; + sum = 0.0; + for (double x : numbers) + sum += (x - mean) * (x - mean); + double std = Math.sqrt(sum / (size - 1)); + System.out.println(); + System.out.printf("Mean: %.2f\n", mean); + System.out.printf("Std dev: %.2f\n", std); + } +} diff --git a/tests/ArrayListTest.java b/tests/ArrayListTest.java index e10342a..59b69fb 100644 --- a/tests/ArrayListTest.java +++ b/tests/ArrayListTest.java @@ -366,8 +366,8 @@ void contains() { boolean expectedOneElement = false; - boolean actualOneElement = oneElement.contains("nothere"); - ; + boolean actualOneElement = oneElement.contains("other"); + Assertions.assertEquals(expectedOneElement, actualOneElement); @@ -405,8 +405,8 @@ void isEmpty() { boolean expectedOneElement = false; - boolean actualOneElement = oneElement.contains("nothere"); - ; + boolean actualOneElement = oneElement.contains("other"); + Assertions.assertEquals(expectedOneElement, actualOneElement); diff --git a/tests/LinkedListTest.java b/tests/LinkedListTest.java index 147ff39..966bab5 100644 --- a/tests/LinkedListTest.java +++ b/tests/LinkedListTest.java @@ -1,7 +1,7 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.util.Iterator; + class LinkedListTest { @Test @@ -370,8 +370,8 @@ void contains() { boolean expectedOneElement = false; - boolean actualOneElement = oneElement.contains("nothere"); - ; + boolean actualOneElement = oneElement.contains("other"); + Assertions.assertEquals(expectedOneElement, actualOneElement); @@ -410,8 +410,8 @@ void isEmpty() { boolean expectedOneElement = false; - boolean actualOneElement = oneElement.contains("nothere"); - ; + boolean actualOneElement = oneElement.contains("other"); + Assertions.assertEquals(expectedOneElement, actualOneElement);