From 107a3999ca176bba05413f5345f47dbcebcdd6d5 Mon Sep 17 00:00:00 2001 From: Luis Romero Date: Sat, 10 Mar 2018 14:55:58 -0500 Subject: [PATCH 01/16] starting MyArrayList and tests --- .idea/.name | 1 + .idea/compiler.xml | 16 + .idea/libraries/Maven__junit_junit_4_12.xml | 13 + .../Maven__org_hamcrest_hamcrest_core_1_3.xml | 13 + .idea/misc.xml | 13 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + .idea/workspace.xml | 358 ++++++++++++++++++ generics.iml | 16 + src/main/java/MyArrayList.java | 9 + src/test/java/MyArrayListTest.java | 10 + 11 files changed, 463 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/compiler.xml create mode 100644 .idea/libraries/Maven__junit_junit_4_12.xml create mode 100644 .idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml create mode 100644 generics.iml create mode 100644 src/main/java/MyArrayList.java create mode 100644 src/test/java/MyArrayListTest.java 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..1aa7440 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,358 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + true + DEFINITION_ORDER + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -405,74 +416,74 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + - + - + - - + + - + - + - + - + - + @@ -501,8 +512,8 @@ - - + + @@ -511,21 +522,22 @@ - - + + - - - - - - - - - - - - + + + + + + + + + + + + + diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java index 4039d17..a306a36 100644 --- a/src/main/java/MyArrayList.java +++ b/src/main/java/MyArrayList.java @@ -1,7 +1,10 @@ import java.util.Arrays; +import java.util.logging.Logger; public class MyArrayList { + private static final Logger logger = Logger.getGlobal(); + private E[] myArrayList; private int size = 0; @@ -32,16 +35,7 @@ public void add(int index, E element) { if (index < 0) { throw new ArrayIndexOutOfBoundsException(index); } - ensureCapacity(); -// if (myArrayList.length == 1) { -// myArrayList[index] = element; -// } else { -// for (int i = size; i > index; i--) { -// myArrayList[i] = myArrayList[i - 1]; -// } -// myArrayList[index] = element; -// } if (myArrayList.length > 1) { shiftElementsFromIndexToSize(index); } @@ -54,18 +48,28 @@ public E get(int index){ } public E remove(int index) { - // return the element that was removed - return null; + if (index < 0 || index >= myArrayList.length) { + logger.info("Array length is zero. There are no elements to delete in this array."); + throw new ArrayIndexOutOfBoundsException(index); + } + E elementRemoved = myArrayList[index]; + if ( (myArrayList.length == 1 && index == 0) || (index == size - 1) ) { + myArrayList[index] = null; + } else { + shiftElementsFromSizeToIndex(index); + myArrayList[size - 1] = null; + } + size--; + return elementRemoved; } public E set(int index, E element) { - // returns the element previously at the specified position - E previousElement = myArrayList[index]; + E elementPreviouslyAtIndex = myArrayList[index]; if (index < 0 || index >= myArrayList.length) { throw new ArrayIndexOutOfBoundsException(index); } myArrayList[index] = element; - return previousElement; + return elementPreviouslyAtIndex; } public void clear() { @@ -104,4 +108,10 @@ public void shiftElementsFromIndexToSize(int index) { } } + public void shiftElementsFromSizeToIndex(int index) { + for (int i = index; i < size - 1; i++) { + myArrayList[i] = myArrayList[i + 1]; + } + } + } diff --git a/src/test/java/MyArrayListTest.java b/src/test/java/MyArrayListTest.java index c39c7c2..ba1f114 100644 --- a/src/test/java/MyArrayListTest.java +++ b/src/test/java/MyArrayListTest.java @@ -5,10 +5,21 @@ public class MyArrayListTest { private MyArrayList mal; + @Test + public void MyArrayListDefaultConstructorTest() { + // Given + int expectedArrayLength = 10; + mal = new MyArrayList<>(); + // When + int actualArrayLength = mal.length(); + // Then + Assert.assertEquals(expectedArrayLength, actualArrayLength); + } + @Test public void MyArrayListConstructorLengthTest() { // Given - int expectedArrayLength = 1; + int expectedArrayLength = 0; mal = new MyArrayList<>(expectedArrayLength); // When int actualArrayLength = mal.length(); @@ -60,20 +71,67 @@ public void getTest() { Assert.assertEquals(expectedArrayLength, actualArrayLength); } -// @Test -// public void removeTest() { -// // Given -// int expectedIntAtIndex0 = 0; -// int expectedIntAtIndex1 = 1; -// int startingLength = 1; -// // When -// mal = new MyArrayList<>(startingLength); -// mal.add(expectedIntAtIndex0); -// mal.add(expectedIntAtIndex1); -// int actualIntAtIndex1 = mal.get(1); -// // Then -// Assert.assertEquals(expectedIntAtIndex1, actualIntAtIndex1); -// } + @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() { From a570db943ebdaf6f934bce97a9a749d9bd350a50 Mon Sep 17 00:00:00 2001 From: Luis Romero Date: Sun, 11 Mar 2018 12:22:10 -0400 Subject: [PATCH 07/16] finished clear method --- .idea/workspace.xml | 178 +++++++++++++++++------------ src/main/java/MyArrayList.java | 8 +- src/test/java/MyArrayListTest.java | 17 +++ 3 files changed, 125 insertions(+), 78 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 98b6c60..9bb5605 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -14,14 +15,14 @@ - + - + - + @@ -33,8 +34,8 @@ - - + + @@ -45,12 +46,12 @@ - + - - + + @@ -59,12 +60,14 @@ - - - - - - + + + + + + + + @@ -81,6 +84,11 @@ + + + length() + + @@ -115,10 +123,10 @@ - @@ -138,6 +146,8 @@ + + @@ -181,9 +191,7 @@ + + @@ -268,7 +272,7 @@ - + - + - + - + - - - - - + + + + + - - - - - + + + + + @@ -481,65 +485,64 @@ - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + - - + - + - @@ -550,7 +553,9 @@ + + @@ -577,14 +582,88 @@ - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -596,60 +675,31 @@ - + - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + - - + + @@ -658,8 +708,8 @@ - - + + @@ -674,6 +724,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/MyArrayList.java b/src/main/java/MyArrayList.java index 66b49fa..5241881 100644 --- a/src/main/java/MyArrayList.java +++ b/src/main/java/MyArrayList.java @@ -149,4 +149,7 @@ public void shiftElementsFromSizeToIndex(int index) { } } + public Object[] toArray() { + return Arrays.copyOf(myArrayList, size); + } } diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index 4e568bc..df43140 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -10,7 +10,7 @@ public MySet() { } public MySet(Collection collection) { - + this.myArrayList = new MyArrayList<>(collection); } public MySet(int initialCapacity) { @@ -27,7 +27,10 @@ public boolean add(E element) { } public boolean addAll(Collection collection) { - return false; + for (E element : collection) { + myArrayList.add(element); + } + return true; } public void clear() { @@ -75,7 +78,7 @@ public int size() { } public Object[] toArray() { - return null; + return myArrayList.toArray(); } public T[] toArray(T[] a) { diff --git a/src/test/java/MySetTest.java b/src/test/java/MySetTest.java index 9916313..ca1f48e 100644 --- a/src/test/java/MySetTest.java +++ b/src/test/java/MySetTest.java @@ -1,6 +1,8 @@ import org.junit.Assert; import org.junit.Test; +import java.util.Arrays; + public class MySetTest { private MySet mySet; @@ -35,6 +37,28 @@ public void addTest() { Assert.assertEquals(expectedSize, actualSize); } + @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 sizeTest() { // Given From ef230254c9f07744be4cfd33d88e7e48bb316059 Mon Sep 17 00:00:00 2001 From: Luis Romero Date: Mon, 12 Mar 2018 08:38:42 -0400 Subject: [PATCH 15/16] modifying clear method --- .idea/workspace.xml | 267 ++++++++++++++++++----------------- src/main/java/MySet.java | 2 +- src/test/java/MySetTest.java | 17 +++ 3 files changed, 154 insertions(+), 132 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0ee2dc5..6a65399 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,7 +3,6 @@ - @@ -35,8 +34,8 @@ - - + + @@ -47,8 +46,8 @@ - - + + @@ -60,11 +59,11 @@ - + - - + + @@ -73,40 +72,42 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - + + + + - + - - + + @@ -160,8 +161,8 @@ @@ -272,7 +273,7 @@ - + - + - - - - + + + + - - - - - + + + + + @@ -486,14 +487,11 @@ 1520709799727 - + - - - @@ -521,9 +519,12 @@ + + + - @@ -531,12 +532,13 @@ + - + @@ -553,7 +555,6 @@ - @@ -636,30 +637,32 @@ - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + - - - + + + + @@ -696,10 +699,62 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + @@ -708,8 +763,8 @@ - - + + @@ -730,55 +785,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/java/MySet.java b/src/main/java/MySet.java index df43140..cb54ed9 100644 --- a/src/main/java/MySet.java +++ b/src/main/java/MySet.java @@ -34,7 +34,7 @@ public boolean addAll(Collection collection) { } public void clear() { - + this.myArrayList.clear(); } public boolean contains(Object object) { diff --git a/src/test/java/MySetTest.java b/src/test/java/MySetTest.java index ca1f48e..9ce734f 100644 --- a/src/test/java/MySetTest.java +++ b/src/test/java/MySetTest.java @@ -59,6 +59,23 @@ public void addAllTest() { // 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 sizeTest() { // Given From 4a8ead2e9a5ef1adca5d801f899dd7420f1902ef Mon Sep 17 00:00:00 2001 From: Luis Romero Date: Mon, 12 Mar 2018 16:48:36 -0400 Subject: [PATCH 16/16] finished part 1 and part 2; did not do all the methods from Set in java api; only the ones that match myArrayList class in same lab --- .idea/workspace.xml | 416 ++++++++++++++++++--------------- src/main/java/MyArrayList.java | 24 ++ src/main/java/MySet.java | 69 +++--- src/test/java/MySetTest.java | 97 +++++--- 4 files changed, 364 insertions(+), 242 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 6a65399..91fa1a3 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -3,6 +3,7 @@ + @@ -15,7 +16,7 @@ - + @@ -31,11 +32,11 @@ - + - - + + @@ -43,11 +44,11 @@ - + - - + + @@ -59,11 +60,11 @@ - + - - + + @@ -72,42 +73,30 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + - + - - + + @@ -116,14 +105,14 @@ - - - - - - - - + + + + + + + + @@ -145,6 +134,7 @@ size length length() + contains capacity @@ -161,8 +151,8 @@ @@ -184,9 +174,9 @@ - @@ -207,7 +197,7 @@ - + @@ -250,9 +240,9 @@