From 5f06e6677ae5d0e3d373c491ffb5e97449bd1730 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sat, 10 Mar 2018 16:57:10 -0500 Subject: [PATCH 1/8] Finished StackArrayList and StackArray, all tests passing --- src/main/java/Pair/Arrays.java | 8 +- src/main/java/StackArray/GenericStack.java | 15 +++ src/main/java/StackArray/ObjectStack.java | 14 +++ src/main/java/StackArrayList/Stack.java | 14 +++ src/main/java/Table/Table.java | 4 +- .../java/StackArray/GenericStackTest.java | 82 +++++++------- src/test/java/StackArray/ObjectStackTest.java | 78 +++++++------- src/test/java/StackArrayList/StackTest.java | 90 ++++++++-------- src/test/java/Table/TableTest.java | 100 +++++++++--------- 9 files changed, 224 insertions(+), 181 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5bdf780..5e16603 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -10,7 +10,7 @@ * A max method that returns the largest item in the arraylist * 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 class Arrays { +// public static <___> Pair firstLast(ArrayList<___> a) { +// } +//} diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index d84c4db..3401f59 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -11,5 +11,20 @@ public class GenericStack { private E[] elements; public GenericStack() { + elements= (E[]) new Object[0]; + } + + public void push(E element){ + elements= Arrays.copyOf(elements,elements.length+1); + elements[elements.length-1]=element; + elements=elements; + } + public E pop(){ + E elementPoppded = elements[elements.length-1]; + elements=Arrays.copyOf(elements,elements.length-1); + return elementPoppded; + } + public boolean isEmpty(){ + return elements.length==0; } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 1124698..c4b215b 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -11,6 +11,20 @@ public class ObjectStack { private Object[] elements; public ObjectStack() { + elements=new Object[0]; } + + public void push(Object element){ + elements=Arrays.copyOf(elements,elements.length+1); + elements[elements.length-1]=element; + } + public Object pop(){ + Object elementPopped = elements[elements.length-1]; + elements=Arrays.copyOf(elements,elements.length-1); + return elementPopped; + } + public boolean isEmpty(){ + return elements.length==0; + } } diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 0338de3..062dc24 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -11,6 +11,20 @@ public class Stack { public Stack(){ + elements = new ArrayList(); + } + + public void push(E element){ + elements.add(element); + } + public E pop(){ + E elementPopped = (E) elements.get(elements.size()-1); + elements.remove(elements.size()-1); + + return elementPopped; + } + public boolean isEmpty() { + return elements.size()==0; } } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 5ccce23..9edc12d 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -11,7 +11,7 @@ */ public class Table { private ArrayList entries; + Table table; + - public Table() { - } } 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..00dc659 100644 --- a/src/test/java/StackArrayList/StackTest.java +++ b/src/test/java/StackArrayList/StackTest.java @@ -1,45 +1,45 @@ -//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.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 diff --git a/src/test/java/Table/TableTest.java b/src/test/java/Table/TableTest.java index 15ac19c..8bebde6 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 From dede34742b18341e6072db1095e8c6d5a1397157 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sat, 10 Mar 2018 18:11:46 -0500 Subject: [PATCH 2/8] Finished the Table class, all tests passing --- src/main/java/Table/Table.java | 59 +++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 9edc12d..f14cc66 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -10,8 +10,65 @@ * Void return on `remove`. */ public class Table { - private ArrayList entries; + private ArrayList entries; Table table; + public Table() { + entries = new ArrayList(); + } + + public boolean contains(K key){ + for (Entry entry:entries) { + if(entry.getKey().equals(key)){ + return true; + } + } + return false; + } + + public V get(K key){ + for(int i=0;i Date: Sat, 10 Mar 2018 20:57:42 -0500 Subject: [PATCH 3/8] completed the Swap class --- src/main/java/StackArray/GenericStack.java | 1 - src/main/java/TableNested/TableNested.java | 76 +++++++++++++ src/test/java/Swap/SwapTest.java | 30 +++--- .../java/TableNested/TableNestedTest.java | 100 +++++++++--------- 4 files changed, 141 insertions(+), 66 deletions(-) diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index 3401f59..a1277a0 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -17,7 +17,6 @@ public GenericStack() { public void push(E element){ elements= Arrays.copyOf(elements,elements.length+1); elements[elements.length-1]=element; - elements=elements; } public E pop(){ E elementPoppded = elements[elements.length-1]; diff --git a/src/main/java/TableNested/TableNested.java b/src/main/java/TableNested/TableNested.java index 7e0dfdd..b1692f5 100644 --- a/src/main/java/TableNested/TableNested.java +++ b/src/main/java/TableNested/TableNested.java @@ -1,5 +1,7 @@ package TableNested; +import Table.Entry; + import java.util.ArrayList; /** @@ -7,5 +9,79 @@ * Think about how nested classes should work with generics. */ public class TableNested { + private ArrayList entries; + public TableNested(){ + entries = new ArrayList(); + + } + 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; + } + + } + + public boolean contains(K key){ + for (TableNested.Entry entry:entries) { + if(entry.getKey().equals(key)){ + return true; + } + } + return false; + } + + public V get(K key){ + for(int i=0;i 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 From 7b2c57052d47227420e147214cb21cbb7ec87059 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sun, 11 Mar 2018 00:20:53 -0500 Subject: [PATCH 4/8] working on ArrayListCombiner --- .../ArrayListCombiner/ArrayListCombiner.java | 18 ++++- .../ArrayListCombinerTest.java | 70 +++++++++---------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index d302cd2..2a29cd7 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -1,6 +1,7 @@ package ArrayListCombiner; import java.util.ArrayList; +import java.util.Collections; /** * Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items, @@ -8,5 +9,20 @@ * The first method should be called extendCombiner and should use ? extends E * The second method should be called superCombiner and should use ? super E */ -public class ArrayListCombiner { +public class ArrayListCombiner { + private ArrayList myArrayList; + + public ArrayListCombiner(){ + + } + + public static ArrayList extendCombiner(ArrayList first, ArrayListsecond){ + Collections.copy(first, second); + return first; + } + + public static ArrayList superCombiner(ArrayListfirst,ArrayListsecond){ + Collections.copy(second,first); + return second; + } } diff --git a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java index 957a878..9e16484 100644 --- a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java +++ b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java @@ -9,40 +9,40 @@ 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 9a8e11c1309baf0c4f7b24b089ce5a2444c64656 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sun, 11 Mar 2018 12:27:13 -0400 Subject: [PATCH 5/8] Finished ArrayListCombiner and MapFunction --- .../ArrayListCombiner/ArrayListCombiner.java | 15 ++-- src/main/java/MapFunc/MapFunc.java | 9 +++ src/test/java/MapFunc/MapFuncTest.java | 72 +++++++++---------- 3 files changed, 53 insertions(+), 43 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index 2a29cd7..6fcf52b 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -3,26 +3,27 @@ import java.util.ArrayList; import java.util.Collections; +import static java.util.Collections.copy; + /** * Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items, * to the first. Use a wildcard for one of the type arguments in each method. * The first method should be called extendCombiner and should use ? extends E * The second method should be called superCombiner and should use ? super E */ -public class ArrayListCombiner { - private ArrayList myArrayList; +public class ArrayListCombiner { public ArrayListCombiner(){ } - public static ArrayList extendCombiner(ArrayList first, ArrayListsecond){ - Collections.copy(first, second); + public static ArrayList extendCombiner(ArrayList first, ArrayListsecond){ + first.addAll(second); return first; } - public static ArrayList superCombiner(ArrayListfirst,ArrayListsecond){ - Collections.copy(second,first); - return second; + public static ArrayList superCombiner(ArrayListfirst,ArrayListsecond){ + first.addAll(second); + return first; } } diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index ed4bf66..bf4da7b 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -9,4 +9,13 @@ */ public class MapFunc { + + public static ArrayList map(ArrayListoriginalArrayList, Function functionToapply){ + ArrayList mappedArrayList = new ArrayList<>(); + for(int i=0;i 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 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 From 4d0d4a8c580ad8eb418307960f57366f1e2261d3 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sun, 11 Mar 2018 12:57:11 -0400 Subject: [PATCH 6/8] Implemented the getters and min, max methods for Pair --- src/main/java/MapFunc/MapFunc.java | 6 +-- src/main/java/Pair/Pair.java | 27 ++++++++++++- src/test/java/Pair/PairTest.java | 64 +++++++++++++++--------------- 3 files changed, 61 insertions(+), 36 deletions(-) diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index bf4da7b..697fb3d 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -10,10 +10,10 @@ public class MapFunc { - public static ArrayList map(ArrayListoriginalArrayList, Function functionToapply){ + public static ArrayList map(ArrayListinput, Function functionToapply){ ArrayList mappedArrayList = new ArrayList<>(); - for(int i=0;i 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; + + 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(){ + if(first.compareTo(second)==1){ + return second; + } else return first; + } + public E max(){ + return first.compareTo(second)==1 ? first : second; + } + } diff --git a/src/test/java/Pair/PairTest.java b/src/test/java/Pair/PairTest.java index d616178..baf2071 100644 --- a/src/test/java/Pair/PairTest.java +++ b/src/test/java/Pair/PairTest.java @@ -1,32 +1,32 @@ -//package Pair; -// -//import org.junit.Test; -//import org.junit.Assert; -// -//public class PairTest { -// -// @Test -// public void testGetters() throws Exception { -// // Given a pair with "Foo" and "Bar" -// Pair p = new Pair("Foo", "Bar"); -// // When getFirst and getSecond are called, they should be returned. -// Assert.assertEquals("Foo", p.getFirst()); -// Assert.assertEquals("Bar", p.getSecond()); -// } -// -// @Test -// public void testMin() throws Exception { -// // Given a pair with two values -// Pair p = new Pair(1.23, 2.34); -// // When p.min() is called, the smallest should be returned. -// Assert.assertEquals(new Double(1.23), p.min()); -// } -// -// @Test -// public void testMax() throws Exception { -// // Given a pair with two values -// Pair p = new Pair(1.23, 2.34); -// // When p.max() is called, the largest should be returned. -// Assert.assertEquals(new Double(2.34), p.max()); -// } -//} \ No newline at end of file +package Pair; + +import org.junit.Test; +import org.junit.Assert; + +public class PairTest { + + @Test + public void testGetters() throws Exception { + // Given a pair with "Foo" and "Bar" + Pair p = new Pair("Foo", "Bar"); + // When getFirst and getSecond are called, they should be returned. + Assert.assertEquals("Foo", p.getFirst()); + Assert.assertEquals("Bar", p.getSecond()); + } + + @Test + public void testMin() throws Exception { + // Given a pair with two values + Pair p = new Pair(1.23, 2.34); + // When p.min() is called, the smallest should be returned. + Assert.assertEquals(new Double(1.23), p.min()); + } + + @Test + public void testMax() throws Exception { + // Given a pair with two values + Pair p = new Pair(1.23, 2.34); + // When p.max() is called, the largest should be returned. + Assert.assertEquals(new Double(2.34), p.max()); + } +} \ No newline at end of file From 50f99beeaf008a8367916759eac35b8fd8bfbbb7 Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sun, 11 Mar 2018 13:42:49 -0400 Subject: [PATCH 7/8] completed Pair --- src/main/java/Pair/Arrays.java | 25 ++++- src/main/java/Pair/Pair.java | 3 +- src/test/java/Pair/ArraysTest.java | 148 ++++++++++++++--------------- 3 files changed, 97 insertions(+), 79 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5e16603..3b07af5 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -10,7 +10,24 @@ * A max method that returns the largest item in the arraylist * 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 class Arrays { + public Arrays myArray = new Arrays(); + public static Pair firstLast(ArrayList a) { + return new Pair(a.get(0),a.get(a.size()-1)); + } + + public static E min(ArrayList inputArrayList){ + Collections.sort(inputArrayList); + return inputArrayList.get(0); + } + + public static E max(ArrayList inputArrayList){ + Collections.sort(inputArrayList); + return inputArrayList.get(inputArrayList.size()-1); + } + + public static Pair minMax(ArrayListinputArrayList){ + Collections.sort(inputArrayList); + return firstLast(inputArrayList); + } +} diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index 007aa1b..45559c7 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -27,7 +27,8 @@ public E getSecond(){ public E min(){ if(first.compareTo(second)==1){ return second; - } else return first; + } + return first; } public E max(){ return first.compareTo(second)==1 ? first : second; diff --git a/src/test/java/Pair/ArraysTest.java b/src/test/java/Pair/ArraysTest.java index 4d32e23..0d51970 100644 --- a/src/test/java/Pair/ArraysTest.java +++ b/src/test/java/Pair/ArraysTest.java @@ -1,74 +1,74 @@ -//package Pair; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//import java.util.ArrayList; -// -//public class ArraysTest { -// @Test -// public void firstLast() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When firstLast is called -// Pair result = Arrays.firstLast(al); -// // Then it should return the first and last items -// Assert.assertEquals(new Integer(1), result.getFirst()); -// Assert.assertEquals(new Integer(1000), result.getSecond()); -// } -// -// @Test -// public void testMin() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When min is called assert that it gets the smallest item -// Assert.assertEquals(new Integer(0), Arrays.min(al)); -// } -// -// @Test -// public void testMax() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When min is called assert that it gets the largest item -// Assert.assertEquals(new Integer(1000), Arrays.max(al)); -// } -// -// @Test -// public void testMinMax() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When minMax is called -// Pair result = Arrays.minMax(al); -// // Then it should return the first and last items -// Assert.assertEquals(new Integer(0), result.min()); -// Assert.assertEquals(new Integer(1000), result.max()); -// } -//} +package Pair; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +public class ArraysTest { + @Test + public void firstLast() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When firstLast is called + Pair result = Arrays.firstLast(al); + // Then it should return the first and last items + Assert.assertEquals(new Integer(1), result.getFirst()); + Assert.assertEquals(new Integer(1000), result.getSecond()); + } + + @Test + public void testMin() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When min is called assert that it gets the smallest item + Assert.assertEquals(new Integer(0), Arrays.min(al)); + } + + @Test + public void testMax() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When min is called assert that it gets the largest item + Assert.assertEquals(new Integer(1000), Arrays.max(al)); + } + + @Test + public void testMinMax() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When minMax is called + Pair result = Arrays.minMax(al); + // Then it should return the first and last items + Assert.assertEquals(new Integer(0), result.min()); + Assert.assertEquals(new Integer(1000), result.max()); + } +} From 192c84c13dcc2b86d358bbdcb78d73b0384758fd Mon Sep 17 00:00:00 2001 From: Kibret Tecle Date: Sun, 11 Mar 2018 16:56:26 -0400 Subject: [PATCH 8/8] making chages --- src/main/java/ArrayListCombiner/ArrayListCombiner.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index 6fcf52b..579e5af 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -19,6 +19,8 @@ public ArrayListCombiner(){ public static ArrayList extendCombiner(ArrayList first, ArrayListsecond){ first.addAll(second); + + return first; }