Skip to content

Completed #58

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 8 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
19 changes: 19 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ArrayListCombiner;

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

import static java.util.Collections.copy;

/**
* Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items,
Expand All @@ -9,4 +12,20 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {

public ArrayListCombiner(){

}

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


return first;
}

public static<E> ArrayList superCombiner(ArrayList<? super E>first,ArrayList<E>second){
first.addAll(second);
return first;
}
}
9 changes: 9 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
*/
public class MapFunc {


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

}
19 changes: 18 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
* 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 myArray = new 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>E min(ArrayList<E> inputArrayList){
Collections.sort(inputArrayList);
return inputArrayList.get(0);
}

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

public static <E extends Comparable> Pair<E> minMax(ArrayList<E>inputArrayList){
Collections.sort(inputArrayList);
return firstLast(inputArrayList);
}
}
28 changes: 27 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,32 @@
* 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)==1){
return second;
}
return first;
}
public E max(){
return first.compareTo(second)==1 ? first : second;
}


}
14 changes: 14 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,19 @@ public class GenericStack<E> {
private E[] elements;

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

public void push(E element){
elements= Arrays.copyOf(elements,elements.length+1);
elements[elements.length-1]=element;
}
public E pop(){
E elementPoppded = elements[elements.length-1];
elements=Arrays.copyOf(elements,elements.length-1);
return elementPoppded;
}
public boolean isEmpty(){
return elements.length==0;
}
}
14 changes: 14 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public class ObjectStack<E> {
private Object[] elements;

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

}

public void push(Object element){
elements=Arrays.copyOf(elements,elements.length+1);
elements[elements.length-1]=element;
}
public Object pop(){
Object elementPopped = elements[elements.length-1];
elements=Arrays.copyOf(elements,elements.length-1);
return elementPopped;
}
public boolean isEmpty(){
return elements.length==0;
}
}
14 changes: 14 additions & 0 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ public class Stack<E> {


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

public void push(E element){
elements.add(element);
}
public E pop(){
E elementPopped = (E) elements.get(elements.size()-1);
elements.remove(elements.size()-1);

return elementPopped;
}

public boolean isEmpty() {
return elements.size()==0;
}
}
61 changes: 59 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,65 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;

private ArrayList<Entry> entries;
Table table;
public Table() {
entries = new ArrayList<Entry>();
}

public boolean contains(K key){
for (Entry entry:entries) {
if(entry.getKey().equals(key)){
return true;
}
}
return false;
}

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 void put(K key, V value){
Entry myEntry;
if(contains(key)){
myEntry = getEntry(key);
int index =entries.indexOf(myEntry);
entries.remove(index);
entries.add(index,new Entry(key,value));
}else{
entries.add(new Entry(key,value));
}
}

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

}
public Entry getEntry(K key){
for(int i=0;i<entries.size();i++) {
if (entries.get(i).getKey().equals(key)) {
return entries.get(i);
}
}
return null;

}






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

import Table.Entry;

import java.util.ArrayList;

/**
* All you need to do for this microlab is take the Table and Entry from the last one and make Entry a nested class.
* Think about how nested classes should work with generics.
*/
public class TableNested<K, V> {
private ArrayList<Entry> entries;
public TableNested(){
entries = new ArrayList<Entry>();

}
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 boolean contains(K key){
for (TableNested.Entry entry:entries) {
if(entry.getKey().equals(key)){
return true;
}
}
return false;
}

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 void put(K key, V value){
if(contains(key)){
int index =entries.indexOf(getEntry(key));
entries.remove(index);
entries.add(index,new Entry(key,value));
}else{
entries.add(new Entry(key,value));
}
}

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

}
public Entry getEntry(K key){
for(int i=0;i<entries.size();i++) {
if (entries.get(i).getKey().equals(key)) {
return entries.get(i);
}
}
return null;

}




}
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