Skip to content

Done #73

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

Done #73

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
18 changes: 18 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,22 @@
* 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);
}
}


//ArrayListCombiner -- Write two methods, superCombiner and extendCombiner, which each take two arraylists and
// append all of the items from the second to the first. superCombiner should use ? super E and extendCombiner
// should use ? extends E.

//All the tests pass with this as the superCombiner but it's not done as asked
//public static <E> void superCombiner (ArrayList<E> first, ArrayList<? extends E> second){
// first.addAll(second);

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

public static <T, R> ArrayList<R> map(ArrayList<T> arr, Function<T, R> mapfunc) {
ArrayList outputArray = new ArrayList<>();
for (T val : arr) {
R applyFuncVal = mapfunc.apply(val);
outputArray.add(applyFuncVal);
}
return outputArray;
}

}

//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.

34 changes: 33 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,39 @@
* 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
*/
@SuppressWarnings("unchecked")
public class Arrays {
public static <___> Pair<E> firstLast(ArrayList<___> a) {


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> Comparable max(ArrayList<E> a ){
return Collections.max(a);

}

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

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



// 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.

43 changes: 42 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,47 @@
* 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 Pair(E first, E second) {
this.first = first;
this.second = second;
}


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

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

@SuppressWarnings("unchecked")
public E min() {
if (first.compareTo(second) > 0) {
return second;
}
return first;
}
@SuppressWarnings("unchecked")
public E max() {
if (first.compareTo(second) < 0) {
return second;
}
return first;
}


}


//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.



34 changes: 33 additions & 1 deletion src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package StackArray;

//import com.sun.java.util.jar.pack.ConstantPool;

import java.util.Arrays;

/**
Expand All @@ -8,8 +10,38 @@
* @param <E>
*/
public class GenericStack<E> {
private E[] elements;

private Object[] elements;
private int top;
private final static int EMPTY = -1;
private final static int DEFAULT_Capacity = 10;

public GenericStack() {

this(DEFAULT_Capacity);
}

public GenericStack(int initialCapacity) {
elements = new Object [initialCapacity];
top = EMPTY;
}

public boolean isEmpty(){

return (top == EMPTY);
}

@SuppressWarnings("uncheck")
public E pop(){
if (top == EMPTY){
throw new IndexOutOfBoundsException();
}
return (E)elements[top--];
}

public void push(E e) {
elements[++top] = e;
}
}


33 changes: 33 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,45 @@
/**
* 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;
private int top;
private final static int EMPTY = -1;
private final static int DEFAULT_Capacity = 10;

public ObjectStack() {
this(DEFAULT_Capacity);
}

public ObjectStack(int initialCapacity) {
elements = new Object[initialCapacity];
top = EMPTY;
}

public boolean isEmpty() {

return (top == EMPTY);
}

@SuppressWarnings("unchecked")
public Object pop() {
if (top == EMPTY) {
throw new IndexOutOfBoundsException();
}
return elements[top--];
}

public void push(Object o) {

elements[++top] = o;
}

}




36 changes: 33 additions & 3 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
package StackArrayList;

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

/**
* 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 class Stack<E> {

private ArrayList<E> elements;

public Stack(){
public Stack() {

this.elements = new ArrayList();
}

/**
* @return true if this list contains no elements
*/
public boolean isEmpty() {
return this.elements.size() == 0;
}

/**
* Pushes an item onto the top of this stack. This has exactly the same effect as:
* addElement(item)
* @param item the item to be pushed onto this stack.
* //should return the item argument.
*/
public void push(E item) {
elements.add(item);
}

/*
*Removes the object at the top of this stack
*returns that object as the value of this function.
*/
public E pop() throws IndexOutOfBoundsException{
return this.elements.remove(this.elements.size() - 1);
}
}


4 changes: 4 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ public Entry(K key, V value) {
}

public K getKey() {

return key;
}

public V getValue() {

return value;
}

}


57 changes: 53 additions & 4 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,64 @@
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.
* 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.
* 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 entries;
private ArrayList<Entry<K, V>> entries;

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

/*
takes a key and returns either the entry from the arraylist with that key or null if none is found
*/
public V get(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return (V) entries.get(i).getValue();
}
}
return null;
}

/*
put takes a key value and sets the value in the Arraylist with that key or null if none is found
note keys are unique
*/
public void put(K key, V value) {
//check to see if key exists, if so replace value, if not add both key and value
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
entries.remove(i);
entries.add(new Entry<>(key, value));
break;
}
}
entries.add(new Entry<>(key, value));
}


/*
remove which takes a key and removes it from the arraylist if its in there. Its a void method no return type
*/
public void remove(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
entries.remove(i);
break;
}

}

}
}


Loading