From 4802a203ee6c724251a37f5a4b2a18577bc2824f Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sat, 10 Mar 2018 13:26:10 -0500 Subject: [PATCH 1/7] Finished Stack --- src/main/java/Pair/Arrays.java | 32 ++++---- src/main/java/StackArrayList/Stack.java | 18 ++++- src/test/java/StackArrayList/StackTest.java | 90 ++++++++++----------- 3 files changed, 78 insertions(+), 62 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5bdf780..502e04a 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -1,16 +1,16 @@ -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 - * element in the arraylist. - * You must also make a min method that returns the smallest item in the array list - * 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) { - } -} +//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 +// * element in the arraylist. +// * You must also make a min method that returns the smallest item in the array list +// * 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) { +// } +//} diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 0338de3..e119539 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -1,16 +1,32 @@ package StackArrayList; import java.util.ArrayList; +import java.util.Collections; /** * 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 ArrayList elements; public Stack(){ + this.elements = new ArrayList(); + } + + public E push(E element) { + this.elements.add(element); + return element; + } + + public boolean isEmpty() { + return this.elements.isEmpty(); + } + public E pop() throws IndexOutOfBoundsException{ + Collections.reverse(this.elements); + E element = (E )this.elements.remove( 0); + return element; } } 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 From 0a25f2749e78443b1e2cdb17f85ecbb3edb990f2 Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sat, 10 Mar 2018 17:39:23 -0500 Subject: [PATCH 2/7] Finished Table/Nested Table --- src/main/java/StackArray/GenericStack.java | 24 +++++ src/main/java/StackArray/ObjectStack.java | 24 +++++ src/main/java/Table/Entry.java | 11 ++ src/main/java/Table/Table.java | 33 +++++- src/main/java/TableNested/TableNested.java | 65 ++++++++++++ .../java/StackArray/GenericStackTest.java | 82 +++++++------- src/test/java/StackArray/ObjectStackTest.java | 78 +++++++------- src/test/java/Table/TableTest.java | 100 +++++++++--------- .../java/TableNested/TableNestedTest.java | 100 +++++++++--------- 9 files changed, 336 insertions(+), 181 deletions(-) diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index d84c4db..8e333a4 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -8,8 +8,32 @@ * @param */ public class GenericStack { + private E[] elements; + Integer actualSize = 0; public GenericStack() { + this.elements = (E[]) new Object[2]; + } + + public boolean isEmpty() { + return actualSize == 0; + } + + public void push(E element) { + + if (this.actualSize >= this.elements.length -1) { + this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length); + } + + this.elements[actualSize++] = element; + + } + + public E pop() throws IndexOutOfBoundsException{ + E element = this.elements[actualSize-1]; + this.elements[actualSize-1] = null; + actualSize--; + return element; } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 1124698..cdf3599 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -8,9 +8,33 @@ * @param */ public class ObjectStack { + private Object[] elements; + Integer actualSize = 0; public ObjectStack() { + this.elements = new Object[2]; + + } + + public boolean isEmpty() { + return actualSize == 0; + } + + public void push(E element) { + + if (this.actualSize >= this.elements.length -1) { + this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length); + } + + this.elements[actualSize++] = element; + + } + public Object pop() throws IndexOutOfBoundsException{ + Object element = this.elements[actualSize-1]; + this.elements[actualSize-1] = null; + actualSize--; + return element; } } diff --git a/src/main/java/Table/Entry.java b/src/main/java/Table/Entry.java index 8c97478..53d172f 100644 --- a/src/main/java/Table/Entry.java +++ b/src/main/java/Table/Entry.java @@ -1,6 +1,7 @@ package Table; public class Entry { + private K key; private V value; @@ -17,4 +18,14 @@ public V getValue() { return value; } + public void setValue(V value) { + this.value = value; + } + + + public void setKey(K key) { + this.key = key; + } + + } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 5ccce23..2837689 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -10,8 +10,39 @@ * Void return on `remove`. */ public class Table { - private ArrayList entries; + private ArrayList entries; public Table() { + this.entries = new ArrayList<>(); } + + public V get(K key) { + for (Entry entry : entries){ + if (entry.getKey().equals(key)){ + return (V) entry.getValue(); + + } + } + return null; + } + + public void put(K key, V value) { + for(Entry entry : entries){ + entry.setKey(key); + entry.setValue(value); + return; + } + + entries.add(new Entry(key, value)); + } + + public void remove(K key) { + for(Entry entry : entries){ + if (key.equals(entry.getKey())){ + entries.remove(entry); + return; + } + } + } + } diff --git a/src/main/java/TableNested/TableNested.java b/src/main/java/TableNested/TableNested.java index 7e0dfdd..ed884a1 100644 --- a/src/main/java/TableNested/TableNested.java +++ b/src/main/java/TableNested/TableNested.java @@ -8,4 +8,69 @@ */ public class TableNested { + private ArrayList entries; + + public TableNested() { + this.entries = new ArrayList<>(); + } + + + 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; + } + + public void setValue(V value) { + this.value = value; + } + + + public void setKey(K key) { + this.key = key; + } + } + + + public void remove(K key) { + for (Entry entry : entries) { + if (key.equals(entry.getKey())) { + entries.remove(entry); + return; + } + } + } + + public V get(K key) { + for (Entry entry : entries) { + if (entry.getKey().equals(key)) { + return (V) entry.getValue(); + + } + } + return null; + } + + public void put(K key, V value) { + for (Entry entry : entries) { + entry.setKey(key); + entry.setValue(value); + return; + } + + entries.add(new Entry(key, value)); + } + } 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/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 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 From aa588697a2bf55df5509bdc07e3ffb51835f8068 Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sat, 10 Mar 2018 18:13:19 -0500 Subject: [PATCH 3/7] Finished Swap --- src/test/java/Swap/SwapTest.java | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/test/java/Swap/SwapTest.java b/src/test/java/Swap/SwapTest.java index 2583b9d..7cd6a14 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 From 5332958f888495a86897ff555b7e06abb5694b9e Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sat, 10 Mar 2018 21:27:57 -0500 Subject: [PATCH 4/7] Finished ArrayListCombiner --- .../ArrayListCombiner/ArrayListCombiner.java | 10 +++ .../ArrayListCombinerTest.java | 70 +++++++++---------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index d302cd2..df58aaa 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -1,5 +1,8 @@ package ArrayListCombiner; +import Employee.Employee; +import Employee.Manager; + import java.util.ArrayList; /** @@ -9,4 +12,11 @@ * The second method should be called superCombiner and should use ? super E */ public class ArrayListCombiner { + public static void extendCombiner(ArrayList first, ArrayList second) { + first.addAll(second); + } + + public static void superCombiner(ArrayList first, ArrayList second) { + first.addAll(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 f449b25caa1126a9ceefa9719826ae6796acd37b Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sat, 10 Mar 2018 22:56:55 -0500 Subject: [PATCH 5/7] Working on Pair and MapFunc --- src/main/java/MapFunc/MapFunc.java | 16 ++- src/main/java/Pair/Arrays.java | 32 +++--- src/main/java/Pair/Pair.java | 5 +- src/test/java/MapFunc/MapFuncTest.java | 72 ++++++------ src/test/java/Pair/ArraysTest.java | 148 ++++++++++++------------- src/test/java/Pair/PairTest.java | 64 +++++------ 6 files changed, 177 insertions(+), 160 deletions(-) diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index ed4bf66..6c5512f 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -7,6 +7,20 @@ * Create a function called `map` that takes an ArrayList and a `Function` object, * and returns an ArrayList with all of the elements of the first after the function is applied to them. */ -public class MapFunc { +public class MapFunc { + private ArrayList mapArray; + + public MapFunc() { + this.mapArray = new ArrayList<>(); + } + + public static ArrayList map(ArrayList mapArray, Function mapFunction){ + ArrayList funcArray = new ArrayList(); + for (int i = 0; i < mapArray.size(); i++ ){ + mapFunction.apply(i); + funcArray.add(i); + } + return funcArray; + } } diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 502e04a..5bdf780 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -1,16 +1,16 @@ -//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 -// * element in the arraylist. -// * You must also make a min method that returns the smallest item in the array list -// * 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) { -// } -//} +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 + * element in the arraylist. + * You must also make a min method that returns the smallest item in the array list + * 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) { + } +} diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index c4dd905..68e1af8 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -7,6 +7,9 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair { + public T min() { + return null; + } } diff --git a/src/test/java/MapFunc/MapFuncTest.java b/src/test/java/MapFunc/MapFuncTest.java index 3c7534f..fdc6401 100644 --- a/src/test/java/MapFunc/MapFuncTest.java +++ b/src/test/java/MapFunc/MapFuncTest.java @@ -1,36 +1,36 @@ -//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 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 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()); + } +} 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 f5ea9ee1a0e28fac2a3dd8d86bbc40525d339762 Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sun, 11 Mar 2018 13:07:33 -0400 Subject: [PATCH 6/7] Finished Map Function --- src/main/java/MapFunc/MapFunc.java | 27 +++-- src/main/java/Pair/Arrays.java | 5 +- src/main/java/Pair/Pair.java | 43 ++++++- src/test/java/MapFunc/MapFuncTest.java | 3 +- src/test/java/Pair/ArraysTest.java | 148 ++++++++++++------------- 5 files changed, 134 insertions(+), 92 deletions(-) diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index 6c5512f..348eeaf 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -7,20 +7,23 @@ * Create a function called `map` that takes an ArrayList and a `Function` object, * and returns an ArrayList with all of the elements of the first after the function is applied to them. */ -public class MapFunc { +public class MapFunc { - private ArrayList mapArray; - - public MapFunc() { - this.mapArray = new ArrayList<>(); - } - - public static ArrayList map(ArrayList mapArray, Function mapFunction){ + public static ArrayList map(ArrayList mapArray, Function mapFunction) { ArrayList funcArray = new ArrayList(); - for (int i = 0; i < mapArray.size(); i++ ){ - mapFunction.apply(i); - funcArray.add(i); + for (T mapArrayValue : mapArray) { + R changedValue = mapFunction.apply(mapArrayValue); + funcArray.add(changedValue); + } - return funcArray; + return funcArray; } + + /* For this part you need to create a generic method and give it the type parameter of + because the function uses T for the type being passed in. Then R for the return type. + The method then returns ArrayList of type R because this is the array that had the function applied to it. + The argument being passed in is of type T being the original array being passed into the function. + + */ } + diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5bdf780..c28c12d 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 68e1af8..d4da133 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -1,5 +1,6 @@ package Pair; + /** * You need to store two values of type `E`, set them in a constructor, and have the following methods, * getFirst @@ -7,9 +8,47 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair> implements Comparable{ + + private E first; + private E second; + + public Pair(E first, E second){ + this.first = first; + this.second = second; + + } + + public E getFirst() { + return this.first; + } + + public E getSecond() { + return this.second; + } + + public E min() { + if (compareTo(second) < 0){ + return this.first; + + } else if (compareTo(second) > 0){ + return this.second; + } + return null; + } + + public E max(){ + if (compareTo(second) < 0){ + return this.second; - public T min() { + } else if (compareTo(second) > 0){ + return this.first; + } return null; } + + @Override + public int compareTo(E values) { + return first.compareTo(values); + } } diff --git a/src/test/java/MapFunc/MapFuncTest.java b/src/test/java/MapFunc/MapFuncTest.java index fdc6401..19988b3 100644 --- a/src/test/java/MapFunc/MapFuncTest.java +++ b/src/test/java/MapFunc/MapFuncTest.java @@ -1,6 +1,5 @@ package MapFunc; -import MapFunc.MapFunc; import org.junit.Test; import java.util.ArrayList; @@ -14,7 +13,7 @@ public void testSingleTypeMap() throws Exception { 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 */ ); + 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)); diff --git a/src/test/java/Pair/ArraysTest.java b/src/test/java/Pair/ArraysTest.java index 0d51970..4d32e23 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 249e7ed56c8b75f3b0e30f0890d5677afd332085 Mon Sep 17 00:00:00 2001 From: CWinarski Date: Sun, 11 Mar 2018 14:46:41 -0400 Subject: [PATCH 7/7] Finished Lab --- src/main/java/MapFunc/MapFunc.java | 1 - src/main/java/Pair/Arrays.java | 27 +++- src/main/java/Pair/Pair.java | 16 +-- src/main/java/StackArray/GenericStack.java | 13 +- src/main/java/StackArray/ObjectStack.java | 9 +- src/main/java/StackArrayList/Stack.java | 6 +- src/main/java/Table/Table.java | 12 +- src/test/java/Pair/ArraysTest.java | 148 ++++++++++----------- 8 files changed, 127 insertions(+), 105 deletions(-) diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index 348eeaf..99eea78 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -23,7 +23,6 @@ public static ArrayList map(ArrayList mapArray, Function mapF because the function uses T for the type being passed in. Then R for the return type. The method then returns ArrayList of type R because this is the array that had the function applied to it. The argument being passed in is of type T being the original array being passed into the function. - */ } diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index c28c12d..6b04867 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -11,7 +11,28 @@ * 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) { -// return null; -// } + public static > Pair firstLast(ArrayList a) { + E first = a.get(0); + E last = a.get(a.size() - 1); + Pair firstLast = new Pair<>(first, last); + return firstLast; + } + + public static > E min(ArrayList a) { + E minimumValue = Collections.min(a); + return minimumValue; + } + + public static > E max(ArrayList a) { + E maximumValue = Collections.max(a); + return maximumValue; + } + + public static > Pair minMax(ArrayList a) { + E maximumValue = Collections.max(a); + E minimumValue = Collections.min(a); + Pair minMax = new Pair<>(maximumValue, minimumValue); + return minMax; + } + } diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index d4da133..14137d8 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -8,12 +8,12 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair> implements Comparable{ +public class Pair> implements Comparable { private E first; private E second; - public Pair(E first, E second){ + public Pair(E first, E second) { this.first = first; this.second = second; @@ -27,21 +27,21 @@ public E getSecond() { return this.second; } - public E min() { - if (compareTo(second) < 0){ + public E min() { + if (compareTo(second) < 0) { return this.first; - } else if (compareTo(second) > 0){ + } else if (compareTo(second) > 0) { return this.second; } return null; } - public E max(){ - if (compareTo(second) < 0){ + public E max() { + if (compareTo(second) < 0) { return this.second; - } else if (compareTo(second) > 0){ + } else if (compareTo(second) > 0) { return this.first; } return null; diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index 8e333a4..97a7286 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -5,6 +5,7 @@ /** * 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 { @@ -22,7 +23,7 @@ public boolean isEmpty() { public void push(E element) { - if (this.actualSize >= this.elements.length -1) { + if (this.actualSize >= this.elements.length - 1) { this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length); } @@ -30,10 +31,10 @@ public void push(E element) { } - public E pop() throws IndexOutOfBoundsException{ - E element = this.elements[actualSize-1]; - this.elements[actualSize-1] = null; - actualSize--; - return element; + public E pop() throws IndexOutOfBoundsException { + E element = this.elements[actualSize - 1]; + this.elements[actualSize - 1] = null; + actualSize--; + return element; } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index cdf3599..99dc8f7 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -5,6 +5,7 @@ /** * 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 { @@ -23,7 +24,7 @@ public boolean isEmpty() { public void push(E element) { - if (this.actualSize >= this.elements.length -1) { + if (this.actualSize >= this.elements.length - 1) { this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length); } @@ -31,9 +32,9 @@ public void push(E element) { } - public Object pop() throws IndexOutOfBoundsException{ - Object element = this.elements[actualSize-1]; - this.elements[actualSize-1] = null; + public Object pop() throws IndexOutOfBoundsException { + Object element = this.elements[actualSize - 1]; + this.elements[actualSize - 1] = null; actualSize--; return element; } diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index e119539..265f17d 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -11,7 +11,7 @@ public class Stack { private ArrayList elements; - public Stack(){ + public Stack() { this.elements = new ArrayList(); } @@ -24,9 +24,9 @@ public boolean isEmpty() { return this.elements.isEmpty(); } - public E pop() throws IndexOutOfBoundsException{ + public E pop() throws IndexOutOfBoundsException { Collections.reverse(this.elements); - E element = (E )this.elements.remove( 0); + E element = (E) this.elements.remove(0); return element; } } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 2837689..6d5ecc7 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -17,9 +17,9 @@ public Table() { } public V get(K key) { - for (Entry entry : entries){ - if (entry.getKey().equals(key)){ - return (V) entry.getValue(); + for (Entry entry : entries) { + if (entry.getKey().equals(key)) { + return (V) entry.getValue(); } } @@ -27,7 +27,7 @@ public V get(K key) { } public void put(K key, V value) { - for(Entry entry : entries){ + for (Entry entry : entries) { entry.setKey(key); entry.setValue(value); return; @@ -37,8 +37,8 @@ public void put(K key, V value) { } public void remove(K key) { - for(Entry entry : entries){ - if (key.equals(entry.getKey())){ + for (Entry entry : entries) { + if (key.equals(entry.getKey())) { entries.remove(entry); return; } 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()); + } +}