Skip to content

done #76

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 7 commits into
base: master
Choose a base branch
from
Open

done #76

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

import java.lang.reflect.Array;
import java.util.ArrayList;

/**
Expand All @@ -9,4 +10,20 @@
* 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);
}
}


//to describe static in my head....
//you don't need the juice to have the sauce
//so for example, a class is the sauce...makes a meal what it is
//an object is the juice, great flavor but not what you want with everymeal
//if you check out the test, and you notice its not instantiated you don't need the juice
//because the test already has the sauce
12 changes: 12 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,16 @@
*/
public class MapFunc {

//okay katrice, so public method is because this test does not create a instance of MapFunc
//The <T, R> when its static you have to define the generic so you can pass it into a static
//We are returning an Arraylist of the results and they take in an ArrayList of type T
public static <T,R> ArrayList<R> map(ArrayList<T> lists, Function<T,R> function){
ArrayList<R> result = new ArrayList<>();
for(T item: lists){
R entry = function.apply(item);
result.add(entry);

}
return result;
}
}
24 changes: 23 additions & 1 deletion src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,28 @@
* 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) {
return new Pair(a.get(0), a.get(a.size()-1));
}


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) {

return new Pair<E>(min(al), max(al));

}
}
29 changes: 28 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,33 @@
* 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)>0);
return first;

}

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

}

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

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

public E push(E value){
//needs to remove an element from E[] and resize
//now I just need to push the value onto the end of the array
E[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
newArray[newArray.length-1] = value;
this.elements = newArray;
return value;

}

public E pop(){
//according to the api, pop should return a value
//didn't need to assign the value
E value = this.elements[elements.length-1];
E[] newArray = Arrays.copyOf(elements, elements.length - 1);
//newArray[newArray.length-1] = value;
this.elements = newArray;
return value;
}

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

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

public ObjectStack() {
this.elements = new Object[0];
}
//the way stacks work, its just like generics accept you are using Objects
public Object push(Object value){
Object[] newArray = Arrays.copyOf(this.elements, this.elements.length + 1);
newArray[newArray.length-1] = value;
this.elements = newArray;
return value;
}

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

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

private ArrayList elements;


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

public void push(E value){
//pushes adds an element
this.elements.add(value);
}

public E pop(){
//removes an element
return (E) elements.remove(this.elements.size()-1);
}

public boolean isEmpty(){
//Checks to see is the elements array is empty, if its equal to 0 its empty.
return (elements.size()==0);
}


}
6 changes: 6 additions & 0 deletions src/main/java/Swap/Swap.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ public static <T> T[] swap(int i, int j, T... values) {
return values;
}
}

//Explanation
//Stating Generic T is equal to the indexes i of values
//those indexes are also equal to the indexes j of values
//and index j of values equals temp
//now return values
40 changes: 39 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,46 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;

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

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

public void put(K key, V value){
//need to enter value and key into entry
int counter = 0;
for(Entry e : entries){
if(e.getKey().equals(key)){
entries.set(counter, new Entry<>(key, value));
return;
}
}
entries.add(new Entry<>(key, value));
}

public void remove(K key){
//needs to take a key, and if that key exists in the arraylist, remove the item
for(Entry e : entries){
if(e.getKey().equals(key)){
entries.remove(key);
return;
}
}


}


}


57 changes: 57 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,62 @@
* Think about how nested classes should work with generics.
*/
public class TableNested<K, V> {
private ArrayList<Table.Entry> entries;

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

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

public void put(K key, V value) {
//need to enter value and key into entry
int counter = 0;
for (Table.Entry e : entries) {
if (e.getKey().equals(key)) {
entries.set(counter, new Table.Entry<>(key, value));
return;
}
}
entries.add(new Table.Entry<>(key, value));
}

public void remove(K key) {
//needs to take a key, and if that key exists in the arraylist, remove the item
for (Table.Entry e : entries) {
if (e.getKey().equals(key)) {
entries.remove(e);
return;
}
}


}


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;
}

}
}
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