Skip to content

submit lab #78

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 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,11 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {
public static <E> void superCombiner(ArrayList<? super E> parent, ArrayList<E> child) {
parent.addAll(child);
}

public static <E> void extendCombiner(ArrayList<E> parent, ArrayList<? extends E> child) {
parent.addAll(child);
}
}
8 changes: 8 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
*/
public class MapFunc {

public static <T, R> ArrayList<R> map(ArrayList<T> a, Function<T, R> f) {

ArrayList<R> result = new ArrayList<>(a.size());
a.forEach(
t -> result.add(f.apply(t))
);
return result;
}
}
18 changes: 17 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
* And a minmax method that returns a pair containing the largest and smallest items from the array list
*/
public class Arrays {
public static <___> Pair<E> firstLast(ArrayList<___> a) {
public static <E extends Comparable<E>> Pair<E> firstLast(ArrayList<E> a) {
E first = a.get(0);
E second = a.get(a.size() - 1);

return new Pair<>(first, second);
}

public <E extends Comparable<E>> E max(ArrayList<E> a) {
return Collections.max(a);
}

public <E extends Comparable<E>> E min(ArrayList<E> a) {
return Collections.min(a);
}

public <E extends Comparable<E>> Pair<E> minmax(ArrayList<E> a) {
return new Pair<>(min(a), max(a));
}
}
24 changes: 23 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E extends Comparable<E>> {
private E first;
private E second;

public Pair(E first, E second) {
this.first = first;
this.second = second;
}

public E getFirst() {
return first;
}

public E getSecond() {
return second;
}

public E min() {
return (first.compareTo(second) < 1) ? first : second;
}

public E max() {
return (first.compareTo(second) > 0) ? first : second;
}
}
5 changes: 3 additions & 2 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package StackArray;

import java.util.Arrays;
import StackArrayList.Stack;

/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class GenericStack<E> {
public class GenericStack<E> extends Stack<E> {
private E[] elements;

public GenericStack() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package StackArray;

import java.util.Arrays;
import StackArrayList.Stack;

/**
* Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class ObjectStack<E> {
public class ObjectStack<E> extends Stack<E> {
private Object[] elements;

public ObjectStack() {

}
}
17 changes: 15 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
package StackArrayList;

import java.util.ArrayList;
import java.util.List;

/**
* 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 List<E> elements;


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

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

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

public boolean isEmpty() {
return elements.size() == 0;
}
}
18 changes: 16 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Table;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
* This class needs to manage an ArrayList of Entry objects. It needs a get method that takes a key and returns
Expand All @@ -10,8 +11,21 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private Map<K, V> entries;

public Table() {
entries = new HashMap<>();
}

public V get(K key) {
return entries.get(key);
}

public void put(K key, V value) {
entries.put(key, value);
}

public void remove(K key) {
entries.remove(key);
}
}
21 changes: 19 additions & 2 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package TableNested;

import java.util.ArrayList;
import Table.Table;

/**
* All you need to do for this microlab is take the Table and Entry from the last one and make Entry a nested class.
* Think about how nested classes should work with generics.
*/
public class TableNested<K, V> {
public class TableNested<K, V> extends Table<K, V> {
private class Entry<K, V> {
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;
}

}
}
74 changes: 37 additions & 37 deletions src/test/java/ArrayListCombiner/ArrayListCombinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,47 @@

import Employee.Employee;
import Employee.Manager;
import org.junit.Test;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;

public class ArrayListCombinerTest {
// Employee foo = new Employee("FOO", 100);
// Manager bar = new Manager("BAR", 100);
// @Test
// public void testExtendCombiner() throws Exception {
// // Given an array list with employees
// ArrayList<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);
}

}
71 changes: 35 additions & 36 deletions src/test/java/MapFunc/MapFuncTest.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
//package MapFunc;
//
//import MapFunc.MapFunc;
//import org.junit.Test;
//
//import java.util.ArrayList;
//import org.junit.Assert;
//
//public class MapFuncTest {
// @Test
// public void testSingleTypeMap() throws Exception {
// // Given an integer array list
// ArrayList<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 org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;

public class MapFuncTest {
@Test
public void testSingleTypeMap() throws Exception {
// Given an integer array list
ArrayList<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