From 60130a81a0227f6fe354a0b03b7ccd85aa1f913f Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 11:08:56 -0400 Subject: [PATCH 1/7] part 1 & 2 --- src/main/java/Pair/Arrays.java | 32 ++++---- src/main/java/StackArray/ObjectStack.java | 36 +++++---- src/main/java/StackArrayList/Stack.java | 20 +++++ src/test/java/StackArrayList/StackTest.java | 90 ++++++++++----------- 4 files changed, 101 insertions(+), 77 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/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 1124698..5538c76 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -1,16 +1,20 @@ -package StackArray; - -import java.util.Arrays; - -/** - * 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 { - private Object[] elements; - - public ObjectStack() { - - } -} +//package StackArray; +// +//import java.util.Arrays; +// +///** +// * 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 { +// private Object[] elements; +// +// public ObjectStack() { +// this.elements = new Object[]; +// } +// //the way stacks work, its just like generics accept you are using Objects +// public Object push(Object value){ +// +// } +//} diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 0338de3..be157c6 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -7,10 +7,30 @@ * If you pop on an empty stack, throw an IndexOutOfBoundsException. */ public class Stack { + private ArrayList elements; public Stack(){ + this.elements = new ArrayList(); + } + + public void push(E value){ + //pushes adds an element + this.elements.add(value); + } + + public E pop(){ + //removes an element + return (E) elements.remove(this.elements.size()-1); + } + + public boolean isEmpty(){ + //Checks to see is the elements array is empty, if its equal to 0 its empty. + return (elements.size()==0); + } + public E pop() { + return (E) this.elements; } } 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 75c1c7887253ec470cbd5d83d12a33d90b8a2344 Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 11:52:27 -0400 Subject: [PATCH 2/7] onto part3 --- src/main/java/StackArray/GenericStack.java | 26 ++++++ src/main/java/StackArray/ObjectStack.java | 55 ++++++++----- src/main/java/StackArrayList/Stack.java | 4 +- .../java/StackArray/GenericStackTest.java | 82 +++++++++---------- src/test/java/StackArray/ObjectStackTest.java | 78 +++++++++--------- 5 files changed, 142 insertions(+), 103 deletions(-) diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index d84c4db..85be25e 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -11,5 +11,31 @@ public class GenericStack { private E[] elements; public GenericStack() { + this.elements = (E[]) new Object[0]; + } + + public E push(E value){ + //needs to remove an element from E[] and resize + //now I just need to push the value onto the end of the array + E[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1); + newArray[newArray.length-1] = value; + this.elements = newArray; + return value; + + } + + public E pop(){ + //according to the api, pop should return a value + //didn't need to assign the value + E value = this.elements[elements.length-1]; + E[] newArray = Arrays.copyOf(elements, elements.length - 1); + //newArray[newArray.length-1] = value; + this.elements = newArray; + return value; + } + + public boolean isEmpty(){ + return (elements.length==0); + } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 5538c76..e8d0c4f 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -1,20 +1,35 @@ -//package StackArray; -// -//import java.util.Arrays; -// -///** -// * 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 { -// private Object[] elements; -// -// public ObjectStack() { -// this.elements = new Object[]; -// } -// //the way stacks work, its just like generics accept you are using Objects -// public Object push(Object value){ -// -// } -//} +package StackArray; + +import java.util.Arrays; + +/** + * 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 { + private Object[] elements; + + public ObjectStack() { + this.elements = new Object[0]; + } + //the way stacks work, its just like generics accept you are using Objects + public Object push(Object value){ + Object[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1); + newArray[newArray.length-1] = value; + this.elements = newArray; + return value; + } + + public Object pop(){ + Object value = this.elements[elements.length-1]; + Object[] newArray = Arrays.copyOf(elements, elements.length - 1); + //newArray[newArray.length-1] = value; + this.elements = newArray; + return value; + } + + public boolean isEmpty(){ + return(elements.length==0); + } +} diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index be157c6..f7013a4 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -30,7 +30,5 @@ public boolean isEmpty(){ return (elements.size()==0); } - public E pop() { - return (E) this.elements; - } + } 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 From 159e6e3bb76a2b0fa10c738993a78115494a5f7c Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 13:26:51 -0400 Subject: [PATCH 3/7] on part 4 --- src/main/java/Table/Entry.java | 2 + src/main/java/Table/Table.java | 42 +++++++++++- src/test/java/Table/TableTest.java | 100 ++++++++++++++--------------- 3 files changed, 93 insertions(+), 51 deletions(-) diff --git a/src/main/java/Table/Entry.java b/src/main/java/Table/Entry.java index 8c97478..c2c73b7 100644 --- a/src/main/java/Table/Entry.java +++ b/src/main/java/Table/Entry.java @@ -10,10 +10,12 @@ public Entry(K key, V value) { } public K getKey() { + return key; } public V getValue() { + return value; } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 5ccce23..18e4cbd 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -10,8 +10,48 @@ * Void return on `remove`. */ public class Table { - private ArrayList entries; + private ArrayList entries; public Table() { + this.entries = new ArrayList<>(); } + + public Object get(K key){ + for(Entry e : entries){ + if(e.getKey().equals(key)){ + return e.getValue(); + } + } + return null; + } + + public void put(K key, V value){ + //need to enter value and key into entry + int counter = 0; + for(Entry e : entries){ + if(e.getKey().equals(key)){ + entries.set(counter, new Entry<>(key, value)); + return; + } + } + entries.add(new Entry<>(key, value)); + } + + public void remove(K key){ + //needs to take a key, and if that key exists in the arraylist, remove the item + for(Entry e : entries){ + if(e.getKey().equals(key)){ + entries.remove(key); + return; + } + } + + + } + + } + + +//Steps +//made the ArrayList Manage the Entry entries 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 8f9590d49aeea4f8f891e1b1027fe5bc98b45478 Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 13:33:08 -0400 Subject: [PATCH 4/7] done part 4 --- src/main/java/Table/Entry.java | 2 - src/main/java/Table/Table.java | 2 - src/main/java/TableNested/TableNested.java | 57 ++++++++++ .../java/TableNested/TableNestedTest.java | 100 +++++++++--------- 4 files changed, 107 insertions(+), 54 deletions(-) diff --git a/src/main/java/Table/Entry.java b/src/main/java/Table/Entry.java index c2c73b7..8c97478 100644 --- a/src/main/java/Table/Entry.java +++ b/src/main/java/Table/Entry.java @@ -10,12 +10,10 @@ public Entry(K key, V value) { } public K getKey() { - return key; } public V getValue() { - return value; } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 18e4cbd..c1005fe 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -53,5 +53,3 @@ public void remove(K key){ } -//Steps -//made the ArrayList Manage the Entry entries diff --git a/src/main/java/TableNested/TableNested.java b/src/main/java/TableNested/TableNested.java index 7e0dfdd..5698881 100644 --- a/src/main/java/TableNested/TableNested.java +++ b/src/main/java/TableNested/TableNested.java @@ -7,5 +7,62 @@ * Think about how nested classes should work with generics. */ public class TableNested { + private ArrayList entries; + public TableNested() { + this.entries = new ArrayList<>(); + } + + public Object get(K key) { + for (Table.Entry e : entries) { + if (e.getKey().equals(key)) { + return e.getValue(); + } + } + return null; + } + + public void put(K key, V value) { + //need to enter value and key into entry + int counter = 0; + for (Table.Entry e : entries) { + if (e.getKey().equals(key)) { + entries.set(counter, new Table.Entry<>(key, value)); + return; + } + } + entries.add(new Table.Entry<>(key, value)); + } + + public void remove(K key) { + //needs to take a key, and if that key exists in the arraylist, remove the item + for (Table.Entry e : entries) { + if (e.getKey().equals(key)) { + entries.remove(e); + return; + } + } + + + } + + + public class Entry { + private K key; + private V value; + + public Entry(K key, V value) { + this.key = key; + this.value = value; + } + + public K getKey() { + return key; + } + + public V getValue() { + return value; + } + + } } diff --git a/src/test/java/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 e52779d10535feb1defdeff78c148ee83aa88965 Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 14:43:51 -0400 Subject: [PATCH 5/7] working on pt 7 --- .../ArrayListCombiner/ArrayListCombiner.java | 17 +++++ src/main/java/MapFunc/MapFunc.java | 6 ++ src/main/java/Swap/Swap.java | 6 ++ .../ArrayListCombinerTest.java | 70 +++++++++--------- src/test/java/MapFunc/MapFuncTest.java | 72 +++++++++---------- src/test/java/Swap/SwapTest.java | 38 ++++++---- 6 files changed, 123 insertions(+), 86 deletions(-) diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index d302cd2..ffabb29 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -1,5 +1,6 @@ package ArrayListCombiner; +import java.lang.reflect.Array; import java.util.ArrayList; /** @@ -9,4 +10,20 @@ * 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); + } } + + +//to describe static in my head.... +//you don't need the juice to have the sauce +//so for example, a class is the sauce...makes a meal what it is +//an object is the juice, great flavor but not what you want with everymeal +//if you check out the test, and you notice its not instantiated you don't need the juice +//because the test already has the sauce diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index ed4bf66..6476ca6 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -9,4 +9,10 @@ */ public class MapFunc { + //okay katrice, so public method is because this test does not create a instance of MapFunc + //The when its static you have to define the generic so you can pass it into a static + //We are returning an Arraylist of the results and they take in an ArrayList of type T + public static ArrayList map(ArrayList, Function){ + + } } diff --git a/src/main/java/Swap/Swap.java b/src/main/java/Swap/Swap.java index 19984a5..fdd9639 100644 --- a/src/main/java/Swap/Swap.java +++ b/src/main/java/Swap/Swap.java @@ -11,3 +11,9 @@ public static T[] swap(int i, int j, T... values) { return values; } } + +//Explanation +//Stating Generic T is equal to the indexes i of values +//those indexes are also equal to the indexes j of values +//and index j of values equals temp +//now return values \ No newline at end of file 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 diff --git a/src/test/java/MapFunc/MapFuncTest.java b/src/test/java/MapFunc/MapFuncTest.java index 3c7534f..a3fdf58 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/Swap/SwapTest.java b/src/test/java/Swap/SwapTest.java index 2583b9d..ba982dc 100644 --- a/src/test/java/Swap/SwapTest.java +++ b/src/test/java/Swap/SwapTest.java @@ -1,16 +1,24 @@ 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); + } +} + +//Explanation +//Stating Generic T is equal to the indexes i of values +//those indexes are also equal to the indexes j of values +//and index j of values equals temp +//now return values + +//so 1.5 is value i & 2.3 is value j \ No newline at end of file From 8aedfcd1e9e091627967d394ece4c8755a9f085e Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 15:10:51 -0400 Subject: [PATCH 6/7] done pt 7, onto pt 8 --- src/main/java/MapFunc/MapFunc.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index 6476ca6..442457b 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -12,7 +12,13 @@ public class MapFunc { //okay katrice, so public method is because this test does not create a instance of MapFunc //The when its static you have to define the generic so you can pass it into a static //We are returning an Arraylist of the results and they take in an ArrayList of type T - public static ArrayList map(ArrayList, Function){ + public static ArrayList map(ArrayList lists, Function function){ + ArrayList result = new ArrayList<>(); + for(T item: lists){ + R entry = function.apply(item); + result.add(entry); + } + return result; } } From e972bbaba53dd5f3af6ba5f2df604de783d9cf4c Mon Sep 17 00:00:00 2001 From: Katrice Williams-Dredden Date: Mon, 12 Mar 2018 16:02:27 -0400 Subject: [PATCH 7/7] done --- src/main/java/Pair/Arrays.java | 54 +++++++---- src/main/java/Pair/Pair.java | 29 +++++- src/test/java/Pair/ArraysTest.java | 148 ++++++++++++++--------------- src/test/java/Pair/PairTest.java | 64 ++++++------- 4 files changed, 172 insertions(+), 123 deletions(-) diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 502e04a..2fd1964 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -1,16 +1,38 @@ -//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) { + return new Pair(a.get(0), a.get(a.size()-1)); + } + + + public static E min(ArrayList al) { + + Collections.sort(al); + return al.get(0); + } + + + public static E max(ArrayList al) { + + Collections.sort(al); + return al.get(al.size()-1); + } + + + public static Pair minMax(ArrayList al) { + + return new Pair(min(al), max(al)); + + } +} diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index c4dd905..95e4a05 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -7,6 +7,33 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair { + private E first; + private E second; + + 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)>0); + return first; + + } + + public E max(){ + if(first.compareTo(second)>0); + return 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()); + } +} 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