Skip to content

Finished whole lab #66

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 10 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
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> first, ArrayList<? extends E> second) {
first.addAll(second);
}

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

public static <T, R> ArrayList<R> map(ArrayList<T> input, Function<T, R> function) {
ArrayList<R> finalArray = new ArrayList<>();
for (T itemToChange : input) {
R result = function.apply(itemToChange);
finalArray.add(result);
}
return finalArray;
}

}
21 changes: 20 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,25 @@
* 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> Pair<E> firstLast(ArrayList<E> a) {
Pair<E> pairToReturn = new Pair<E>(a.get(0), a.get(a.size() - 1));
return pairToReturn;
}

public static <E extends Comparable> E min(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return arrayList.get(0);
}

public static <E extends Comparable> E max(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return arrayList.get(arrayList.size() - 1);
}

public static <E extends Comparable> Pair<E> minMax(ArrayList<E> arrayList) {
E minimum = min(arrayList);
E maximum = max(arrayList);
return new Pair<E>(minimum, maximum);
}
}
34 changes: 33 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,38 @@
* 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 e1;
}

public E getSecond() {
return e2;
}

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

public E max() {
if (e1.compareTo(e2) < 0) {
return e2;
} else {
return e1;
}
}

}
23 changes: 23 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,28 @@ public class GenericStack<E> {
private E[] elements;

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

public E push(E item) {
E[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
newArray[newArray.length - 1] = item;
this.elements = newArray;
return item;
}

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

public boolean isEmpty() {
if (this.elements.length == 0) {
return true;
} else {
return false;
}
}
}
21 changes: 21 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@ public class ObjectStack<E> {
private Object[] elements;

public ObjectStack() {
this.elements = new Object[0];
}
public Object push(Object item) {
Object[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
newArray[newArray.length - 1] = item;
this.elements = newArray;
return item;
}

public Object pop() {
Object item = this.elements[this.elements.length - 1];
Object[] newArray = Arrays.copyOf(this.elements, this.elements.length - 1);
this.elements = newArray;
return item;
}

public boolean isEmpty() {
if (this.elements.length == 0) {
return true;
} else {
return false;
}
}
}
18 changes: 17 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,24 @@
public class Stack<E> {
private ArrayList elements;


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

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

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

public boolean isEmpty() {
if (this.elements.size() == 0) {
return true;
} else {
return false;
}
}
}
37 changes: 36 additions & 1 deletion 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.security.Key;

/**
* 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,42 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

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

public void put(K key, V value) {
for (Entry entry : this.entries) {
if (entry.getKey().equals(key)) {
entries.set(entries.indexOf(entry), new Entry<>(key, value));
return;
}
}
entries.add(new Entry<>(key, value));
}

public Object get(K key) {
for (Entry entry : this.entries) {
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}

public void remove(K key) {
// for (Entry entry : this.entries) {
// if (entry.getKey().equals(key)) {
// this.entries.remove(entry);
// }
// }
for (int i = 0; i < this.entries.size(); i++) {
if (this.entries.get(i).getKey().equals(key)) {
this.entries.remove(i);
}
}
}

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

import java.util.ArrayList;
import java.util.Map.Entry;


/**
* 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> {
private ArrayList<Entry> entries;

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

public void put(K key, V value) {
for (Entry entry : this.entries) {
if (entry.getKey().equals(key)) {
entries.set(entries.indexOf(entry), new Entry<>(key, value));
return;
}
}
entries.add(new Entry<>(key, value));
}

public Object get(K key) {
for (Entry entry : this.entries) {
if (entry.getKey().equals(key)) {
return entry.getValue();
}
}
return null;
}

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

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

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