Skip to content
Closed
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
Binary file not shown.
Binary file added out/production/main/Employee/Employee.class
Binary file not shown.
Binary file added out/production/main/Employee/Manager.class
Binary file not shown.
Binary file added out/production/main/MapFunc/MapFunc.class
Binary file not shown.
Binary file added out/production/main/Pair/Arrays.class
Binary file not shown.
Binary file added out/production/main/Pair/Pair.class
Binary file not shown.
Binary file added out/production/main/StackArray/GenericStack.class
Binary file not shown.
Binary file added out/production/main/StackArray/ObjectStack.class
Binary file not shown.
Binary file added out/production/main/StackArrayList/Stack.class
Binary file not shown.
Binary file added out/production/main/Swap/Swap.class
Binary file not shown.
Binary file added out/production/main/Table/Entry.class
Binary file not shown.
Binary file added out/production/main/Table/Table.class
Binary file not shown.
Binary file not shown.
Binary file added out/production/main/TableNested/TableNested.class
Binary file not shown.
Binary file not shown.
Binary file added out/test/test/MapFunc/MapFuncTest.class
Binary file not shown.
Binary file added out/test/test/Pair/ArraysTest.class
Binary file not shown.
Binary file added out/test/test/Pair/PairTest.class
Binary file not shown.
Binary file added out/test/test/StackArray/GenericStackTest.class
Binary file not shown.
Binary file added out/test/test/StackArray/ObjectStackTest.class
Binary file not shown.
Binary file added out/test/test/StackArrayList/StackTest.class
Binary file not shown.
Binary file added out/test/test/Swap/SwapTest.class
Binary file not shown.
Binary file added out/test/test/Table/TableTest.class
Binary file not shown.
Binary file added out/test/test/TableNested/TableNestedTest.class
Binary file not shown.
11 changes: 11 additions & 0 deletions 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 @@ -9,4 +11,13 @@
* 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);
}

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

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

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

public static <T,R> ArrayList<R> map(List<T> item, Function<T,R> function){
ArrayList<R> result = new ArrayList<R>();

for(T t: item){
result.add(function.apply(t));
}

return result;

}
}
34 changes: 32 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
* In here you must make firstLast, which will return a pair of the first element in the array list and the last
Expand All @@ -11,6 +12,35 @@
* 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) {

public static <E extends Comparable<E>> Pair<E> firstLast(ArrayList<E> a){

Pair<E> pair = new Pair<E>(a.get(0), a.get(a.size() - 1));
return pair;
}

public static <E extends Comparable<E>> E min(ArrayList<E> al) {
//al.sort(Comparator.naturalOrder());
Collections.sort(al);
return al.get(0);

}

public static <E extends Comparable> E max(ArrayList<E> al) {
// al.sort(Comparator.naturalOrder());
Collections.sort(al);
return al.get(al.size() -1);

}

public static <E extends Comparable<E>> Pair<E> minMax(ArrayList<E> al) {
// Collections.sort(al);

// al.sort(Comparator.naturalOrder());


Pair<E> pair = new Pair<E>(min(al), max(al));
return pair;
}
}
}
32 changes: 31 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
package Pair;

import java.util.function.Supplier;

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

private E first;
private E second;


public Pair(E a, E b){
this.first = a;
this.second = b;
}

public E getFirst() {
return first;
}

public E getSecond() {
return second;
}

public E min(){
int result =getFirst().compareTo(getSecond());
return result>0? getSecond():getFirst();
}

public E max(){
int result =getFirst().compareTo(getSecond());
return result>0? getFirst():getSecond();
}


}
29 changes: 28 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 java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;

/**
Expand All @@ -10,6 +12,31 @@
public class GenericStack<E> {
private E[] elements;

public GenericStack() {
public GenericStack(E[] elements) {
this.elements = elements;
}

public GenericStack( ) {
elements = (E[])(new ArrayList<E>()).toArray();
// elements = (E[])new Object[1];
}

public void push(E foo) {
elements = Arrays.copyOf(elements, elements.length +1);
elements[elements.length -1] = foo;

}

public E pop() {
E lastElement = elements[elements.length - 1];
elements = Arrays.copyOf(elements, elements.length -1);
return lastElement;
}

public boolean isEmpty() {
if (elements.length == 0){
return true;
}
return false;
}
}
29 changes: 27 additions & 2 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package StackArray;

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

/**
Expand All @@ -8,9 +9,33 @@
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements;
private E[] elements;

public ObjectStack() {
public ObjectStack(E[] elements) {
this.elements = elements;
}

public ObjectStack( ) {
elements = (E[])(new ArrayList<E>()).toArray();
// elements = (E[])new Object[1];
}

public void push(E foo) {
elements = Arrays.copyOf(elements, elements.length +1);
elements[elements.length -1] = foo;

}

public E pop() {
E lastElement = elements[elements.length - 1];
elements = Arrays.copyOf(elements, elements.length -1);
return lastElement;
}

public boolean isEmpty() {
if (elements.length == 0){
return true;
}
return false;
}
}
16 changes: 15 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,24 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {
private ArrayList elements;
private ArrayList<E> elements;



public Stack(){
elements = new ArrayList<E>();
}

public E pop() {
if (elements.size()!=0) return elements.remove(elements.size() -1);
else throw new IndexOutOfBoundsException();
}

public void push(E element) {
elements.add(element);
}

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

public void setValue(V value) {
this.value = value;
}
}
36 changes: 34 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,40 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList <Entry<K,V>> entries = new ArrayList<>();

public Table() {
}
}

public void put(K key, V value) {

for (Entry<K, V> entry: entries){
if (entry.getKey().equals(key))
entry.setValue(value);
}
entries.add(new Entry<>(key, value));

}


public V get(K key) {
for(Entry entry: entries){
if(entry.getKey().equals(key)){
V newValue = (V) entry.getValue();
return newValue;
}
}
return null;
}

public void remove(K key) {

Entry<K,V> toRemove = null;
for (Entry<K,V> entry:entries)
if (entry.getKey().equals(key))
toRemove = entry;

entries.remove(toRemove);
}

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

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

public TableNested() {
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 setValue(V value) {
this.value = value;
}
}

}

public void put(K key, V value) {

for (Entry<K, V> entry: entries){
if (entry.getKey().equals(key))
entry.setValue(value);
}
entries.add(new Entry<>(key, value));

}


public V get(K key) {
for(Entry entry: entries){
if(entry.getKey().equals(key)){
V newValue = (V) entry.getValue();
return newValue;
}
}
return null;
}

public void remove(K key) {

Entry<K,V> toRemove = null;
for (Entry<K,V> entry:entries)
if (entry.getKey().equals(key))
toRemove = entry;

entries.remove(toRemove);
}


}
Loading