Skip to content

done #65

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

done #65

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


import Employee.Employee;

import java.util.ArrayList;

/**
Expand All @@ -8,5 +11,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<T> {


public static <T> void extendCombiner(ArrayList<T> first, ArrayList<? extends T> second) {
first.addAll(second);
}

public static <T> void superCombiner(ArrayList<? super T> first, ArrayList<T> second) {
first.addAll(second);
}
}
10 changes: 9 additions & 1 deletion src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
* 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 class MapFunc{


public static <T,R> ArrayList map(ArrayList<T> intList, Function<T,R> lambdaExpression){
ArrayList<R> result = new ArrayList<>();
for(T value : intList) {
result.add(lambdaExpression.apply(value));
}
return result;
}
}
20 changes: 19 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@
* 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) {
Pair pair = new Pair(a.get(0), a.get(a.size()-1));
return pair;
}


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

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

public static <E extends Comparable> Pair<E> minMax(ArrayList<E> al) {
Collections.sort(al);
return firstLast(al);
}
}
24 changes: 23 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
* 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 last;

public Pair(E first, E last){
this.first = first;
this.last = last;
}

public E min(){
return (first.compareTo(last) == 1 ? last : first);
}

public E max(){
return (first.compareTo(last) == 1 ? first : last);
}

public E getFirst() {
return first;
}

public E getSecond() {
return last;
}
}
20 changes: 20 additions & 0 deletions src/main/java/StackArray/GenericStack.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 @@ -11,5 +12,24 @@ public class GenericStack<E> {
private E[] elements;

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

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

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

public boolean isEmpty(){
if(elements.length == 0){
return true;
}
return false;
}
}
18 changes: 18 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ public class ObjectStack<E> {
private Object[] elements;

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

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

public Object pop() throws IndexOutOfBoundsException{
Object result = elements[elements.length-1];
this.elements = Arrays.copyOf(elements, elements.length-1);
return result;
}

public boolean isEmpty(){
if(elements.length == 0){
return true;
}
return false;
}
}
21 changes: 20 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package StackArrayList;

import jdk.nashorn.internal.runtime.regexp.joni.constants.StackType;

import java.util.ArrayList;

/**
* Implement Stack<E> by adding the push, pop, and isEmpty functions. It must pass the prewritten unit tests.
* 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 void push(E element){
elements.add(element);
}

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

public boolean isEmpty(){
if (elements.size() == 0){
return true;
}
return false;
}
}
27 changes: 26 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,33 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry<K,V>> entries;

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

public void put(K key, V value){
Entry<K, V> newEntry = new Entry<>(key, value);
entries.add(newEntry);
}

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

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

}
46 changes: 46 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,51 @@
* Think about how nested classes should work with generics.
*/
public class TableNested<K, V> {
class Entry{
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;
}

}
private ArrayList<Entry> entries;

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

public void put(K key, V value){
Entry newEntry = new Entry(key, value);
entries.add(newEntry);
}

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

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

}
70 changes: 35 additions & 35 deletions src/test/java/ArrayListCombiner/ArrayListCombinerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,40 @@
import java.util.ArrayList;

public class ArrayListCombinerTest {
// Employee foo = new Employee("FOO", 100);
// Manager bar = new Manager("BAR", 100);
// @Test
// public void testExtendCombiner() throws Exception {
// // Given an array list with employees
// ArrayList<Employee> first = new ArrayList<>();
// first.add(foo);
// // An an array list with managers
// ArrayList<Manager> second = new ArrayList<>();
// second.add(bar);
// // When I combine them
// ArrayListCombiner.extendCombiner(first, second);
// // Then I should get an arrayList with both
// ArrayList<Employee> expected = new ArrayList<>();
// expected.add(foo);
// expected.add(bar);
// Assert.assertEquals(expected, first);
// }
//
// @Test
// public void testSuperCombiner() throws Exception {
// // Given an array list with employees
// ArrayList<Employee> first = new ArrayList<>();
// first.add(foo);
// // An an array list with managers
// ArrayList<Manager> second = new ArrayList<>();
// second.add(bar);
// // When I combine them
// ArrayListCombiner.superCombiner(first, second);
// // Then I should get an arrayList with both
// ArrayList<Employee> expected = new ArrayList<>();
// expected.add(foo);
// expected.add(bar);
// Assert.assertEquals(expected, first);
// }
Employee foo = new Employee("FOO", 100);
Manager bar = new Manager("BAR", 100);
@Test
public void testExtendCombiner() throws Exception {
// Given an array list with employees
ArrayList<Employee> first = new ArrayList<>();
first.add(foo);
// An an array list with managers
ArrayList<Manager> second = new ArrayList<>();
second.add(bar);
// When I combine them
ArrayListCombiner.extendCombiner(first, second);
// Then I should get an arrayList with both
ArrayList<Employee> expected = new ArrayList<>();
expected.add(foo);
expected.add(bar);
Assert.assertEquals(expected, first);
}

@Test
public void testSuperCombiner() throws Exception {
// Given an array list with employees
ArrayList<Employee> first = new ArrayList<>();
first.add(foo);
// An an array list with managers
ArrayList<Manager> second = new ArrayList<>();
second.add(bar);
// When I combine them
ArrayListCombiner.superCombiner(first, second);
// Then I should get an arrayList with both
ArrayList<Employee> expected = new ArrayList<>();
expected.add(foo);
expected.add(bar);
Assert.assertEquals(expected, first);
}

}
Loading