From 164dcce18da2957e2176cc0819be752b7b7340ff Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 21:44:55 -0400 Subject: [PATCH 1/6] StackArrayList green --- src/main/java/Pair/Arrays.java | 3 +- src/main/java/Pair/Pair.java | 2 +- src/main/java/StackArray/GenericStack.java | 5 +- src/main/java/StackArray/ObjectStack.java | 5 +- src/main/java/StackArrayList/Stack.java | 15 +++- .../java/StackArray/GenericStackTest.java | 82 ++++++++--------- src/test/java/StackArray/ObjectStackTest.java | 78 ++++++++-------- src/test/java/StackArrayList/StackTest.java | 89 +++++++++---------- 8 files changed, 147 insertions(+), 132 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5bdf780..ddf05fc 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -11,6 +11,7 @@ * And a minmax method that returns a pair containing the largest and smallest items from the array list */ public class Arrays { - public static <___> Pair firstLast(ArrayList<___> a) { + public static Pair firstLast(ArrayList a) { + return null; } } diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index c4dd905..fbb14ce 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -7,6 +7,6 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair { } diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index d84c4db..bb1cc0e 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -1,13 +1,14 @@ package StackArray; -import java.util.Arrays; +import StackArrayList.Stack; /** * Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty. * Remember, you might need to resize the stack in the push method. + * * @param */ -public class GenericStack { +public class GenericStack extends Stack { private E[] elements; public GenericStack() { diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 1124698..d2227d7 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -1,13 +1,14 @@ package StackArray; -import java.util.Arrays; +import StackArrayList.Stack; /** * Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty. * Remember, you might need to resize the stack in the push method. + * * @param */ -public class ObjectStack { +public class ObjectStack extends Stack { private Object[] elements; public ObjectStack() { diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 0338de3..381f183 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -1,16 +1,29 @@ package StackArrayList; import java.util.ArrayList; +import java.util.List; /** * Implement Stack by adding the push, pop, and isEmpty functions. It must pass the prewritten unit tests. * If you pop on an empty stack, throw an IndexOutOfBoundsException. */ public class Stack { - private ArrayList elements; + private List elements; public Stack(){ + elements = new ArrayList<>(); + } + + public void push(E e) { + elements.add(e); + } + + public E pop() { + return elements.remove(elements.size()-1); + } + public boolean isEmpty() { + return elements.size() == 0; } } diff --git a/src/test/java/StackArray/GenericStackTest.java b/src/test/java/StackArray/GenericStackTest.java index 0aacd92..c61b904 100644 --- a/src/test/java/StackArray/GenericStackTest.java +++ b/src/test/java/StackArray/GenericStackTest.java @@ -1,41 +1,41 @@ -//package StackArray; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class GenericStackTest { -// @Test -// public void testPushingGrowsTheStack() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// -// // Assert that it is empty. -// Assert.assertEquals(true, stack.isEmpty()); -// // When we push something onto the stack -// stack.push("foobar"); -// // Then it shouldn't be empty -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testPushingAndPoppingOrder() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// // When we push two elements on it -// stack.push("foo"); -// stack.push("bar"); -// // Then we should see them returned in the correct order -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// // When it's popped -// stack.pop(); -// // Then we should get an exception -// } -// -//} \ No newline at end of file +package StackArray; + +import org.junit.Assert; +import org.junit.Test; + +public class GenericStackTest { + @Test + public void testPushingGrowsTheStack() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + + // Assert that it is empty. + Assert.assertEquals(true, stack.isEmpty()); + // When we push something onto the stack + stack.push("foobar"); + // Then it shouldn't be empty + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testPushingAndPoppingOrder() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + // When we push two elements on it + stack.push("foo"); + stack.push("bar"); + // Then we should see them returned in the correct order + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + // When it's popped + stack.pop(); + // Then we should get an exception + } + +} \ No newline at end of file diff --git a/src/test/java/StackArray/ObjectStackTest.java b/src/test/java/StackArray/ObjectStackTest.java index 9ec9615..2576f1b 100644 --- a/src/test/java/StackArray/ObjectStackTest.java +++ b/src/test/java/StackArray/ObjectStackTest.java @@ -1,39 +1,39 @@ -//package StackArray; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class ObjectStackTest { -// @Test -// public void testPushingGrowsTheStack() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // Assert that it is empty. -// Assert.assertEquals(true, stack.isEmpty()); -// // When we push something onto the stack -// stack.push("foobar"); -// // Then it shouldn't be empty -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testPushingAndPoppingOrder() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // When we push two elements on it -// stack.push("foo"); -// stack.push("bar"); -// // Then we should see them returned in the correct order -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // When it's popped -// stack.pop(); -// // Then we should get an exception -// } -//} \ No newline at end of file +package StackArray; + +import org.junit.Assert; +import org.junit.Test; + +public class ObjectStackTest { + @Test + public void testPushingGrowsTheStack() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // Assert that it is empty. + Assert.assertEquals(true, stack.isEmpty()); + // When we push something onto the stack + stack.push("foobar"); + // Then it shouldn't be empty + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testPushingAndPoppingOrder() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // When we push two elements on it + stack.push("foo"); + stack.push("bar"); + // Then we should see them returned in the correct order + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // When it's popped + stack.pop(); + // Then we should get an exception + } +} \ No newline at end of file diff --git a/src/test/java/StackArrayList/StackTest.java b/src/test/java/StackArrayList/StackTest.java index 0ce7cf0..c83884f 100644 --- a/src/test/java/StackArrayList/StackTest.java +++ b/src/test/java/StackArrayList/StackTest.java @@ -1,45 +1,44 @@ -//package StackArrayList; -// -//import org.junit.Test; -// -//import org.junit.Assert; -// -//public class StackTest { -// @Test -// public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// // Assert that it starts empty -// Assert.assertEquals(true, stack.isEmpty()); -// // When an element gets pushed -// stack.push("foobar"); -// // Then the stack should not be empty. -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// -// //When two items are pushed -// stack.push("foo"); -// stack.push("bar"); -// -// // Then they should come off in reverse order. -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// -// // And then the stack should be empty -// Assert.assertEquals(true, stack.isEmpty()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// // Then it is popped -// stack.pop(); -// // We should get an exception -// } -//} \ No newline at end of file +package StackArrayList; + +import org.junit.Assert; +import org.junit.Test; + +public class StackTest { + @Test + public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + // Assert that it starts empty + Assert.assertEquals(true, stack.isEmpty()); + // When an element gets pushed + stack.push("foobar"); + // Then the stack should not be empty. + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + + //When two items are pushed + stack.push("foo"); + stack.push("bar"); + + // Then they should come off in reverse order. + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + + // And then the stack should be empty + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + // Then it is popped + stack.pop(); + // We should get an exception + } +} \ No newline at end of file From 21de74bab5352e355240309f39ca216fc8586fae Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 22:05:22 -0400 Subject: [PATCH 2/6] Everything extending Stack works, combiner stubs --- .../ArrayListCombiner/ArrayListCombiner.java | 5 ++ src/main/java/Pair/Arrays.java | 18 ++++- src/main/java/Pair/Pair.java | 22 ++++++ src/main/java/StackArray/ObjectStack.java | 4 +- .../ArrayListCombinerTest.java | 74 +++++++++---------- 5 files changed, 81 insertions(+), 42 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index d302cd2..6d48b17 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -9,4 +9,9 @@ * The second method should be called superCombiner and should use ? super E */ public class ArrayListCombiner { + public static void superCombiner(ArrayList parent, ArrayList child) { + } + + public static void extendCombiner(ArrayList parent, ArrayList child) { + } } diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index ddf05fc..5ea2d57 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -1,7 +1,6 @@ package Pair; import java.util.ArrayList; -import java.util.Collections; /** * In here you must make firstLast, which will return a pair of the first element in the array list and the last @@ -11,7 +10,22 @@ * And a minmax method that returns a pair containing the largest and smallest items from the array list */ public class Arrays { - public static Pair firstLast(ArrayList a) { + public static Pair firstLast(ArrayList a) { + E first = a.get(0); + E second = a.get(a.size() - 1); + + return new Pair<>(first, second); + } + + public E max(ArrayList a) { + return null; + } + + public E min(ArrayList a) { + return null; + } + + public Pair minmax(ArrayList a) { return null; } } diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index fbb14ce..0b0c517 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -8,5 +8,27 @@ * max -> returns the maximum of the pair */ public class Pair { + private E first; + private E second; + public Pair(E first, E second) { + this.first = first; + this.second = second; + } + + public E getFirst() { + return first; + } + + public E getSecond() { + return second; + } + + public E min() { + return null; + } + + public E max() { + return null; + } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index d2227d7..a98269c 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -11,7 +11,5 @@ public class ObjectStack extends Stack { private Object[] elements; - public ObjectStack() { - - } + public ObjectStack() {} } diff --git a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java index 957a878..4f7d368 100644 --- a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java +++ b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java @@ -2,47 +2,47 @@ import Employee.Employee; import Employee.Manager; -import org.junit.Test; - import org.junit.Assert; +import org.junit.Test; import java.util.ArrayList; public class ArrayListCombinerTest { -// Employee foo = new Employee("FOO", 100); -// Manager bar = new Manager("BAR", 100); -// @Test -// public void testExtendCombiner() throws Exception { -// // Given an array list with employees -// ArrayList first = new ArrayList<>(); -// first.add(foo); -// // An an array list with managers -// ArrayList second = new ArrayList<>(); -// second.add(bar); -// // When I combine them -// ArrayListCombiner.extendCombiner(first, second); -// // Then I should get an arrayList with both -// ArrayList expected = new ArrayList<>(); -// expected.add(foo); -// expected.add(bar); -// Assert.assertEquals(expected, first); -// } -// -// @Test -// public void testSuperCombiner() throws Exception { -// // Given an array list with employees -// ArrayList first = new ArrayList<>(); -// first.add(foo); -// // An an array list with managers -// ArrayList second = new ArrayList<>(); -// second.add(bar); -// // When I combine them -// ArrayListCombiner.superCombiner(first, second); -// // Then I should get an arrayList with both -// ArrayList expected = new ArrayList<>(); -// expected.add(foo); -// expected.add(bar); -// Assert.assertEquals(expected, first); -// } + Employee foo = new Employee("FOO", 100); + Manager bar = new Manager("BAR", 100); + + @Test + public void testExtendCombiner() throws Exception { + // Given an array list with employees + ArrayList first = new ArrayList<>(); + first.add(foo); + // An an array list with managers + ArrayList second = new ArrayList<>(); + second.add(bar); + // When I combine them + ArrayListCombiner.extendCombiner(first, second); + // Then I should get an arrayList with both + ArrayList expected = new ArrayList<>(); + expected.add(foo); + expected.add(bar); + Assert.assertEquals(expected, first); + } + + @Test + public void testSuperCombiner() throws Exception { + // Given an array list with employees + ArrayList first = new ArrayList<>(); + first.add(foo); + // An an array list with managers + ArrayList second = new ArrayList<>(); + second.add(bar); + // When I combine them + ArrayListCombiner.superCombiner(first, second); + // Then I should get an arrayList with both + ArrayList expected = new ArrayList<>(); + expected.add(foo); + expected.add(bar); + Assert.assertEquals(expected, first); + } } \ No newline at end of file From 398f30ff23969f727184ef1a71df748eb0a8f3ee Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 22:09:08 -0400 Subject: [PATCH 3/6] Pair->min,max --- src/main/java/Pair/Pair.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index 0b0c517..fa6be40 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -7,7 +7,7 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair> { private E first; private E second; @@ -25,10 +25,10 @@ public E getSecond() { } public E min() { - return null; + return (first.compareTo(second) < 1) ? first : second; } public E max() { - return null; + return (first.compareTo(second) > 0) ? first : second; } } From d67c8576f92a651a638f22044e8fae8f7c38513e Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 22:43:21 -0400 Subject: [PATCH 4/6] Implement Combiner methods --- .../ArrayListCombiner/ArrayListCombiner.java | 1 + src/main/java/Pair/Arrays.java | 16 +++++++++------- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index 6d48b17..13f282e 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -10,6 +10,7 @@ */ public class ArrayListCombiner { public static void superCombiner(ArrayList parent, ArrayList child) { + } public static void extendCombiner(ArrayList parent, ArrayList child) { diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5ea2d57..983bc89 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -1,6 +1,8 @@ package Pair; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; /** * In here you must make firstLast, which will return a pair of the first element in the array list and the last @@ -10,22 +12,22 @@ * And a minmax method that returns a pair containing the largest and smallest items from the array list */ public class Arrays { - public static Pair firstLast(ArrayList a) { + public static > Pair firstLast(ArrayList a) { E first = a.get(0); E second = a.get(a.size() - 1); return new Pair<>(first, second); } - public E max(ArrayList a) { - return null; + public > E max(ArrayList a) { + return Collections.max(a); } - public E min(ArrayList a) { - return null; + public > E min(ArrayList a) { + return Collections.min(a); } - public Pair minmax(ArrayList a) { - return null; + public > Pair minmax(ArrayList a) { + return new Pair<>(min(a), max(a)); } } From c9022fbf40deaa67bbe5b76606ae8d41f81e6440 Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 23:09:46 -0400 Subject: [PATCH 5/6] MapFunc::map --- .../ArrayListCombiner/ArrayListCombiner.java | 3 +- src/main/java/MapFunc/MapFunc.java | 8 +++ src/test/java/MapFunc/MapFuncTest.java | 71 +++++++++---------- 3 files changed, 45 insertions(+), 37 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index 13f282e..0f153cd 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -10,9 +10,10 @@ */ public class ArrayListCombiner { public static void superCombiner(ArrayList parent, ArrayList child) { - + parent.addAll(child); } public static void extendCombiner(ArrayList parent, ArrayList child) { + parent.addAll(child); } } diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index ed4bf66..f857403 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -9,4 +9,12 @@ */ public class MapFunc { + public static ArrayList map(ArrayList a, Function f) { + + ArrayList result = new ArrayList<>(a.size()); + a.forEach( + t -> result.add(f.apply(t)) + ); + return result; + } } diff --git a/src/test/java/MapFunc/MapFuncTest.java b/src/test/java/MapFunc/MapFuncTest.java index 3c7534f..7a1a84b 100644 --- a/src/test/java/MapFunc/MapFuncTest.java +++ b/src/test/java/MapFunc/MapFuncTest.java @@ -1,36 +1,35 @@ -//package MapFunc; -// -//import MapFunc.MapFunc; -//import org.junit.Test; -// -//import java.util.ArrayList; -//import org.junit.Assert; -// -//public class MapFuncTest { -// @Test -// public void testSingleTypeMap() throws Exception { -// // Given an integer array list -// ArrayList intList = new ArrayList<>(); -// intList.add(1); -// intList.add(2); -// // When it's mapped with a function to double the value -// ArrayList mappedList = MapFunc.map(intList, num -> num*2); -// // Then all the values are doubled -// Assert.assertEquals(new Integer(2), mappedList.get(0)); -// Assert.assertEquals(new Integer(4), mappedList.get(1)); -// } -// -// @Test -// public void testMultipleTypeMap() throws Exception { -// // Given an integer array list -// ArrayList intList = new ArrayList<>(); -// intList.add(1); -// intList.add(2); -// // When it's mapped with to string -// ArrayList mappedList = MapFunc.map(intList, num -> num.toString()); -// // Then all the values are doubled -// Assert.assertEquals("1", mappedList.get(0)); -// Assert.assertEquals("2", mappedList.get(1)); -// } -// -//} \ No newline at end of file +package MapFunc; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +public class MapFuncTest { + @Test + public void testSingleTypeMap() throws Exception { + // Given an integer array list + ArrayList intList = new ArrayList<>(); + intList.add(1); + intList.add(2); + // When it's mapped with a function to double the value + ArrayList mappedList = MapFunc.map(intList, num -> num * 2); + // Then all the values are doubled + Assert.assertEquals(new Integer(2), mappedList.get(0)); + Assert.assertEquals(new Integer(4), mappedList.get(1)); + } + + @Test + public void testMultipleTypeMap() throws Exception { + // Given an integer array list + ArrayList intList = new ArrayList<>(); + intList.add(1); + intList.add(2); + // When it's mapped with to string + ArrayList mappedList = MapFunc.map(intList, num -> num.toString()); + // Then all the values are doubled + Assert.assertEquals("1", mappedList.get(0)); + Assert.assertEquals("2", mappedList.get(1)); + } + +} \ No newline at end of file From f9e6c8fad3c758e5e46326530529d5e6fe883331 Mon Sep 17 00:00:00 2001 From: vvmk Date: Mon, 12 Mar 2018 23:27:28 -0400 Subject: [PATCH 6/6] Swap, Table, TableNested --- src/main/java/Pair/Arrays.java | 1 - src/main/java/StackArray/ObjectStack.java | 3 +- src/main/java/StackArrayList/Stack.java | 4 +- src/main/java/Table/Table.java | 18 +++- src/main/java/TableNested/TableNested.java | 21 +++- src/test/java/Swap/SwapTest.java | 30 +++--- src/test/java/Table/TableTest.java | 100 +++++++++--------- .../java/TableNested/TableNestedTest.java | 100 +++++++++--------- 8 files changed, 154 insertions(+), 123 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 983bc89..9377c3a 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -2,7 +2,6 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; /** * In here you must make firstLast, which will return a pair of the first element in the array list and the last diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index a98269c..6f15515 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -11,5 +11,6 @@ public class ObjectStack extends Stack { private Object[] elements; - public ObjectStack() {} + public ObjectStack() { + } } diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 381f183..11731db 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -11,7 +11,7 @@ public class Stack { private List elements; - public Stack(){ + public Stack() { elements = new ArrayList<>(); } @@ -20,7 +20,7 @@ public void push(E e) { } public E pop() { - return elements.remove(elements.size()-1); + return elements.remove(elements.size() - 1); } public boolean isEmpty() { diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 5ccce23..6bf775e 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -1,6 +1,7 @@ package Table; -import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; /** * This class needs to manage an ArrayList of Entry objects. It needs a get method that takes a key and returns @@ -10,8 +11,21 @@ * Void return on `remove`. */ public class Table { - private ArrayList entries; + private Map entries; public Table() { + entries = new HashMap<>(); + } + + public V get(K key) { + return entries.get(key); + } + + public void put(K key, V value) { + entries.put(key, value); + } + + public void remove(K key) { + entries.remove(key); } } diff --git a/src/main/java/TableNested/TableNested.java b/src/main/java/TableNested/TableNested.java index 7e0dfdd..c787d5b 100644 --- a/src/main/java/TableNested/TableNested.java +++ b/src/main/java/TableNested/TableNested.java @@ -1,11 +1,28 @@ package TableNested; -import java.util.ArrayList; +import Table.Table; /** * All you need to do for this microlab is take the Table and Entry from the last one and make Entry a nested class. * Think about how nested classes should work with generics. */ -public class TableNested { +public class TableNested extends Table { + private class Entry { + private K key; + private V value; + public Entry(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + } } diff --git a/src/test/java/Swap/SwapTest.java b/src/test/java/Swap/SwapTest.java index 2583b9d..8069084 100644 --- a/src/test/java/Swap/SwapTest.java +++ b/src/test/java/Swap/SwapTest.java @@ -1,16 +1,16 @@ package Swap; -// -//import org.junit.Assert; -//import org.junit.Test; -// -///** -// * Get the tests passing. -// */ -//public class SwapTest { -// @Test -// public void testSwap() throws Exception { -// Double[] result = Swap.swap(0,1, 1.5, 2,3); -// Double[] expected = {2.0, 1.5, 3.0}; -// Assert.assertArrayEquals(expected, result); -// } -//} \ No newline at end of file + +import org.junit.Assert; +import org.junit.Test; + +/** + * Get the tests passing. + */ +public class SwapTest { + @Test + public void testSwap() throws Exception { + Double[] result = Swap.swap(0, 1, 1.5, 2.0, 3.0); + Double[] expected = {2.0, 1.5, 3.0}; + Assert.assertArrayEquals(expected, result); + } +} \ No newline at end of file diff --git a/src/test/java/Table/TableTest.java b/src/test/java/Table/TableTest.java index 15ac19c..1d10b31 100644 --- a/src/test/java/Table/TableTest.java +++ b/src/test/java/Table/TableTest.java @@ -1,50 +1,50 @@ -//package Table; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class TableTest { -// @Test -// public void testGetWithoutAnItemReturnsNull() throws Exception { -// // Given an empty table -// Table table = new Table(); -// // When we try and get an item then it returns null -// Assert.assertEquals(table.get("foo"), null); -// } -// -// @Test -// public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // Then we should be able to get it's value -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// // And then we should be able to get it again as it wasn't removed -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// } -// -// @Test -// public void testOverwritingAnItem() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // And we put a new value with the same key -// table.put("foo", 2); -// // Then we should get back the new value -// Assert.assertEquals(table.get("foo"), new Integer(2)); -// } -// -// @Test -// public void testRemoveAnItem() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // And we remove that item -// table.remove("foo"); -// // Then we should get back null for that balue -// Assert.assertEquals(table.get("foo"), null); -// } -//} \ No newline at end of file +package Table; + +import org.junit.Assert; +import org.junit.Test; + +public class TableTest { + @Test + public void testGetWithoutAnItemReturnsNull() throws Exception { + // Given an empty table + Table table = new Table(); + // When we try and get an item then it returns null + Assert.assertEquals(table.get("foo"), null); + } + + @Test + public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { + //Given an empty table + Table table = new Table<>(); + // When we put an item in it + table.put("foo", 1); + // Then we should be able to get it's value + Assert.assertEquals(table.get("foo"), new Integer(1)); + // And then we should be able to get it again as it wasn't removed + Assert.assertEquals(table.get("foo"), new Integer(1)); + } + + @Test + public void testOverwritingAnItem() throws Exception { + //Given an empty table + Table table = new Table<>(); + // When we put an item in it + table.put("foo", 1); + // And we put a new value with the same key + table.put("foo", 2); + // Then we should get back the new value + Assert.assertEquals(table.get("foo"), new Integer(2)); + } + + @Test + public void testRemoveAnItem() throws Exception { + //Given an empty table + Table table = new Table<>(); + // When we put an item in it + table.put("foo", 1); + // And we remove that item + table.remove("foo"); + // Then we should get back null for that balue + Assert.assertEquals(table.get("foo"), null); + } +} \ No newline at end of file diff --git a/src/test/java/TableNested/TableNestedTest.java b/src/test/java/TableNested/TableNestedTest.java index 8432277..fdf12d5 100644 --- a/src/test/java/TableNested/TableNestedTest.java +++ b/src/test/java/TableNested/TableNestedTest.java @@ -1,50 +1,50 @@ -//package TableNested; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class TableNestedTest { -// @Test -// public void testGetWithoutAnItemReturnsNull() throws Exception { -// // Given an empty table -// TableNested table = new TableNested(); -// // When we try and get an item then it returns null -// Assert.assertEquals(table.get("foo"), null); -// } -// -// @Test -// public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // Then we should be able to get it's value -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// // And then we should be able to get it again as it wasn't removed -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// } -// -// @Test -// public void testOverwritingAnItem() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // And we put a new value with the same key -// table.put("foo", 2); -// // Then we should get back the new value -// Assert.assertEquals(table.get("foo"), new Integer(2)); -// } -// -// @Test -// public void testRemoveAnItem() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // And we remove that item -// table.remove("foo"); -// // Then we should get back null for that balue -// Assert.assertEquals(table.get("foo"), null); -// } -//} \ No newline at end of file +package TableNested; + +import org.junit.Assert; +import org.junit.Test; + +public class TableNestedTest { + @Test + public void testGetWithoutAnItemReturnsNull() throws Exception { + // Given an empty table + TableNested table = new TableNested(); + // When we try and get an item then it returns null + Assert.assertEquals(table.get("foo"), null); + } + + @Test + public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // Then we should be able to get it's value + Assert.assertEquals(table.get("foo"), new Integer(1)); + // And then we should be able to get it again as it wasn't removed + Assert.assertEquals(table.get("foo"), new Integer(1)); + } + + @Test + public void testOverwritingAnItem() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // And we put a new value with the same key + table.put("foo", 2); + // Then we should get back the new value + Assert.assertEquals(table.get("foo"), new Integer(2)); + } + + @Test + public void testRemoveAnItem() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // And we remove that item + table.remove("foo"); + // Then we should get back null for that balue + Assert.assertEquals(table.get("foo"), null); + } +} \ No newline at end of file