Skip to content
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
29 changes: 27 additions & 2 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
package ArrayListCombiner;

import Employee.Employee;

import java.util.ArrayList;

/**
* Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items,
* to the first. Use a wildcard for one of the type arguments in each method.
* Create two generic methods that take two arraylists.
*
* The methods should both append the second ArrayList's items, to the first.
* Use a wildcard for one of the type arguments in each method.
*
* 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 static void extendCombiner(ArrayList<E> first, ArrayList<E> second)

// ? extends guaranteeing it's passing in correct type
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);
}



}



15 changes: 15 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,19 @@
*/
public class MapFunc {

public static <T,R> ArrayList map(ArrayList<T> arrayList, Function<T,R> function) {

ArrayList<T> newArrayList = new ArrayList<>();

for (int i = 0; i < arrayList.size(); i++) {
newArrayList.add((T) function.apply(arrayList.get(i))); //function.apply using the lambda
}
return newArrayList;
}
}


//MapFunc -- Make a map method that takes an ArrayList and a Function<T,R> object and
// returns an arraylist containing all of the elements of the first with the function applied to them.
// T = type
// R = return type
31 changes: 30 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,35 @@
* 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) {
return new Pair(a.get(0), a.get(a.size() - 1));
}


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


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

public static <E extends Comparable> Pair minMax(ArrayList<E> al) {
return new Pair<>(min(al), max(al));
}

}



//Pair -- This is a multi-step one:
// Create a Pair that stores a pair of elements of type E.
// Create two methods, min and max, that return the largest and smallest of the Pair.
// Create a utility class called Arrays and, in that, create a method
// public static <___> Pair<E> firstLast(ArrayList<___> a)
// That returns a Pair containing the first and last element of the array.
// NOTE: The <___> is there because you need to fill in the blank.
// In Arrays make two methods, min and max that returns the smallest and largest elements in the ArrayList.
// In Arrays make a minMax function that returns a Pair with the minimum and maximum values of the ArrayList.
35 changes: 34 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,45 @@
package Pair;

import java.util.Collections;

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

private E first;
private E second;

public E getFirst() {
return first;
}

public E getSecond() {
return second;
}

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


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

public E max() {
if( first.compareTo(second) > 0){
return first;
}
return second;
}

}
26 changes: 26 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,37 @@
/**
* 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;

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


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


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

}


public boolean isEmpty() {
if (elements.length == 0) {
return true;
}
return false;
}
}
24 changes: 24 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,36 @@
/**
* 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;

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

public void push(Object value) {
Object[] newArray = Arrays.copyOf(elements, elements.length + 1);
newArray[newArray.length - 1] = value;
this.elements = newArray;
}


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

}


public boolean isEmpty() {
if (elements.length == 0) {
return true;
}
return false;
}
}
24 changes: 22 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,30 @@
* 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() {
return this.elements.size() == 0;
}


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


public E pop() {
E value = elements.get(elements.size() - 1);
elements.remove(elements.size() - 1);
return value;

}
}


14 changes: 14 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,17 @@ public V getValue() {
}

}


//Table -- Implemented for you is Entry<K, V>.
// Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.

// You must implement get which takes a key and returns either the entry
// from the ArrayList with that key, or null if none is found.

// put which takes a key and value and sets the value in the ArrayList to Entry(key, value);

// Remember, a key point to exactly one value
// remove which takes a key and removes it from the ArrayList if it's in there.

// It's a void method; no return type.
58 changes: 54 additions & 4 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,65 @@
import java.util.ArrayList;

/**
* This class needs to manage an ArrayList of Entry objects. It needs a get method that takes a key and returns
* its corresponding value, or null of the key is not in the arraylist. It needs a put method that takes a key and value
* and makes an entry with key, value. NOTE: There cannot be two entries with the same key.
* This class needs to manage an ArrayList of Entry objects.
* <p>
* It needs a get method that takes a key and returns
* its corresponding value, or null of the key is not in the arraylist.
* <p>
* It needs a put method that takes a key and value and makes an entry with key, value.
* <p>
* NOTE: There cannot be two entries with the same key.
* <p>
* It also needs a remove method which takes a key and, if that key exists in the arraylist, removes that item.
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

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

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


public void put(K key, V value) {
int count = 0;
for (Entry entry : entries) {
if (entry.getKey().equals(key)) {
entries.set(count, new Entry(key, value));
count++;
}
}
entries.add(new Entry(key, value));
}

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


//Table -- Implemented for you is Entry<K, V>.
// Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.

// You must implement get which takes a key and returns either the entry
// from the ArrayList with that key, or null if none is found.

// put which takes a key and value and sets the value in the ArrayList to Entry(key, value);

// Remember, a key point to exactly one value
// remove which takes a key and removes it from the ArrayList if it's in there.

// It's a void method; no return type.
Loading