Skip to content

Finito #74

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 4 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
18 changes: 17 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,19 @@
* 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
{


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);
}
//superCombiner and extendCombiner

}
26 changes: 25 additions & 1 deletion src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@
* 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
{
/*

MapFunc -- Make a map method that takes an ArrayList and a
Function<T,R> object and returns an arraylist containing all of the elements
of the first with the function applied to them.

*/

//T is the Type and R is the return type against the T
public static<T,R> ArrayList map(ArrayList<T> inputArrayList, Function<T,R> function)
{
ArrayList<R> values = new ArrayList<R>();

for (int i = 0; i < inputArrayList.size(); i++)
{
R output = function.apply(inputArrayList.get(i));
values.add(output);

}

return values;

}

}
28 changes: 26 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,31 @@
* 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
{
//Pair<E> is the return type
//firstLast is the name of the method
//Parameter is an ArrayList of 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> a)
{
return Collections.min(a);
}

public static <E extends Comparable> Comparable max(ArrayList<E> a)
{
return Collections.max(a);
}

public static <E extends Comparable> Pair minMax(ArrayList<E> a)
{
return new Pair<>(min(a), max(a));
}



}
41 changes: 40 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@
* min -> returns the minimum of the pair
* max -> returns the maximum of the pair
*/
public class Pair {
public class Pair<E extends Comparable>
{
E first;
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 is less then second it returns a negative
if(first.compareTo(second) < 0)
{
return first;
}
return second;
}

public E max()
{
if(first.compareTo(second) > 0)
{
return first;
}

return second;
}



}
33 changes: 32 additions & 1 deletion src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@
public class GenericStack<E> {
private E[] elements;

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

public void push(E value)
{
E[] newArray = Arrays.copyOf(elements, elements.length +1);
newArray[newArray.length - 1] = value;
this.elements = newArray;
}
public boolean isEmpty()
{
if (this.elements.length == 0)
{
return true;
}
return false;

}


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




}
33 changes: 31 additions & 2 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,39 @@
* Remember, you might need to resize the stack in the push method.
* @param <E>
*/
public class ObjectStack<E> {
public class ObjectStack<E>
{
private Object[] elements;

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

}

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

public Object pop()
{
Object value = elements[elements.length - 1];
Object[] newArray = Arrays.copyOf(elements, elements.length - 1);
this.elements = newArray;
return value;

}

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

return false;
}
}
23 changes: 20 additions & 3 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,28 @@
* 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;
public class Stack<E>
{
private ArrayList<E> elements;

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

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

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

public E pop()
{
//Get the last element before removing it
E value = elements.get(elements.size() - 1);
elements.remove(elements.size() - 1);

return value;
}
}
43 changes: 41 additions & 2 deletions src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,47 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

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

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

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

entries.add(new Entry<>(key, value));
}

public void remove(Object key)
{
for (int i = 0; i < entries.size(); i++)
{
entries.remove(i);
}
}



}
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 java.util.ArrayList;
import java.util.Arrays;

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

//field
private ArrayList<Entry> entries;

//contructor
public TableNested()
{
this.entries = new ArrayList<>();
}

//PUT, GET, REMOVE methods

public void put(K key, V value)
{


for (int i = 0; i < entries.size(); i++)
{
if (entries.get(i).getKey().equals(key))
{
entries.set(i, new Entry<>(key, value));
return;
}

}

entries.add(new Entry<>(key, value));

}

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

public void remove(Object key)
{
for (int i = 0; i < entries.size(); i++)
{
entries.remove(i);
}

}



//Nested Class
private 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