Skip to content

finished #59

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
8 changes: 8 additions & 0 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,12 @@
* 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);
}
}
5 changes: 5 additions & 0 deletions src/main/java/Employee/Employee.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,10 @@ public final String getName() {
public double getSalary() {
return salary;
}

@Override
public String toString() {
return this.getName();
}
}

8 changes: 7 additions & 1 deletion src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package MapFunc;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* 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 static <T, R> ArrayList map(ArrayList<T> list, Function<T, R> function) {
return (ArrayList<T>)list.stream().map(function).collect(Collectors.toList());
}
}
22 changes: 21 additions & 1 deletion 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 @@ -11,6 +12,25 @@
* 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<E>(a.get(0), a.get(a.size() -1));
}

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

public static <E extends Comparable <? super E>> E max(ArrayList<? extends E> inputArr) {
Collections.sort(inputArr);
Collections.reverse(inputArr);
return inputArr.get(0);
}

public static <E extends Comparable<? super E>> Pair<E> minMax(ArrayList<? extends E> input) {
E min = Arrays.min(input);
E max = Arrays.max(input);
return new Pair<E>(min, max);
}
}

30 changes: 28 additions & 2 deletions src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,38 @@
package Pair;

import java.util.Collections;

/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* getFirst
* getSecond
* 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 extends Comparable<? super E>> E min() {
if (first.compareTo(second) == 1) {
return (E) second;
}
return (E) first;
}

}
public E max() {
if(first.compareTo(second) == -1) {
return second;
}
return first;
}
}
33 changes: 33 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package StackArray;

import java.util.ArrayList;
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 @@ -11,5 +13,36 @@ public class GenericStack<E> {
private E[] elements;

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

public E push(E item) {
this.elements = Arrays.copyOf(this.elements, this.size()+ 1);
this.elements[this.size() - 1] = item;
return item;
}

public E pop() {
E oldValue = this.elements[this.size() -1];
int numMoved = size() - this.size() - 1;
if (numMoved > 0)
System.arraycopy(this.elements, this.size() + 1, this.elements, this.size(),
numMoved);
this.elements = Arrays.copyOf(this.elements, this.size() -1);

return oldValue;
}

public boolean isEmpty() {
for (E element: this.elements) {
if (element != null){
return false;
}
}
return true;
}

public int size() {
return this.elements.length;
}
}
30 changes: 30 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ public class ObjectStack<E> {
private Object[] elements;

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

public Object push(Object item) {
this.elements = Arrays.copyOf(this.elements, this.size()+ 1);
this.elements[this.size() - 1] = item;
return item;
}

public Object pop() {
Object oldValue = this.elements[this.size() -1];
int numMoved = size() - this.size() - 1;
if (numMoved > 0)
System.arraycopy(this.elements, this.size() + 1, this.elements, this.size(),
numMoved);
this.elements = Arrays.copyOf(this.elements, this.size() -1);

return oldValue;
}

public boolean isEmpty() {
for(Object element: this.elements) {
if (element != null) {
return false;
}
}
return true;
}

public int size() {
return this.elements.length;
}
}
19 changes: 18 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@
* 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 E pop() {
return this.elements.remove(elements.size() -1);
}

public E push(E item) {
this.elements.add(item);
return item;
}

public boolean isEmpty() {
return this.elements.isEmpty();
}




}
51 changes: 50 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Table;

import java.util.ArrayList;
import java.util.Hashtable;


/**
* This class needs to manage an ArrayList of Entry objects. It needs a get method that takes a key and returns
Expand All @@ -10,8 +12,55 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;


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

}

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

public V put(K key, V value) {
int keyIndex = -1;
int counter = 0;
for(Entry entry: entries) {
counter++;
if (entry.getKey().equals(key)) {
keyIndex = counter;
}
}

if(keyIndex != -1) {
this.entries.set(keyIndex -1, new Entry(key, value));
} else {
this.entries.add(new Entry(key, value));
}
return value;
}

public void remove(K key) {
int keyIndex = -1;
int counter = 0;
for(Entry entry: entries) {
counter++;
if (entry.getKey().equals(key)) {
keyIndex = counter;
}
}

if(keyIndex != -1) {
this.entries.remove(keyIndex -1);
}
}
}


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

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

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

public V put(K key, V value) {
int keyIndex = -1;
int counter = 0;
for(Entry entry: entries) {
counter++;
if (entry.getKey().equals(key)) {
keyIndex = counter;
}
}
if(keyIndex != -1) {
this.entries.set(keyIndex -1, new Entry(key, value));
} else {
this.entries.add(new Entry(key, value));
}
return value;
}


public void remove(K key) {
int keyIndex = -1;
int counter = 0;
for(Entry entry: entries) {
counter++;
if (entry.getKey().equals(key)) {
keyIndex = counter;
}
}

if(keyIndex != -1) {
this.entries.remove(keyIndex -1);
}
}


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