Skip to content

Lab Complete #71

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 1 commit 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
12 changes: 12 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ArrayListCombiner;

import Employee.*;

import java.util.ArrayList;

/**
Expand All @@ -9,4 +11,14 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {

public static <E> void extendCombiner (ArrayList<E> first, ArrayList<? extends E> second) {
first.addAll(second);

}

public static <E> void superCombiner(ArrayList<? super E> first, ArrayList<E> second) {
first.addAll(second);

}
}
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<R> map(ArrayList<T> obj, Function<T,R> func) {
ArrayList<R> temp = new ArrayList<>();
for (int i = 0; i < obj.size(); i++) {
temp.add(func.apply(obj.get(i)));

}
return temp;
}


}
27 changes: 25 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,30 @@
* 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<E extends Comparable> {

public Arrays() {
}

public static <E extends Comparable> Pair<E > firstLast(ArrayList<E> a) {
return new Pair<E>(a.get(0), a.get(a.size()-1)) ;

}

public static <E extends Comparable> E min(ArrayList<E> al) {
E min = al.get(0);
for (E val: al) {
if (val.compareTo(min) <0) min = val;
}
return min;

}

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

public static <E extends Comparable> Pair<E> minMax(ArrayList<E> al) {
return new Pair(min(al), max(al));
}
}
32 changes: 31 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,36 @@
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E extends Comparable> {

private E e1;
private E e2;

public Pair(E e1, E e2) {
this.e1 = e1;
this.e2 = e2;
}

public E getFirst() {
return this.e1;
}

public E getSecond() {
return this.e2;
}

public E max() {
if (this.e1.compareTo(this.e2) > 0 ) {
return this.e1;
}
return this.e2;
}

public E min() {
if (this.e1.compareTo(this.e2) < 0 ) {
return this.e1;
}
return this.e2;
}

}
26 changes: 26 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package StackArray;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Stack;

/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
Expand All @@ -11,5 +13,29 @@ public class GenericStack<E> {
private E[] elements;

public GenericStack() {
this.elements = (E[]) new Object[0];

}

public E pop() {
E popValue = elements[elements.length -1];
elements = Arrays.copyOf(elements, elements.length-1);
return popValue;
}

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


}

public boolean isEmpty() {
for (E obj: elements)
{ if (!obj.equals(null))
return false;
}
return true;
}

}
17 changes: 17 additions & 0 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@ public class Stack<E> {


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

public E pop() {

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

public void push( E obj) {

elements.add(elements.size(), obj);

}

public boolean isEmpty() {

return elements.size() == 0;
}

}
6 changes: 6 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ public V getValue() {
return value;
}

public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = value;
}
}
40 changes: 38 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Table;


import java.util.ArrayList;

/**
Expand All @@ -10,8 +11,43 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;

private ArrayList<Entry> entries;



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

}

public V get(K key) {
for (Entry entry: entries) {
if (key.equals(entry.getKey()))
return (V) entry.getValue();

}

return null;
}


public void put(K key, V value) {
for (Entry entry: entries) {
if(key.equals(entry.getKey())) {
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;
}

}
}
}
65 changes: 65 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package TableNested;



import java.util.ArrayList;

/**
Expand All @@ -8,4 +10,67 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry> entries;

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

}

public V get(K key) {
for (Entry entry: entries) {
if (key.equals(entry.getKey()))
return (V) entry.getValue();

}

return null;
}


public void put(K key, V value) {
for (Entry entry: entries) {
if(key.equals(entry.getKey())) {
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;
}

}
}


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;
}

public void setKey(K key) {
this.key = key;
}
public void setValue(V value) {
this.value = 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);
}

}
Loading