Skip to content

Finished Lab #57

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

import Employee.Employee;
import Employee.Manager;

import java.util.ArrayList;

/**
Expand All @@ -9,4 +12,11 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {
public static void extendCombiner(ArrayList<Employee> first, ArrayList<? extends Manager> second) {
first.addAll(second);
}

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

public static <T, R> ArrayList<R> map(ArrayList<T> mapArray, Function<T, R> mapFunction) {
ArrayList funcArray = new ArrayList();
for (T mapArrayValue : mapArray) {
R changedValue = mapFunction.apply(mapArrayValue);
funcArray.add(changedValue);

}
return funcArray;
}

/* For this part you need to create a generic method and give it the type parameter of <T, R>
because the function uses T for the type being passed in. Then R for the return type.
The method then returns ArrayList of type R because this is the array that had the function applied to it.
The argument being passed in is of type T being the original array being passed into the function.
*/
}

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) {
public static <E extends Comparable<E>> Pair<E> firstLast(ArrayList<E> a) {
E first = a.get(0);
E last = a.get(a.size() - 1);
Pair<E> firstLast = new Pair<>(first, last);
return firstLast;
}

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

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

public static <E extends Comparable<E>> Pair<E> minMax(ArrayList<E> a) {
E maximumValue = Collections.max(a);
E minimumValue = Collections.min(a);
Pair<E> minMax = new Pair<>(maximumValue, minimumValue);
return minMax;
}

}
44 changes: 43 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
package Pair;


/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* getFirst
* getSecond
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E extends Comparable<E>> implements Comparable<E> {

private E first;
private E second;

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

}

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

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

public E min() {
if (compareTo(second) < 0) {
return this.first;

} else if (compareTo(second) > 0) {
return this.second;
}
return null;
}

public E max() {
if (compareTo(second) < 0) {
return this.second;

} else if (compareTo(second) > 0) {
return this.first;
}
return null;
}

@Override
public int compareTo(E values) {
return first.compareTo(values);
}
}
25 changes: 25 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,36 @@
/**
* 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> {

private E[] elements;
Integer actualSize = 0;

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

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

public void push(E element) {

if (this.actualSize >= this.elements.length - 1) {
this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length);
}

this.elements[actualSize++] = element;

}

public E pop() throws IndexOutOfBoundsException {
E element = this.elements[actualSize - 1];
this.elements[actualSize - 1] = null;
actualSize--;
return element;
}
}
25 changes: 25 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@
/**
* 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> {

private Object[] elements;
Integer actualSize = 0;

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

}

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

public void push(E element) {

if (this.actualSize >= this.elements.length - 1) {
this.elements = Arrays.copyOf(this.elements, this.elements.length + this.elements.length);
}

this.elements[actualSize++] = element;

}

public Object pop() throws IndexOutOfBoundsException {
Object element = this.elements[actualSize - 1];
this.elements[actualSize - 1] = null;
actualSize--;
return element;
}
}
18 changes: 17 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
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;

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

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

public boolean isEmpty() {
return this.elements.isEmpty();
}

public E pop() throws IndexOutOfBoundsException {
Collections.reverse(this.elements);
E element = (E) this.elements.remove(0);
return element;
}
}
11 changes: 11 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Table;

public class Entry<K, V> {

private K key;
private V value;

Expand All @@ -17,4 +18,14 @@ public V getValue() {
return value;
}

public void setValue(V value) {
this.value = value;
}


public void setKey(K key) {
this.key = key;
}


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

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

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

}
}
return null;
}

public void put(K key, V value) {
for (Entry entry : entries) {
entry.setKey(key);
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
Expand Up @@ -8,4 +8,69 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry> entries;

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


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

public void setValue(V value) {
this.value = value;
}


public void setKey(K key) {
this.key = key;
}
}


public void remove(K key) {
for (Entry entry : entries) {
if (key.equals(entry.getKey())) {
entries.remove(entry);
return;
}
}
}

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

}
}
return null;
}

public void put(K key, V value) {
for (Entry entry : entries) {
entry.setKey(key);
entry.setValue(value);
return;
}

entries.add(new Entry(key, value));
}

}
Loading