Skip to content

Done #68

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 4 commits into
base: master
Choose a base branch
from
Open

Done #68

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
11 changes: 9 additions & 2 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,12 @@
* The first method should be called extendCombiner and should use ? extends E
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {
}
public class ArrayListCombiner { //the second array extends from the first array so it will inherit the behavior from the first array
public static <E> void superCombiner(ArrayList<? super E> firstArray, ArrayList<E> secondArray){
firstArray.addAll(secondArray); //addall will append all elements of the second array to end of the first array
}
public static <E> void extendCombiner(ArrayList<E> firstArray, ArrayList<? extends E> secondArray){ //making the arrays able to combine
firstArray.addAll(secondArray);//add all will combine the second array elements to the end of the first
} //the firstArray is the parent class to the second array according to the test
//Manager extends employee
}
10 changes: 8 additions & 2 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
* Create a function called `map` that takes an ArrayList and a `Function<T,R>` object,
* and returns an ArrayList with all of the elements of the first after the function is applied to them.
*/
public class MapFunc {

public class MapFunc {//T being my input type and R being my output type
public static <T, R> ArrayList<R> map (ArrayList<T> array, Function<T, R> functor){
ArrayList<R> arrayList = new ArrayList<>();//empty arraylist
for (T thelid : array) { //iterate through my array for each of the lids of type T
arrayList.add(functor.apply(thelid));//in my new list add the function being applied
} //to each of the lids
return arrayList; //arrayList being my output type
}
}
24 changes: 23 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
* 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) {

@SuppressWarnings("unchecked")
public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> arrayList) {
return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
}

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

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

@SuppressWarnings("unchecked")
public static <E extends Comparable> Pair<E> minMax(ArrayList<E> arrayList) {
Collections.sort(arrayList);
return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
}
}

46 changes: 44 additions & 2 deletions src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,48 @@
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair <E extends Comparable> {
//without extending you cannot compare generic types
//by extending comparing individual values of generic types is now viable
private E firstValue;
private E secondValue;

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

private int compareTo(E otherValue) {
return firstValue.compareTo(otherValue);
}

public E getFirst(){
return firstValue;
}

public E getSecond(){
return secondValue;
}

public E min(){
int compareResult = compareTo(firstValue);
boolean secondValueIsGreater = compareResult == -1;

if (secondValueIsGreater) {
return secondValue;
} else {
return firstValue;
}
}

public E max(){
int compareResult = compareTo(secondValue);
boolean firstValIsGreater = compareResult == 1;

if (firstValIsGreater) {
return firstValue;
} else {
return secondValue;
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,27 @@
*/
public class GenericStack<E> {
private E[] elements;
private int size; //actual number of things as opposed to length

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

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

public E push(E foobar) { //push onto to the top of the stack which is the end
this.elements = Arrays.copyOf(this.elements,this.elements.length + 1);
this.elements[this.elements.length - 1] = foobar; //capacity of array not size
size++;
return foobar;
}

public E pop() { //remove object at the top of the stack
E thingy = this.elements[elements.length - 1]; //capacity of array not size
this.elements = Arrays.copyOf(this.elements,this.elements.length - 1);
size--; //
return thingy;
}
}
19 changes: 19 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,27 @@
*/
public class ObjectStack<E> {
private Object[] elements;
private int size;

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

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

public Object push(Object foobar) { //push onto to the top of the stack which is the end
this.elements = Arrays.copyOf(this.elements,this.elements.length + 1);
this.elements[this.elements.length - 1] = foobar; //capacity of array not size
size++;
return foobar;
}

public Object pop() { //remove object at the top of the stack
Object thingy = this.elements[elements.length - 1]; //capacity of array not size
this.elements = Arrays.copyOf(this.elements,this.elements.length - 1);
size--; //
return thingy;
}
}
21 changes: 18 additions & 3 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;
private ArrayList<E> elements;


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

public boolean isEmpty() {//if the array is empty return true otherwise return false
if (elements.size() == 0) return true;
else return false;
}

//@SuppressWarnings("unchecked")
public void push(E foobar) {
elements.add(foobar);
}

//@SuppressWarnings("unchecked")
public E pop() {
return elements.remove(elements.size()-1);
}
}
}
1 change: 0 additions & 1 deletion src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ public K getKey() {
public V getValue() {
return value;
}

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

@SuppressWarnings("unchecked")
public Table() {
this.entries = new ArrayList();
}

@SuppressWarnings("unchecked")
public V get(K foo) {
for (Entry submission : entries) { //for each submission in my entries array
if (submission.getKey().equals(foo)) { //if key of my submission object is equal to the key passed in
return (V)submission.getValue(); //return that value mapped to that key
}
}
return null;
}

public void put(K foo, V i) {
remove(foo); //remove foo from the key
Entry<K, V> entry = new Entry<>(foo, i); //create empty entry object for table
entries.add(entry); //add foo back into the empty entry within entries
}

public void remove(K foo) {
for (int i = 0; i < entries.size(); i++){ //iterate through arrayList
if (entries.get(i).getKey().equals(foo)) //if the key of the current index of entries is equal to foo
entries.remove(i);//if the above is true then remove it
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,40 @@
package TableNested;

import Table.Entry; //imported the entire table entry class
import java.util.ArrayList;

/**
* 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;

@SuppressWarnings("unchecked")
public TableNested() {
this.entries = new ArrayList();
}

@SuppressWarnings("unchecked")
public V get(K foo) {
for (Entry submission : entries) { //for each submission in my entries array
if (submission.getKey().equals(foo)) { //if key of my submission object is equal to the key passed in
return (V) submission.getValue(); //return that value mapped to that key
}
}
return null;
}

public void put(K foo, V i) {
remove(foo); //remove foo from the key
Entry<K, V> entry = new Entry<>(foo, i); //create empty entry object for table
entries.add(entry); //add foo back into the empty entry within entries
}

public void remove(K foo) {
for (int i = 0; i < entries.size(); i++) { //iterate through arrayList
if (entries.get(i).getKey().equals(foo)) //if the key of the current index of entries is equal to foo
entries.remove(i);//if the above is true then remove it
}
}
}
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