Skip to content

Lab complete #69

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 3 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
15 changes: 14 additions & 1 deletion src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ArrayListCombiner;

import Employee.*;

import java.util.ArrayList;

/**
Expand All @@ -9,4 +11,15 @@
* 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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,25 @@

import java.util.ArrayList;
import java.util.function.Function;
import java.util.stream.Stream;

/**
* 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 static <T,R> ArrayList<R> map(ArrayList<T> obj, Function<T,R> func) {

ArrayList<R> ret = new ArrayList<>();
for (int i = 0; i < obj.size(); i++) {
ret.add(func.apply(obj.get(i)));

}return ret;

}


}
37 changes: 35 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,40 @@
* 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
*/
public class Arrays {
public static <___> Pair<E> firstLast(ArrayList<___> a) {
public class Arrays<E extends Comparable> {

public Arrays() {

}

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> E min(ArrayList<E> al) {
E min = al.get(0);
for (int i = 0; i < al.size(); i++)
if (al.get(i).compareTo(min) < 0) {
min = al.get(i);
} return min;


}

public static <E extends Comparable> E max(ArrayList<E> al) {

E max = al.get(0);
for (int i = 0; i < al.size(); i++)
if (al.get(i).compareTo(max) > 0) {
max = al.get(i);
} return max;

}

public static <E extends Comparable> Pair<E> minMax(ArrayList<E> al) {
return new Pair<E> (max(al), min(al));
}
}
30 changes: 29 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
package Pair;

import java.util.ArrayList;

/**
* 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 e1;
private E e2;

public Pair(E e1, E e2){
this.e1 = e1;
this.e2 = e2;
}

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


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

public E min() {
if (this.e1.compareTo(this.e2) < 0) return this.e1;
return this.e2;
}

public E max() {
if (this.e1.compareTo(this.e2) > 0) return this.e1;
return this.e2;
}
}
28 changes: 26 additions & 2 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,32 @@
* @param <E>
*/
public class GenericStack<E> {
private E[] elements;

public GenericStack() {
private E[] elements = (E[]) new Object[0];

GenericStack() {

}

public void push(E e) throws IndexOutOfBoundsException{
int newLength = elements.length +1;
E[] newArray = Arrays.copyOf(elements, newLength);
newArray[newArray.length -1] = e;
elements = newArray;

}

public E pop() throws IndexOutOfBoundsException{
int last = elements.length-1;
E poppedElement= elements[last];
E[] newArray = Arrays.copyOf(elements, last);
elements = newArray;
return poppedElement;
}

public boolean isEmpty() throws IndexOutOfBoundsException{
return elements.length == 0;
}


}
26 changes: 24 additions & 2 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,31 @@
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements;

public ObjectStack() {
private Object[] elements = new Object[0];

ObjectStack() {

}

public void push(Object o) throws IndexOutOfBoundsException{
int newLength = elements.length +1;
Object[] newArray = Arrays.copyOf(elements, newLength);
newArray[newArray.length -1] = o;
elements = newArray;
}

public E pop() throws IndexOutOfBoundsException{
int last = elements.length-1;
E poppedElement= (E) elements[last];
Object[] newArray = Arrays.copyOf(elements, last);
elements = newArray;
return poppedElement;
}

public boolean isEmpty() throws IndexOutOfBoundsException{
return this.elements.length == 0;
}


}
19 changes: 18 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;

private ArrayList<E> elements = new ArrayList<>();

public Stack(){

}

public void push(E input) throws IndexOutOfBoundsException{
elements.add(input);
}

public E pop() throws IndexOutOfBoundsException{
int last = elements.size()-1;
E top= elements.get(elements.size()-1);
elements.remove(last);
return top;
}

public boolean isEmpty() {
if (elements.size() == 0) return true;
else return false;
}

}
9 changes: 9 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,20 @@ public Entry(K key, V 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;
}

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

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

public void put(K k, V v) {
for (Entry entry : entries) {
if (entry.getKey().equals(k)) {
entry.setValue(v);
return;
}
} entries.add( new Entry(k, v));
}

public V get(K k) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(k)) {
return (V) entries.get(i).getValue();
}
}
return null;
}

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

}
63 changes: 63 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,67 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry> entries;

public TableNested() {

this.entries = new ArrayList<>();

}

public void put(K k, V v) {
for (Entry entry : entries) {
if (entry.getKey().equals(k)) {
entry.setValue(v);
return;
}
}
entries.add(new Entry(k, v));
}

public V get(K k) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(k)) {
return (V) entries.get(i).getValue();
}
}
return null;
}

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

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

}
Loading