From 23094e0a4dc88c9b2242da97fe014f9690a28dd4 Mon Sep 17 00:00:00 2001 From: Arjun Desai Date: Thu, 7 Apr 2016 17:15:31 -0400 Subject: [PATCH] Finished lab activity --- .classpath | 7 ++ src/debug/ContainerArray.java | 30 ++++++- src/debug/ContainerArrayTest.java | 12 ++- src/tdd/Cell.java | 41 +++++++++ src/tdd/Sheet.java | 26 ++++++ src/tdd/TestSheet.java | 90 ++++++++++++++++++++ src/voogasalad/Vector.java | 136 ++++++++++++++++++++++++++++++ src/voogasalad/VectorTest.java | 23 +++++ 8 files changed, 360 insertions(+), 5 deletions(-) create mode 100644 .classpath create mode 100644 src/tdd/Cell.java create mode 100644 src/tdd/Sheet.java create mode 100644 src/tdd/TestSheet.java create mode 100644 src/voogasalad/Vector.java create mode 100644 src/voogasalad/VectorTest.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e0fb27 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..f7fc826 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -2,7 +2,7 @@ public class ContainerArray { - private int initialCapacity = 10; + private int limit = 10; private int currentSize = 0; private Object[] internalArray; @@ -10,11 +10,13 @@ public ContainerArray () { this(10); } - public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + public ContainerArray (int limit) { + internalArray = new Object[limit]; } public void add (E element) { + if (currentSize == limit) return; + internalArray[currentSize++] = element; } @@ -23,11 +25,31 @@ public int size () { } public void remove (E objectToRemove) { - currentSize--; + Object[] newArray = new Object[limit]; + int count = 0; + int objectsRemoved=0; + for (Object o : internalArray){ + if (o != objectToRemove){ + newArray[count++] = o; + } else { + objectsRemoved++; + } + } + internalArray = newArray; + currentSize = objectsRemoved; } @SuppressWarnings("unchecked") public E get (int index) { return (E)internalArray[index]; } + + public boolean contains(E objectContained){ + for (Object o : internalArray){ + if (o==objectContained){ + return true; + } + } + return false; + } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..038474c 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -15,10 +15,15 @@ public void setUp () { @Test public void testSizeChangeWithAdd () { + assertEquals("Initial Size",0,myContainer.size()); myContainer.add("Alligator"); myContainer.add("Bear"); myContainer.add("Camel"); assertEquals("Add size", 3, myContainer.size()); + for (int i =0;i<100;i++){ + myContainer.add(Integer.toString(i)); + } + assertEquals("Max size", 10, myContainer.size()); } @Test @@ -30,18 +35,23 @@ public void testObjectIsStored () { @Test public void testSizeChangeWithRemove () { + System.out.println("DAS"+myContainer.size()); myContainer.add("Alligator"); myContainer.add("Bear"); myContainer.remove("Alligator"); + assertEquals("Remove size", 1, myContainer.size()); } @Test public void testObjectIsRemoved () { String alligator = "Alligator"; - myContainer.add("Alligator"); + myContainer.add(alligator); myContainer.add("Bear"); myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } + + + } diff --git a/src/tdd/Cell.java b/src/tdd/Cell.java new file mode 100644 index 0000000..0659f3d --- /dev/null +++ b/src/tdd/Cell.java @@ -0,0 +1,41 @@ +package tdd; + +public class Cell { + private String value=""; + private String numericValue; + + public Cell(){ + + } + + public Cell(String value){ + this.value = value; + if (isInt(value)){ + numericValue = value.trim(); + } + } + + public boolean isInt(String string){ + String s = string.trim(); + if(s.isEmpty()) return false; + for(int i = 0; i < s.length(); i++) { + if(i == 0 && s.charAt(i) == '-') { + if(s.length() == 1) return false; + else continue; + } + if(Character.digit(s.charAt(i),10) < 0) return false; + } + return true; + } + + public String get(){ + if (numericValue == null) return value; + return numericValue; + } + + public String getLiteral(){ + return value; + } + + +} diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..e105b29 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,26 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + +public class Sheet { + Map cellMap; + + public Sheet(){ + cellMap = new HashMap(); + } + + public String get (String string) { + if (!cellMap.containsKey(string)) return ""; + return cellMap.get(string).get(); + } + + public void put (String theCell, String string) { + cellMap.put(theCell, new Cell(string)); + } + + public String getLiteral (String theCell) { + return cellMap.get(theCell).getLiteral(); + } + +} diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..8f9a6b7 --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,90 @@ +package tdd; + +import static org.junit.Assert.*; +import org.junit.Test; + + +public class TestSheet { + @Test + public void testThatCellsAreEmptyByDefault () { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + // Implement each test before going to the next one. + @Test + public void testThatTextCellsAreStored () { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "A string"); + assertEquals("A string", sheet.get(theCell)); + + sheet.put(theCell, "A different string"); + assertEquals("A different string", sheet.get(theCell)); + + sheet.put(theCell, ""); + assertEquals("", sheet.get(theCell)); + } + + // Implement each test before going to the next one; then refactor. + @Test + public void testThatManyCellsExist () { + Sheet sheet = new Sheet(); + sheet.put("A1", "First"); + sheet.put("X27", "Second"); + sheet.put("ZX901", "Third"); + + assertEquals("A1", "First", sheet.get("A1")); + assertEquals("X27", "Second", sheet.get("X27")); + assertEquals("ZX901", "Third", sheet.get("ZX901")); + + sheet.put("A1", "Fourth"); + assertEquals("A1 after", "Fourth", sheet.get("A1")); + assertEquals("X27 same", "Second", sheet.get("X27")); + assertEquals("ZX901 same", "Third", sheet.get("ZX901")); + } + + // Implement each test before going to the next one. + // You can split this test case if it helps. + @Test + public void testThatNumericCellsAreIdentifiedAndStored () { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "X99"); // "Obvious" string + assertEquals("X99", sheet.get(theCell)); + + sheet.put(theCell, "14"); // "Obvious" number + assertEquals("14", sheet.get(theCell)); + + sheet.put(theCell, " 99 X"); // Whole string must be numeric + assertEquals(" 99 X", sheet.get(theCell)); + + sheet.put(theCell, " 1234 "); // Blanks ignored + assertEquals("1234", sheet.get(theCell)); + + sheet.put(theCell, " "); // Just a blank + assertEquals(" ", sheet.get(theCell)); + } + + // Refactor before going to each succeeding test. + @Test + public void testThatWeHaveAccessToCellLiteralValuesForEditing () { + Sheet sheet = new Sheet(); + String theCell = "A21"; + + sheet.put(theCell, "Some string"); + assertEquals("Some string", sheet.getLiteral(theCell)); + + sheet.put(theCell, " 1234 "); + assertEquals(" 1234 ", sheet.getLiteral(theCell)); + + sheet.put(theCell, "=7"); // Foreshadowing formulas:) + assertEquals("=7", sheet.getLiteral(theCell)); + } + + // We'll talk about "get" and formulas next time. + +} diff --git a/src/voogasalad/Vector.java b/src/voogasalad/Vector.java new file mode 100644 index 0000000..54c324a --- /dev/null +++ b/src/voogasalad/Vector.java @@ -0,0 +1,136 @@ +package voogasalad; + +/** + * + * @author Aditya Srinivasan, Arjun Desai, Nick Lockett, Harry Guo, Hunter Lee + * + */ +public class Vector { + + private double xPrev = 0; + private double yPrev = 0; + private double x; + private double y; + private double angle = 0;; + + /** + * Vector class that keeps information about (x, y) coordinates as well as + * previous coordinates + * + * Whenever current coordinates are updated, previous coordinates are updated + * accordingly + * + */ + public Vector (double x, double y) { + setXY(x, y); + } + + /** + * Get the current x coordinate + * + * @return + */ + public double getX() { + return x; + } + + /** + * Get the current y coordinate + * + * @return + */ + public double getY() { + return y; + } + + /** + * Give x a new value + * + * @param x + */ + public void setX(double x) { + this.xPrev = getX(); + this.x = x; + } + + /** + * Give y a new value + * + * @param y + */ + public void setY(double y) { + this.yPrev = getY(); + this.y = y; + } + + /** + * Sets x, y to a new set of values + * + * @param x + * @param y + */ + public void setXY(double x, double y) { + setX(x); + setY(y); + } + + /** + * Adds x coordinate + * + * @param dx + */ + public void addX(double dx) { + this.xPrev = getX(); + x += dx; + } + + /** + * Adds y coordinate + * + * @param dy + */ + public void addY(double dy) { + this.yPrev = getY(); + y += dy; + } + + /** + * Adds vector v to self + * + * @param v + */ + public void addVector(Vector v) { + this.xPrev = getX(); + this.yPrev = getY(); + x += v.getX(); + y += v.getY(); + } + + /** + * @return the xPrev + */ + public double getxPrev() { + return xPrev; + } + + /** + * @return the yPrev + */ + public double getyPrev() { + return yPrev; + } + + /** + * @return the angle + */ + public double getAngle() { + return angle; + } + + /** + * @param angle the angle to set + */ + public void setAngle(double angle) { + this.angle = angle; + } +} diff --git a/src/voogasalad/VectorTest.java b/src/voogasalad/VectorTest.java new file mode 100644 index 0000000..2cccc81 --- /dev/null +++ b/src/voogasalad/VectorTest.java @@ -0,0 +1,23 @@ +package voogasalad; + +import static org.junit.Assert.*; +import org.junit.Before; +import org.junit.Test; + + +public class VectorTest { + private Vector v; + + @Before + public void setUp () { + v = new Vector(10, 10); + } + + @SuppressWarnings("deprecation") + @Test + public void testGetCoordinate () { + assertEquals("Get X", 10, v.getX()); + assertEquals("Get Y", 10, v.getY()); + } + +}