Skip to content

Finished generous generics #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
All passing up Pairs
  • Loading branch information
Joe Hendricks authored and Joe Hendricks committed Mar 10, 2018
commit 72ec6a7b3dfb3cc499dfb059a5f45f62807b2f7f
8 changes: 8 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {

public static <E> void extendCombiner(ArrayList<E> firstArray, ArrayList<? extends E> secondArray) {
firstArray.addAll(secondArray);
}

public static <E> void superCombiner(ArrayList<? super E> firstArray, ArrayList<E> secondArray){
firstArray.addAll(secondArray);
}
}
10 changes: 10 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
*/
public class MapFunc {

public static <T,R> ArrayList map(ArrayList<T> list, Function<T, R> function){

ArrayList<R> newList = new ArrayList<>();

for(int i = 0; i < list.size(); i++){
R combine = function.apply(list.get(i));
newList.add(combine);
}
return newList;
}
}
8 changes: 4 additions & 4 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* A max method that returns the largest item in the arraylist
* And a minmax method that returns a pair containing the largest and smallest items from the array list
*/
public class Arrays {
public static <___> Pair<E> firstLast(ArrayList<___> a) {
}
}
//public class Arrays {
// public static <___> Pair<E> firstLast(ArrayList<___> a) {
// }
//}
24 changes: 23 additions & 1 deletion src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,30 @@
* @param <E>
*/
public class GenericStack<E> {
private E[] elements;
private E[] elements = (E[]) new Object[0];
private int size = 0;

public GenericStack() {

}

public boolean isEmpty() {

return size == 0;
}

public void push(E element) {
this.elements = Arrays.copyOf(this.elements, this.elements.length + 1);
this.elements[elements.length - 1] = element;
size++;

}

public E pop() {
E object = this.elements[size - 1];
this.elements[size - 1] = null;
size--;
return object;
}

}
21 changes: 20 additions & 1 deletion src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,28 @@
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements;
private Object[] elements = new Object[0];
private int size = 0;

public ObjectStack() {

}


public boolean isEmpty() {
return size == 0;
}

public void push(Object element) {
this.elements = Arrays.copyOf(this.elements, this.elements.length + 1);
this.elements[elements.length - 1] = element;
size++;
}

public Object pop() {
Object object = this.elements[size - 1];
this.elements[size - 1] = null;
size--;
return object;
}
}
16 changes: 15 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
package StackArrayList;

import java.util.ArrayList;
import java.util.Collections;

/**
* Implement Stack<E> 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<E> {
private ArrayList elements;
private ArrayList<E> elements;


public Stack(){
this.elements = new ArrayList<E>();
}

public E pop(){
return this.elements.remove(this.elements.size() - 1);
}

public void push(E item){
this.elements.add(item);
}

public boolean isEmpty(){
return (elements.size()==(0));
}

}
33 changes: 32 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,39 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry<K, V>> entries = new ArrayList<>();

public Table() {
}


public V get(K key) {
for (int i = 0; i < entries.size(); i++) {
if(entries.get(i).getKey().equals(key)){
return entries.get(i).getValue();
}
}
return null;
}

public void put(K key, V value) {
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).getKey().equals(key)){
entries.add(new Entry<K, V>(key, value));
entries.remove(i);
break;

}
}
entries.add(new Entry<K, V> (key, value));
}

public void remove(K key) {
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).getKey().equals(key)){
entries.remove(i);
break;
}
}
}
}
51 changes: 51 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,55 @@
*/
public class TableNested<K, V> {

private ArrayList<EntryNested> entries;

public TableNested(){
this.entries = new ArrayList<>();
}

public Object get(K key) {
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).getKey().equals(key)){
return entries.get(i).getValue();
}
}
return null;
}

public void put(K key, V value) {
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).getKey().equals(key)){
entries.set(i, new EntryNested<>(key, value));
break;
}
}
entries.add(new EntryNested<>(key, value));
}

public void remove(K key) {
for(int i = 0; i < entries.size(); i++){
if(entries.get(i).equals(key)){
entries.remove(i);
}
}
}

private class EntryNested<K, V> {
private K key;
private V value;

public EntryNested(K key, V value) {
this.key = key;
this.value = value;
}

public K getKey() {
return key;
}

public V getValue() {
return value;
}

}
}
70 changes: 35 additions & 35 deletions src/test/java/ArrayListCombiner/ArrayListCombinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Employee> first = new ArrayList<>();
// first.add(foo);
// // An an array list with managers
// ArrayList<Manager> second = new ArrayList<>();
// second.add(bar);
// // When I combine them
// ArrayListCombiner.extendCombiner(first, second);
// // Then I should get an arrayList with both
// ArrayList<Employee> 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<Employee> first = new ArrayList<>();
// first.add(foo);
// // An an array list with managers
// ArrayList<Manager> second = new ArrayList<>();
// second.add(bar);
// // When I combine them
// ArrayListCombiner.superCombiner(first, second);
// // Then I should get an arrayList with both
// ArrayList<Employee> 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<Employee> first = new ArrayList<>();
first.add(foo);
// An an array list with managers
ArrayList<Manager> second = new ArrayList<>();
second.add(bar);
// When I combine them
ArrayListCombiner.extendCombiner(first, second);
// Then I should get an arrayList with both
ArrayList<Employee> 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<Employee> first = new ArrayList<>();
first.add(foo);
// An an array list with managers
ArrayList<Manager> second = new ArrayList<>();
second.add(bar);
// When I combine them
ArrayListCombiner.superCombiner(first, second);
// Then I should get an arrayList with both
ArrayList<Employee> expected = new ArrayList<>();
expected.add(foo);
expected.add(bar);
Assert.assertEquals(expected, first);
}

}
72 changes: 36 additions & 36 deletions src/test/java/MapFunc/MapFuncTest.java
Original file line number Diff line number Diff line change
@@ -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<Integer> intList = new ArrayList<>();
// intList.add(1);
// intList.add(2);
// // When it's mapped with a function to double the value
// ArrayList<Integer> 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<Integer> intList = new ArrayList<>();
// intList.add(1);
// intList.add(2);
// // When it's mapped with to string
// ArrayList<String> 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));
// }
//
//}
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<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
// When it's mapped with a function to double the value
ArrayList<Integer> 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<Integer> intList = new ArrayList<>();
intList.add(1);
intList.add(2);
// When it's mapped with to string
ArrayList<String> 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));
}

}
Loading