Skip to content

First 3 Parts #63

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 2 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
13 changes: 12 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.Employee;

import java.util.ArrayList;

/**
Expand All @@ -8,5 +10,14 @@
* 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<E> {

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

public static <T,R> ArrayList map(ArrayList<T> arrayList, Function<T,R> function) {
ArrayList<T> newArrayList = new ArrayList<T>();
for(int i =0; i< arrayList.size(); i++) {
newArrayList.add((T) function.apply(arrayList.get(i)));
}
return newArrayList;
}
}
17 changes: 15 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
* 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));
}
}
35 changes: 33 additions & 2 deletions src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,37 @@
* 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 first;
}

public E getSecond() {
return second;
}

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

public E max() {
if(first.compareTo(second) > 0) {
return first;
} else {
return second;
}
}
}
25 changes: 24 additions & 1 deletion src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,30 @@
*/
public class GenericStack<E> {
private E[] elements;
private int top;
private final static int DEFAULT_SIZE = 10;

public GenericStack() {
this(DEFAULT_SIZE);
}
}

public GenericStack(int initSize)
{
elements = (E[]) new Object [initSize];
top = -1;
}

public E pop() throws IndexOutOfBoundsException{
if (top == -1)
throw new IndexOutOfBoundsException();
return elements[top--];
}

public boolean isEmpty() {
return (top == -1);
}

public void push(E e) {
elements[++top] = e;
}
}
23 changes: 22 additions & 1 deletion src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,29 @@
*/
public class ObjectStack<E> {
private Object[] elements;
private int top;
private final static int DEFAULT_SIZE = 10;

public ObjectStack() {
this(DEFAULT_SIZE);
}
public boolean isEmpty() {
return (top == -1);
}

public ObjectStack(int initSize)
{
elements = (E[]) new Object [initSize];
top = -1;
}

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

public E pop() throws IndexOutOfBoundsException{
if (top == -1)
throw new IndexOutOfBoundsException();
return (E) elements[top--];
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;

private ArrayList<E> elements;

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

public boolean isEmpty() {
return this.elements.size() == 0;
}

public E pop () throws IndexOutOfBoundsException{
return this.elements.remove(this.elements.size() - 1);
}

public void push(E e) {
this.elements.add(e);
}
}
}
48 changes: 46 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Table;

import java.util.ArrayList;
import java.util.Map;

/**
* This class needs to manage an ArrayList of Entry objects. It needs a get method that takes a key and returns
Expand All @@ -10,8 +11,51 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

public Table() {
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 boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

public void put(K key, V value) {
if (containsKey(key)) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
entries.remove(i);
entries.add(i, new Entry(key, value));
}
}
} else {
entries.add(new Entry(key, value));
}
}

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

}
69 changes: 69 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,71 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry> entries;

public TableNested() {
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 boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

public void put(K key, V value) {
if (containsKey(key)) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
entries.remove(i);
entries.add(i, new Entry(key, value));
}
}
} else {
entries.add(new Entry(key, value));
}
}

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

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

}


}
Loading