Skip to content

1-3 done #56

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 3 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
17 changes: 16 additions & 1 deletion src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@
import java.util.ArrayList;

/**
* Create two generic methods that take two arraylists. The methods should both append the second ArrayList's items,
* Create two generic methods that take two arraylists. The methods should both
* append the second ArrayList's items,
* to the first. Use a wildcard for one of the type arguments in each method.
* 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 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;
}

}
18 changes: 15 additions & 3 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
import java.util.function.Function;

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

// type R bc it could return a different type but could be the same
public static <T, R> ArrayList map(ArrayList<T> input, Function<T, R> myFunction) {
//create new arraylist
ArrayList<R> mappedArrayList = new ArrayList<>();
for (int i = 0; i < input.size(); i++) {
R element = myFunction.apply(input.get(i));
mappedArrayList.add(element);
}
return mappedArrayList;
}
}

29 changes: 24 additions & 5 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,35 @@
package Pair;

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

/**
* In here you must make firstLast, which will return a pair of the first element in the array list and the last
* In here you must make firstLast, which will return a pair
* of the first element in the array list and the last
* element in the arraylist.
* You must also make a min method that returns the smallest item in the array list
* You must also make a min method that returns the smallest item
* in the array list
* 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
* 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> E min(ArrayList<E> arrayList){
Collections.sort(arrayList);
return arrayList.get(0);
}

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

public static <E extends Comparable> Pair<E> minMax (ArrayList<E> arrayList){
Collections.sort(arrayList);
//return new Pair<E>(arrayList.get(0), arrayList.get(arrayList.size() - 1));
return firstLast(arrayList);
}
}
35 changes: 33 additions & 2 deletions src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,43 @@
package Pair;

/**
* You need to store two values of type `E`, set them in a constructor, and have the following methods,
* 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 firstValue;
private E secondValue;

public Pair(E first, E second){
this.firstValue = first;
this.secondValue = second;
}

public E getFirst(){
return this.firstValue;
}

public E getSecond(){
return this.secondValue;
}
public E min(){
if (firstValue.compareTo(secondValue) == 1){
return secondValue;
}
return firstValue;
}

public E max(){
if (firstValue.compareTo(secondValue) == -1){
return secondValue;
}
return firstValue;
}


}
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 temp = elements[elements.length-1];
elements = Arrays.copyOf(elements, elements.length - 1);
return temp;
}
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 elementToAdd){
elements = Arrays.copyOf(elements, elements.length+1);
elements[elements.length - 1] = elementToAdd;
}
public Object pop(){
Object temp = elements[elements.length -1];
elements = Arrays.copyOf(elements, elements.length - 1);
return temp;
}
public boolean isEmpty(){
return elements.length == 0;
}
}
17 changes: 16 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,22 @@ public class Stack<E> {
private ArrayList elements;


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

public E pop() {
E lastElement;
lastElement = (E) elements.get(elements.size() - 1);
elements.remove(lastElement);
return lastElement;
}

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

public Boolean isEmpty() {
return elements.size() == 0;
}
}
47 changes: 46 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Table;

import java.util.ArrayList;
import java.util.Map;

/**
* 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 +11,52 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;
private ArrayList<Entry> entries;


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

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 boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

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

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

}
86 changes: 85 additions & 1 deletion src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,95 @@
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.
* 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>();
}
//Entry class within TableNested Class
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;
}

}
//end of nested class

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 boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

public void put(K key, V value) {
if (containsKey(key)) {
int index = entries.indexOf(getEntry(key));
if (entries.get(index).getKey().equals(key)) {
entries.remove(index);
entries.add(index, new Entry<>(key, value));
}
}
else {
entries.add(new Entry<>(key, value));
}
}

public void remove(K key) {
if (containsKey(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;
}

}




Loading