Skip to content

completed #70

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 1 commit 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
16 changes: 15 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 @@ -8,5 +10,17 @@
* 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 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);

}
}
9 changes: 9 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package MapFunc;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.function.Function;

Expand All @@ -9,4 +10,12 @@
*/
public class MapFunc {

public static <T, R> ArrayList<R> map(ArrayList<T>list, Function <T, R> func){
ArrayList<R> newList = new ArrayList<>();
for(int i = 0; i < list.size(); i++) {
newList.add(func.apply(list.get(i)));
}
return newList;
}

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

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

}

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

}


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

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

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

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

public E max () {
if (first.compareTo(second) < 0) {
return second;
}
return first;
}
}
33 changes: 32 additions & 1 deletion src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,46 @@
package StackArray;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;

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

private E[] elements = (E[]) new Object[10];
int size = 0;


public GenericStack() {
}

public void push(E item) {

if (elements.length == size) {
elements = Arrays.copyOf(elements, elements.length + 5);
} else {
elements[size] = item;
size++;
}
}

public E pop() {

E last = elements[size-1];
elements[size -1] = null;
size --;
return last;
}

public boolean isEmpty(){

if(size == 0) {
return true;
}else
return false;
}
}
30 changes: 29 additions & 1 deletion src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,37 @@
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements;

private Object[] elements = new Object[10];
int size = 0;

public ObjectStack() {

}

public void push(E item) {

if (elements.length == size) {
elements = Arrays.copyOf(elements, elements.length + 5);
} else {
elements[size] = item;
size++;
}
}

public E pop() {

E last = (E) elements[size-1];
elements[size -1] = null;
size --;
return last;
}

public boolean isEmpty(){

if(size == 0) {
return true;
}else
return false;
}
}
33 changes: 31 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,39 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;

//DONT FORGET TO INITIALIZE TO AVOID NULL POINTER EXCEPTIONS!!!

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

public Stack(){
}

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

public void push(E item){

elements.add(item);

}

public E pop(){

E last = elements.get(elements.size()-1);

elements.remove(elements.size()-1);

return last;
}

public boolean isEmpty(){

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

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

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

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

public K getKey() {

return key;
}

public V getValue() {

return value;
}

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

private ArrayList<Entry<K, V>> entries = new ArrayList<>();

public Table() {
}

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

public void put (K foo, V i) {
for (int j = 0; j < entries.size(); j++) {
if (entries.get(j).getKey().equals(foo)) {
entries.remove(j);
entries.add(new Entry<>(foo, i));
break;
}
}
entries.add(new Entry<>(foo, i));
}

public void remove(K foo) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(foo)) {
entries.remove(i);
break;
}
}
}
}
37 changes: 37 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package TableNested;

import Table.Entry;

import java.util.ArrayList;

/**
Expand All @@ -8,4 +10,39 @@
*/
public class TableNested<K, V> {

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

private ArrayList<Entry<K, V>> entries;

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

public void put (K foo, V i) {
for (int j = 0; j < entries.size(); j++) {
if (entries.get(j).getKey().equals(foo)) {
entries.remove(j);
entries.add(new Entry<>(foo, i));
break;
}
}
entries.add(new Entry<>(foo, i));
}

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

}
Loading