From cc118f8738043c1bfe9a831f52f9ae934ca53719 Mon Sep 17 00:00:00 2001 From: AaronNewman Date: Thu, 7 Apr 2016 17:42:50 -0400 Subject: [PATCH 1/2] Adding lab exercises --- .classpath | 6 +++++ src/debug/ContainerArray.java | 38 ++++++++++++++++++++++++++----- src/debug/ContainerArrayTest.java | 24 +++++++++++++++++++ src/tdd/Sheet.java | 33 +++++++++++++++++++++++++++ src/voogasalad/Tests | 6 +++++ 5 files changed, 101 insertions(+), 6 deletions(-) create mode 100644 .classpath create mode 100644 src/tdd/Sheet.java create mode 100644 src/voogasalad/Tests diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..fb50116 --- /dev/null +++ b/.classpath @@ -0,0 +1,6 @@ + + + + + + diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..810dd7b 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,21 +1,33 @@ package debug; - public class ContainerArray { - private int initialCapacity = 10; private int currentSize = 0; + private static final int INITIAL_CAPACITY = 10; + private int capacity; private Object[] internalArray; public ContainerArray () { - this(10); + this(INITIAL_CAPACITY); } public ContainerArray (int initialCapacity) { internalArray = new Object[initialCapacity]; + capacity = initialCapacity; } public void add (E element) { - internalArray[currentSize++] = element; + Object[] newInternalArray = new Object[capacity * 2]; + if ((currentSize + 1) > capacity) { + for (int i = 0; i < capacity; i++) { + newInternalArray[i] = internalArray[i]; + } + capacity = capacity * 2; + newInternalArray[currentSize++] = element; + internalArray = newInternalArray; + } + else { + internalArray[currentSize++] = element; + } } public int size () { @@ -23,11 +35,25 @@ public int size () { } public void remove (E objectToRemove) { - currentSize--; + for (int i = 0; i < currentSize; i++) { + if (internalArray[i].equals(objectToRemove)) { + for (int j = i; j < currentSize - 1; j++) { + internalArray[j] = internalArray[j + 1]; + } + i--; + currentSize--; + } + } + //Does not remove object from internalArray + //currentSize needs to be decremented as many times as there are objectToRemove's } @SuppressWarnings("unchecked") public E get (int index) { - return (E)internalArray[index]; + //Does not check if index is out of bounds + if (index > currentSize) { + return null; + } + return (E) internalArray[index]; } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..d55f66d 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -44,4 +44,28 @@ public void testObjectIsRemoved () { myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } + + @Test + public void testSizeLimit () { + myContainer = new ContainerArray<>(1); + myContainer.add("Alligator"); + myContainer.add("Bear"); + } + + @Test + public void testMultipleRemoves () { + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + myContainer.remove("Alligator"); + assertEquals("Remove size", 1, myContainer.size()); + } + + @Test + public void testRemovesAll () { + myContainer.add("Alligator"); + myContainer.add("Alligator"); + myContainer.remove("Alligator"); + assertEquals("Removes all", 0, myContainer.size()); + } } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..8ab85c5 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,33 @@ +package tdd; + +import java.util.HashMap; + +public class Sheet { + HashMap myMap = new HashMap(); + + public void put (String key, String val) { + myMap.put(key, val); + } + + public String get (String key) { + String val = myMap.get(key); + String[] valueArray = val.split(" "); + if (valueArray.length != 1) { + return val; + } + else if (isNumeric(valueArray[0])) { + return val.trim(); + } + else { + return val; + } + } + + public String getLiteral (String key) { + return myMap.get(key); + } + + public boolean isNumeric(String s) { + return s.matches("-?\\d+(\\.\\d+)?"); + } +} diff --git a/src/voogasalad/Tests b/src/voogasalad/Tests new file mode 100644 index 0000000..5af9182 --- /dev/null +++ b/src/voogasalad/Tests @@ -0,0 +1,6 @@ +Tests for VOOGASalad +==================== + +1. Test that user input to authoring environment text fields is transferred over to data files accurately. + +2. Test that whenever the user clicks on a unit it is set as the selected unit. \ No newline at end of file From 2da31dd88deced86b94c66996e1ed53fdfdfafb4 Mon Sep 17 00:00:00 2001 From: Aaron Newman Date: Thu, 7 Apr 2016 17:46:00 -0400 Subject: [PATCH 2/2] Update tests --- src/voogasalad/Tests | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/voogasalad/Tests b/src/voogasalad/Tests index 5af9182..ecd48d2 100644 --- a/src/voogasalad/Tests +++ b/src/voogasalad/Tests @@ -3,4 +3,4 @@ Tests for VOOGASalad 1. Test that user input to authoring environment text fields is transferred over to data files accurately. -2. Test that whenever the user clicks on a unit it is set as the selected unit. \ No newline at end of file +2. Test that whenever the user clicks on a unit it is set as the selected unit.