diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..1f58a0f --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +generics \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..c20d346 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__junit_junit_4_12.xml b/.idea/libraries/Maven__junit_junit_4_12.xml new file mode 100644 index 0000000..d411041 --- /dev/null +++ b/.idea/libraries/Maven__junit_junit_4_12.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml new file mode 100644 index 0000000..f58bbc1 --- /dev/null +++ b/.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d30d09e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,13 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..ca63f44 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..91fa1a3 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,841 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + size + length + length() + contains + capacity + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1520709799727 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/generics.iml b/generics.iml new file mode 100644 index 0000000..9717c26 --- /dev/null +++ b/generics.iml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java new file mode 100644 index 0000000..91f1dcc --- /dev/null +++ b/src/main/java/MyArrayList.java @@ -0,0 +1,179 @@ +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Logger; + +public class MyArrayList { + + private static final Logger logger = Logger.getGlobal(); + + private E[] myArrayList; + // where size = number of elements in array; size != array's length + private int size = 0; + + public MyArrayList() { + this.myArrayList = (E[]) new Object[10]; + } + + public MyArrayList(Collection collection) { + this.myArrayList = (E[]) collection.toArray(); + if (myArrayList.length == 0) { + size = 0; + } else { + for (int i = 0; i < myArrayList.length; i++) { + if (myArrayList[i] == null) { + size = i; + break; + } + } + } + } + + public MyArrayList(int initialCapacity) throws IllegalArgumentException { + if (initialCapacity < 0) { + throw new IllegalArgumentException("The array's initial capacity cannot be less than zero."); + } else { + this.myArrayList = (E[]) new Object[initialCapacity]; + } + } + + public E[] getMyArrayList() { + return myArrayList; + } + + public boolean add(E element) { + ensureCapacity(); + myArrayList[size] = element; + size++; + return true; + } + + public void add(int index, E element) { + if (index < 0) { + throw new ArrayIndexOutOfBoundsException(index); + } + ensureCapacity(); + if (myArrayList.length > 1) { + shiftElementsFromIndexToSize(index); + } + myArrayList[index] = element; + size++; + } + + public E get(int index){ + return myArrayList[index]; + } + + public E remove(int index) { + if (index < 0 || index >= myArrayList.length) { + if (capacity() == 0) { + logger.info("Array length is zero. There are no elements to delete in this array."); + } else { + logger.info("Index out of bounds: " + index); + } + throw new ArrayIndexOutOfBoundsException(index); + } + E elementRemoved = myArrayList[index]; + if (! (index == size - 1)) { + shiftElementsFromSizeToIndex(index); + } + myArrayList[size - 1] = null; + size--; + return elementRemoved; + } + + public void remove(E element) { + this.remove(this.indexOf(element)) ; + } + + public E set(int index, E element) { + E elementPreviouslyAtIndex = myArrayList[index]; + if (index < 0 || index >= myArrayList.length) { + throw new ArrayIndexOutOfBoundsException(index); + } + myArrayList[index] = element; + return elementPreviouslyAtIndex; + } + + public void clear() { + this.myArrayList = (E[]) new Object[0]; + this.size = 0; + } + + public boolean isEmpty() { + if (myArrayList.length == 0) { + return true; + } else { + return false; + } + } + + public boolean contains(E elementToFind) { + if (isEmpty()) { + return false; + } + for (E elementInList : myArrayList) { + if (elementInList == elementToFind) { + return true; + } + } + return false; + } + + public int capacity() { + return myArrayList.length; + } + + public int size() { + return size; + } + + public void ensureCapacity() { + if (myArrayList.length == 0) { + myArrayList = (E[]) new Object[1]; + } else if (size == myArrayList.length) { + doubleArrayCapacity(); + } + } + + public void doubleArrayCapacity() { + E[] arrayWithDoubleCapacity = (E[]) new Object[size * 2]; + System.arraycopy(myArrayList, 0, arrayWithDoubleCapacity, 0, size); + myArrayList = arrayWithDoubleCapacity; + } + + public void shiftElementsFromIndexToSize(int index) { + for (int i = size; i > index; i--) { + myArrayList[i] = myArrayList[i - 1]; + } + } + + public void shiftElementsFromSizeToIndex(int index) { + for (int i = index; i < size - 1; i++) { + myArrayList[i] = myArrayList[i + 1]; + } + } + + public Object[] toArray() { + return Arrays.copyOf(myArrayList, size); + } + + public int indexOf(E element) { + + if (element == null) { + for (int i = 0; i < this.myArrayList.length; i++) { + if (this.myArrayList[i] == element) { + return i; + } + } + } + + for (int i = 0; i < this.myArrayList.length; i++) { + if (this.myArrayList[i].equals(element)) { + return i; + } + } + + return -1; + } +} diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java new file mode 100644 index 0000000..bc0881f --- /dev/null +++ b/src/main/java/MySet.java @@ -0,0 +1,93 @@ +import java.util.Collection; +import java.util.Iterator; + +public class MySet { + + private MyArrayList myArrayList; + + public MySet() { + this.myArrayList = new MyArrayList<>(); + } + + public MySet(Collection collection) { + this.myArrayList = new MyArrayList<>(collection); + } + + public MySet(int initialCapacity) { + this.myArrayList = new MyArrayList<>(initialCapacity); + } + + public boolean add(E element) { + if (myArrayList.isEmpty()) { + myArrayList.add(element); + } else if (! myArrayList.contains(element)) { + myArrayList.add(element); + } + return true; + } + + // Could not get this method to work +// public boolean addAll(Collection collection) { +// for (E element : collection) { +// myArrayList.add(element); +// } +// return true; +// } + + public void clear() { + this.myArrayList.clear(); + } + + public boolean contains(E elementToFind) { + return this.myArrayList.contains(elementToFind); + } + +// public boolean containsAll(Collection collection) { +// return false; +// } + +// public boolean equals(Object object) { +// return false; +// } + +// public int hashCode() { +// return 0; +// } + + public boolean isEmpty() { + return this.myArrayList.isEmpty(); + } + +// public Iterator iterator() { +// return null; +// } + + public void remove(E element) { + this.myArrayList.remove(element); + } + +// public boolean removeAll(Collection collection) { +// return false; +// } + +// public boolean retainAll(Collection collection) { +// return false; +// } + + public int size() { + return myArrayList.size(); + } + + public int capacity() { + return myArrayList.capacity(); + } + + public Object[] toArray() { + return myArrayList.toArray(); + } + +// public T[] toArray(T[] a) { +// return a.toArray(); +// } + +} diff --git a/src/test/java/MyArrayListTest.java b/src/test/java/MyArrayListTest.java new file mode 100644 index 0000000..2900e30 --- /dev/null +++ b/src/test/java/MyArrayListTest.java @@ -0,0 +1,248 @@ +import org.junit.Test; +import org.junit.Assert; + +public class MyArrayListTest { + + private MyArrayList mal; + + @Test + public void MyArrayListDefaultConstructorTest() { + // Given + int expectedArrayLength = 10; + mal = new MyArrayList<>(); + // When + int actualArrayLength = mal.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void MyArrayListConstructorInitialCapacityTest() { + // Given + int expectedArrayLength = 0; + mal = new MyArrayList<>(expectedArrayLength); + // When + int actualArrayLength = mal.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void addElementTest() { + // Given + int expectedIntAtIndex0 = 0; + int expectedIntAtIndex1 = 1; + int startingLength = 0; + // When + mal = new MyArrayList<>(startingLength); + mal.add(expectedIntAtIndex0); + mal.add(expectedIntAtIndex1); + int actualIntAtIndex1 = mal.get(1); + // Then + Assert.assertEquals(expectedIntAtIndex1, actualIntAtIndex1); + } + + @Test + public void addIndexElementTest() { + // Given + int expectedIndex0 = 0; + int expectedIntAtIndex0 = 0; + int expectedIndex1 = 1; + int expectedIntAtIndex1 = 1; + int startingLength = 3; + // When + mal = new MyArrayList<>(startingLength); + mal.add(expectedIndex0, expectedIntAtIndex0); + mal.add(expectedIndex1, expectedIntAtIndex1); + int actualIntAtIndex1 = mal.get(1); + // Then + Assert.assertEquals(expectedIntAtIndex1, actualIntAtIndex1); + } + + @Test + public void getTest() { + // Given + int expectedArrayLength = 1; + mal = new MyArrayList<>(1); + // When + int actualArrayLength = mal.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void removeElementWhenArrayLengthIsZeroExceptionTest() throws ArrayIndexOutOfBoundsException { + // Given + int startingLength = 0; + // When + mal = new MyArrayList<>(startingLength); + mal.remove(0); + } + + @Test + public void removeElementWhenArrayLengthIsOneTest() { + // Given + int originalIntAtIndex0 = 0; + int startingLength = 1; + Integer expectedIntAtIndex0 = null; + // When + mal = new MyArrayList<>(startingLength); + mal.add(originalIntAtIndex0); + mal.remove(0); + Integer actualIntAtIndex0 = mal.get(0); + // Then + Assert.assertEquals(expectedIntAtIndex0, actualIntAtIndex0); + } + + @Test + public void removeElementWhenItsIndexIsSizeMinusOneTest() { + // Given + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int originalIntAtIndex2 = 2; + int startingLength = 0; + Integer expectedIntAtIndex2 = null; + // When + mal = new MyArrayList<>(startingLength); + mal.add(originalIntAtIndex0); + mal.add(originalIntAtIndex1); + mal.add(originalIntAtIndex2); + mal.remove(2); + Integer actualIntAtIndex1 = mal.get(2); + // Then + Assert.assertEquals(expectedIntAtIndex2, actualIntAtIndex1); + } + + @Test + public void removeElementWhenArrayLengthIsThreeTest() { + // Given + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int originalIntAtIndex2 = 2; + int startingLength = 1; + int expectedIntAtIndex1 = originalIntAtIndex2; + // When + mal = new MyArrayList<>(startingLength); + mal.add(originalIntAtIndex0); + mal.add(originalIntAtIndex1); + mal.add(originalIntAtIndex2); + mal.remove(1); + int actualIntAtIndex1 = mal.get(1); + // Then + Assert.assertEquals(expectedIntAtIndex1, actualIntAtIndex1); + } + + @Test + public void setTest() { + // Given + int expectedArrayLength = 1; + int expectedInteger = 7; + mal = new MyArrayList<>(1); + // When + mal.set(0, expectedInteger); + int actualInteger = mal.get(0); + // Then + Assert.assertEquals(expectedInteger, actualInteger); + } + + @Test + public void clearTest() { + // Given + int expectedArrayLength = 0; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mal = new MyArrayList<>(startingArrayLength); + // When + mal.add(originalIntAtIndex0); + mal.add(originalIntAtIndex1); + mal.clear(); + int actualArrayLength = mal.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void isEmptyTest() { + // Given + boolean expectedIsEmpty = true; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mal = new MyArrayList<>(startingArrayLength); + // When + mal.add(originalIntAtIndex0); + mal.add(originalIntAtIndex1); + mal.clear(); + boolean actualIsEmpty = mal.isEmpty(); + // Then + Assert.assertEquals(expectedIsEmpty, actualIsEmpty); + } + + @Test + public void containsTest() { + // Given + boolean expectedContains = true; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mal = new MyArrayList<>(startingArrayLength); + // When + mal.add(originalIntAtIndex0); + mal.add(originalIntAtIndex1); + boolean actualContains = mal.contains(originalIntAtIndex1); + // Then + Assert.assertEquals(expectedContains, actualContains); + } + + @Test + public void capacityTest() { + // Given + int expectedIndex0 = 0; + int expectedIntAtIndex0 = 0; + int expectedIndex1 = 1; + int expectedIntAtIndex1 = 1; + int expectedLength = 3; + int expectedSize = 2; + // When + mal = new MyArrayList<>(expectedLength); + mal.add(expectedIndex0, expectedIntAtIndex0); + mal.add(expectedIndex1, expectedIntAtIndex1); + int actualLength = mal.capacity(); + // Then + Assert.assertEquals(expectedLength, actualLength); + } + + @Test + public void sizeTest() { + // Given + int expectedIndex0 = 0; + int expectedIntAtIndex0 = 0; + int expectedIndex1 = 1; + int expectedIntAtIndex1 = 1; + int expectedLength = 3; + int expectedSize = 2; + // When + mal = new MyArrayList<>(expectedLength); + mal.add(expectedIndex0, expectedIntAtIndex0); + mal.add(expectedIndex1, expectedIntAtIndex1); + int actualSize = mal.size(); + // Then + Assert.assertEquals(expectedSize, actualSize); + } + + @Test + public void ensureCapacityTest() { + // Given + int expectedArrayLength = 2; + int startingArrayLength = 1; + mal = new MyArrayList<>(startingArrayLength); + // When + mal.add(7); + mal.add(8); + int actualArrayLength = mal.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + +} diff --git a/src/test/java/MySetTest.java b/src/test/java/MySetTest.java new file mode 100644 index 0000000..0e38373 --- /dev/null +++ b/src/test/java/MySetTest.java @@ -0,0 +1,134 @@ +import org.junit.Assert; +import org.junit.Test; + +import java.util.Arrays; + +public class MySetTest { + + private MySet mySet; + + @Test + public void MySetDefaultConstructor() { + // Given + int expectedArrayLength = 10; + mySet = new MySet<>(); + // When + int actualArrayLength = mySet.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void MySetConstructorInitialCapacity() { + // Given + int expectedArrayLength = 0; + mySet = new MySet(expectedArrayLength); + // When + int actualArrayLength = mySet.capacity(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void addTest() { + // Given + int expectedSize = 2; + String expectedStringToAdd0 = "0"; + String expectedStringToAdd1 = "1"; + mySet = new MySet<>(); + // When + mySet.add(expectedStringToAdd0); + mySet.add(expectedStringToAdd1); + int actualSize = mySet.size(); + // Then + Assert.assertEquals(expectedSize, actualSize); + } + + // Cannot get this method/test to work +// @Test +// public void addAllTest() { +// // Given +// Integer[] startingArray = { 0, 1, 2 }; +// Integer[] arrayToAdd = { 3, 4, 5 }; +// Integer[] expectedFinalArray = { 0, 1, 2, 3, 4, 5 }; +// String expectedFinalArrayString = Arrays.toString(expectedFinalArray); +// // When +// mySet = new MySet<>(Arrays.asList(startingArray)); +// int mySetSize = mySet.size(); +//// mySet.addAll(Arrays.asList(arrayToAdd)); +// Integer[] actualFinalArray = (Integer[]) mySet.toArray(new Integer[0]); +// +// String actualFinalArrayString = Arrays.toString(actualFinalArray); +// System.out.println(mySetSize); +// System.out.println(expectedFinalArrayString); +// System.out.println(actualFinalArrayString); +// +// // Then +//// Assert.assertEquals(expectedFinalArrayString, actualFinalArrayString); +// } + + @Test + public void clearTest() { + // Given + int expectedArrayLength = 0; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mySet = new MySet(startingArrayLength); + // When + mySet.add(originalIntAtIndex0); + mySet.add(originalIntAtIndex1); + mySet.clear(); + int actualArrayLength = mySet.size(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + + @Test + public void containsTest() { + // Given + boolean expectedContains = true; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mySet = new MySet<>(startingArrayLength); + // When + mySet.add(originalIntAtIndex0); + mySet.add(originalIntAtIndex1); + boolean actualContains = mySet.contains(originalIntAtIndex1); + // Then + Assert.assertEquals(expectedContains, actualContains); + } + + + @Test + public void isEmptyTest() { + // Given + boolean expectedIsEmpty = true; + int originalIntAtIndex0 = 0; + int originalIntAtIndex1 = 1; + int startingArrayLength = 2; + mySet = new MySet<>(startingArrayLength); + // When + mySet.add(originalIntAtIndex0); + mySet.add(originalIntAtIndex1); + mySet.clear(); + boolean actualIsEmpty = mySet.isEmpty(); + // Then + Assert.assertEquals(expectedIsEmpty, actualIsEmpty); + } + @Test + public void sizeTest() { + // Given + int expectedSize = 1; + String expectedStringToAdd0 = "0"; + mySet = new MySet<>(expectedSize); + // When + mySet.add(expectedStringToAdd0); + int actualSize = mySet.size(); + // Then + Assert.assertEquals(expectedSize, actualSize); + } + + +}