Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="output" path="bin"/>
</classpath>
30 changes: 26 additions & 4 deletions src/debug/ContainerArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@


public class ContainerArray<E> {
private int initialCapacity = 10;
private int limit = 10;
private int currentSize = 0;
private Object[] internalArray;

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;
}

Expand All @@ -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;
}
}
12 changes: 11 additions & 1 deletion src/debug/ContainerArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}



}
41 changes: 41 additions & 0 deletions src/tdd/Cell.java
Original file line number Diff line number Diff line change
@@ -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;
}


}
26 changes: 26 additions & 0 deletions src/tdd/Sheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tdd;

import java.util.HashMap;
import java.util.Map;

public class Sheet {
Map<String,Cell> cellMap;

public Sheet(){
cellMap = new HashMap<String,Cell>();
}

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();
}

}
90 changes: 90 additions & 0 deletions src/tdd/TestSheet.java
Original file line number Diff line number Diff line change
@@ -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.

}
Loading