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
73 changes: 45 additions & 28 deletions src/debug/ContainerArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,49 @@


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

public ContainerArray () {
this(10);
}

public ContainerArray (int initialCapacity) {
internalArray = new Object[initialCapacity];
}

public void add (E element) {
internalArray[currentSize++] = element;
}

public int size () {
return currentSize;
}

public void remove (E objectToRemove) {
currentSize--;
}

@SuppressWarnings("unchecked")
public E get (int index) {
return (E)internalArray[index];
}
private int limit = 10;
private int currentSize = 0;
private Object[] internalArray;

public ContainerArray () {
this(10);
}

public ContainerArray (int limit) {
internalArray = new Object[limit];
}

public void add (E element) {
if(currentSize < internalArray.length){
internalArray[currentSize++] = element;
}
}

public int size () {
return currentSize;
}

public void remove (E objectToRemove) {
int removeIndex = -1;
for(int i = 0;i < currentSize;i++){
if(internalArray[i].equals(objectToRemove)){
removeIndex = i;
}
}
if(removeIndex >= 0){
removeElement(removeIndex);
currentSize--;
}
}
private void removeElement(int index){
for(int i = index;i < currentSize-1;i++){
internalArray[i] = internalArray[i+1];
}
}


@SuppressWarnings("unchecked")
public E get (int index) {
return (E)internalArray[index];
}
}
28 changes: 28 additions & 0 deletions src/debug/ContainerArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,32 @@ public void testObjectIsRemoved () {
myContainer.remove("Bear");
assertEquals("Remove should be same reference", alligator, myContainer.get(0));
}

@Test
public void testAddMoreThanLimit(){
myContainer = new ContainerArray<String>(3);
for(int i = 0;i < 4;i++){
myContainer.add("goodbye");
}
}

@Test
public void testRemoveSpecificElement(){
myContainer = new ContainerArray<String>(3);
for(int i = 0;i < 3;i++){
myContainer.add("goodbye");
}
myContainer.remove("hi");
assertEquals("Incorrect size", 3, myContainer.size());
}

@Test
public void testShouldRemoveElement(){
myContainer = new ContainerArray<String>(3);
for(int i = 0;i < 3;i++){
myContainer.add("goodbye");
}
myContainer.remove("goodbye");
assertEquals("Incorrect size", 2, myContainer.size());
}
}
28 changes: 28 additions & 0 deletions src/tdd/Sheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tdd;

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

public class Sheet {

private Map<String, String> map;
private Map<String, String> literals;
public Sheet(){
map = new HashMap<String, String>();
literals = new HashMap<String, String>();
}
public String get(String string){
return map.getOrDefault(string, "");
}
public void put(String theCell, String value){
literals.put(theCell, value);
if(value.trim().matches("^[0-9]+$")){
map.put(theCell, value.trim());
return;
}
map.put(theCell, value);
}
public String getLiteral(String string){
return literals.get(string);
}
}
86 changes: 86 additions & 0 deletions src/tdd/TestSheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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"));
}

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

@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"));
}

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

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



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

import java.util.ArrayList;
import java.util.Collection;

import javafx.util.Pair;

public class Map {
String[][] myMap;
public Map(int size){
myMap = new String[size][size];
}
public void store(int x, int y, String landValue){
if(checkBounds(x,y)){
myMap[x][y] = landValue;
}
}
public String get(int x, int y){
if(checkBounds(x,y)){
return myMap[x][y];
}
return "";
}
private boolean checkBounds(int x, int y){
return (x < myMap.length) && (y < myMap.length);
}
public Collection<Pair<Integer, Integer>> findLandValue(String landVal){
ArrayList<Pair<Integer, Integer>> locs = new ArrayList<>();
for(int i = 0;i < myMap.length;i++){
for(int j = 0;j < myMap[i].length;j++){
if(myMap[i][j] == landVal){
locs.add(new Pair<Integer, Integer>(i, j));
}
}
}
return locs;
}
public int size(){
return myMap.length;
}

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

import static org.junit.Assert.*;

import org.junit.Test;
import voogasalad.Map;

public class TestMap {

Map myMap;
private void setUp(int size) {
myMap = new Map(size);
}

@Test
public void testStore(){
setUp(5);
myMap.store(0, 0, "something");
assertEquals("Stored incorrectly", "something", myMap.get(0, 0));
}

@Test
public void testSize(){
setUp(6);
assertEquals("Size is wrong", 6, myMap.size());
setUp(7);
assertEquals("Size is wrong", 7, myMap.size());
setUp(8);
assertEquals("Size is wrong", 8, myMap.size());
setUp(9);
assertEquals("Size is wrong", 9, myMap.size());
setUp(1234);
assertEquals("Size is wrong", 1234, myMap.size());

}

@Test
public void testOutOfBounds(){
setUp(3);
myMap.get(4, 4);
myMap.store(4, 4, "something");
}

}