Skip to content

assignment complete. enjoy. #77

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
14 changes: 14 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 Table.Entry;

import java.util.ArrayList;

/**
Expand All @@ -9,4 +11,16 @@
* The second method should be called superCombiner and should use ? super E
*/
public class ArrayListCombiner {

public static <E> ArrayList extendCombiner(ArrayList <E> firstArray, ArrayList <? extends E> secondArray){
firstArray.addAll(secondArray);
return firstArray;

}

public static <E> ArrayList superCombiner(ArrayList <? super E> firstArray, ArrayList <E> secondArray){
firstArray.addAll(secondArray);
return firstArray;

}
}
10 changes: 10 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
*/
public class MapFunc {

// in this case, T is input and R is output
public static <T, R> ArrayList<R> map(ArrayList<T> myInput, Function<T,R> myFunc){
ArrayList<R> output = new ArrayList<>();
for (T element : myInput){
output.add(myFunc.apply(element));
}
return output;

}

}
40 changes: 38 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 @@ -10,7 +11,42 @@
* A max method that returns the largest item in the arraylist
* 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 class Arrays<E extends Comparable> {
public static <E extends Comparable> Pair<E> firstLast(ArrayList<E> arrayList) {
E firstItem = arrayList.get(0);
E lastItem = arrayList.get(arrayList.size()-1);

Pair<E> answer= new Pair<>(firstItem,lastItem);

return answer;

}

public static <E extends Comparable> E min (ArrayList<E> arrayList) {
arrayList.sort(Comparator.naturalOrder());
E minItem = arrayList.get(0);

return minItem;

}

public static <E extends Comparable> E max (ArrayList<E> arrayList) {
arrayList.sort(Comparator.naturalOrder());
E maxItem = arrayList.get(arrayList.size()-1);

return maxItem;


}

public static <E extends Comparable> Pair<E> minMax (ArrayList<E> arrayList) {
arrayList.sort(Comparator.naturalOrder());

Pair <E> answer = new Pair(arrayList.get(0), arrayList.get(arrayList.size()-1));

return answer;

}


}
35 changes: 34 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,39 @@
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E extends Comparable> {

E firstVal;
E secondVal;

public Pair(E firstVal, E secondVal){
this.firstVal=firstVal;
this.secondVal=secondVal;
}

public E getFirst(){
return firstVal;
}

public E getSecond(){
return secondVal;
}

public E min(){

E smallerOfTwo;
smallerOfTwo= firstVal.compareTo(secondVal) == 1 ? secondVal:firstVal;

return smallerOfTwo;
}

public E max(){

E largerOfTwo;
largerOfTwo= firstVal.compareTo(secondVal) == 1 ? firstVal:secondVal;

return largerOfTwo;

}

}
5 changes: 5 additions & 0 deletions src/main/java/Stack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//public class Stack <E>{
// public boolean isEmpty(){
// return false;
// }
//}
26 changes: 26 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package StackArray;

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.
Expand All @@ -10,6 +11,31 @@
public class GenericStack<E> {
private E[] elements;

// GenericStack<String> myStack = new GenericStack<>();

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

//casting to just an object: (not array)
// element = (E) new Object;

}

public boolean isEmpty() {

return elements.length==0;

}

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


public E pop() {
E lastElement = (E) elements[elements.length-1];
elements=Arrays.copyOf(elements,elements.length-1);
return lastElement;
}
}
17 changes: 17 additions & 0 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 @@ -11,6 +12,22 @@ public class ObjectStack<E> {
private Object[] elements;

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

}

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

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

public E pop() {
E lastElement = (E) elements[elements.length-1];
elements= Arrays.copyOf(elements,elements.length-1);
return lastElement;
}
}
19 changes: 19 additions & 0 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
* If you pop on an empty stack, throw an IndexOutOfBoundsException.
*/
public class Stack<E> {

private ArrayList elements;


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

public boolean isEmpty() {
boolean answer = false;
answer = elements.size()==0;

return answer;
}

public void push(E foobar) {
elements.add(foobar);

}

public E pop() {
E lastElement = (E) elements.get(elements.size()-1);
elements.remove(elements.size()-1);
return lastElement;
}
}
53 changes: 52 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package Table;

import java.lang.annotation.ElementType;
import java.util.ArrayList;

/**
Expand All @@ -10,8 +11,58 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

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

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

public V get(K key){

V ourLocalVariable = null;


for (Entry element: entries){
if (element.getKey().equals(key)){
ourLocalVariable =(V) element.getValue();
}
}

return ourLocalVariable;

}

public void put(K foo, V i){
Entry ourEntry = new Entry(foo, i);
for (Entry element: entries){
if (element.getKey().equals(ourEntry.getKey())){
entries.set(entries.indexOf(element),ourEntry);
}
}

entries.add(ourEntry);

}

public void remove(K foo){
for (Entry element: entries){
if (element.getKey().equals(foo)){
entries.remove(element);
break;
}
}
}

}
64 changes: 64 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,68 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry> entries;

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


public 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 V get(K key){

V ourLocalVariable = null;


for (Entry element: entries){
if (element.getKey().equals(key)){
ourLocalVariable =(V) element.getValue();
}
}

return ourLocalVariable;

}

public void put(K foo, V i){
Entry ourEntry = new Entry(foo, i);
for (Entry element: entries){
if (element.getKey().equals(ourEntry.getKey())){
entries.set(entries.indexOf(element),ourEntry);
}
}

entries.add(ourEntry);

}

public void remove(K foo){
for (Entry element: entries){
if (element.getKey().equals(foo)){
entries.remove(element);
break;
}
}
}


}
Loading