Skip to content

Finished #72

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 6 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
Expand Up @@ -8,5 +8,15 @@
* 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 <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);
}
}
8 changes: 8 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
*/
public class MapFunc {

public static <T,R, E extends T> ArrayList<R> map (ArrayList<E> arrList, Function<T,R> function){
ArrayList<R> arrayList = new ArrayList<>();
for (E value: arrList){
R rValue = function.apply(value);
arrayList.add(rValue);
}
return arrayList;
}
}
23 changes: 22 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,27 @@
* 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 pair = new Pair (first, last);
return pair;
}

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

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

public static <E extends Comparable<E>> Pair<E> minMax(ArrayList<E> al) {
E minVal = Collections.min(al);
E maxVal = Collections.max(al);
Pair minMaxValues = new Pair (minVal, maxVal);
return minMaxValues;
}
}
46 changes: 45 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,50 @@
* 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 first;
}
else if (compareTo(second)>0){
return second;
}
return null;
}

public E max() {
if (compareTo(second) > 0){
return first;}
else if (compareTo(second)<0){
return second;
}
return null;
}



@Override
public int compareTo(E values) {
return first.compareTo(values);
}

}
23 changes: 23 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,38 @@
package StackArray;

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

/**
* 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 void push(E element) {
E[] newArr = Arrays.copyOf(this.elements, this.elements.length + 1);
newArr[actualSize++] = element;
this.elements = newArr;

}

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

public E pop() {
E element = this.elements[actualSize - 1];
this.elements[actualSize - 1] = null;
actualSize--;
return element;
}
}
21 changes: 20 additions & 1 deletion 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;

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


public void push(E element) {
Object[] newArr = Arrays.copyOf(this.elements, this.elements.length + 1);
newArr[actualSize++] = element;
this.elements = newArr;

}

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

public Object pop() {
Object element = this.elements[actualSize - 1];
this.elements[actualSize - 1] = null;
actualSize--;
return element;
}
}
17 changes: 17 additions & 0 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.
Expand All @@ -11,6 +12,22 @@ public class Stack<E> {


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

public boolean isEmpty() {
return this.elements.isEmpty();
}
public E push(E element) {
this.elements.add(element);
return element;
}
public E pop() {
Collections.reverse(elements);
E element = (E) this.elements.remove(0);
return element;
}


}

8 changes: 8 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,12 @@ public V getValue() {
return value;
}

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

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

}
50 changes: 40 additions & 10 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,46 @@

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.
* 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;
/**
* 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<Entry> entries;

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

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

public void put(K key, V value) {
for (int i =0; i < entries.size(); i++){
if (entries.get(i).getKey().equals(key)){
System.out.println("Error, already exists");
}
entries.get(i).setKey(key);
entries.get(i).setValue(value);
}
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);
}
}
}
}
65 changes: 64 additions & 1 deletion src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,74 @@
package TableNested;

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;

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

public 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 setKey(K key){
this.key = key;
}

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

}





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

public void put(K key, V value) {
for (int i =0; i < entries.size(); i++){
if (entries.get(i).getKey().equals(key)){
System.out.println("Error, already exists");
}
entries.get(i).setKey(key);
entries.get(i).setValue(value);
}
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);
}
}
}
}
Loading