Skip to content

VVG #62

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

VVG #62

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
21 changes: 20 additions & 1 deletion src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,24 @@
* 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<E> {

// private ArrayList<E> first;
// private ArrayList<E> second;

public ArrayListCombiner() {
// first = new ArrayList<>();
// second = new ArrayList<>();
}

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

}

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

public static <T,R,E extends T> ArrayList<R> map(ArrayList<E> list, Function<T,R> function) {

ArrayList<R> newList = new ArrayList<>();

for (int i = 0; i < list.size(); i++) {
R after = function.apply(list.get(i));
newList.add(after);
}

return newList;
}

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

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

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

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

}

}
38 changes: 37 additions & 1 deletion src/main/java/Pair/Pair.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,48 @@
package Pair;

import java.util.ArrayList;
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 thing1, E thing2) {
this.first = thing1;
this.second = thing2;
}

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

public E getSecond() {
return this.second;
}

public E min() {
if (first.compareTo(second) < 0) {
return first;
} else {
return second;
}
}

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


}
35 changes: 35 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,46 @@
/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class GenericStack<E> {

private E[] elements;

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

public E push(E aThing) {

E[] temp = Arrays.copyOf(elements, elements.length + 1);

temp[elements.length] = aThing;
elements = temp;
return aThing;
}

public E pop() throws IndexOutOfBoundsException {

E poppedObj = elements[elements.length - 1];

if (elements.length > 0) {
E[] temp = Arrays.copyOf(elements, elements.length - 1);
for (int i = 0; i < elements.length - 1; i++) {
temp[i] = elements[i];
}
elements = temp;
}
return poppedObj;
}

public boolean isEmpty() {
if (elements.length > 0) {
return false;
} else {
return true;
}
}

}
32 changes: 32 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,44 @@
/**
* Expand the ArrayList implementation of stack here to use an Object[] array. Still implement push, pop, and isEmpty.
* Remember, you might need to resize the stack in the push method.
*
* @param <E>
*/
public class ObjectStack<E> {
private Object[] elements;

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

public Object push(Object aThing) {

Object[] temp = Arrays.copyOf(elements, elements.length + 1);

temp[elements.length] = aThing;
elements = temp;
return aThing;
}

public Object pop() throws IndexOutOfBoundsException {

Object poppedObj = elements[elements.length - 1];

if (elements.length > 0) {
Object[] temp = Arrays.copyOf(elements, elements.length - 1);
for (int i = 0; i < elements.length - 1; i++) {
temp[i] = elements[i];
}
elements = temp;
}
return poppedObj;
}

public boolean isEmpty() {
if (elements.length > 0) {
return false;
} else {
return true;
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package StackArrayList;

import java.util.ArrayList;
import java.util.Arrays;

/**
* Implement Stack<E> by adding the push, pop, and isEmpty functions. It must pass the prewritten unit tests.
Expand All @@ -11,6 +12,28 @@ public class Stack<E> {


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

public E push(E aThing) {

elements.add(aThing);
return (E)elements.get(elements.size() - 1);
}

public E pop() throws IndexOutOfBoundsException {

return (E)elements.remove(elements.size() - 1);

}

public boolean isEmpty() {
if (elements.size() > 0) {
return false;
} else {
return true;
}
}


}
4 changes: 4 additions & 0 deletions src/main/java/Table/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public V getValue() {
return value;
}

public void setValue(V anotherValue) {
this.value = anotherValue;
}

}
44 changes: 43 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,50 @@
* 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 (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return (V) entries.get(i).getValue();
}
}
return null;
}

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.get(i).setValue(value);
}
}
} else {
entries.add(new Entry(key, value));
}
}

public void remove(K key) {

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

public boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

}
70 changes: 70 additions & 0 deletions src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package TableNested;

import Table.Entry;

import java.util.ArrayList;

/**
Expand All @@ -8,4 +10,72 @@
*/
public class TableNested<K, V> {

private class EntryNested {
private K key;
private V value;

private EntryNested(K key, V value) {
this.key = key;
this.value = value;
}

private K getKey() {
return key;
}

private V getValue() {
return value;
}

private void setValue(V anotherValue) {
this.value = anotherValue;
}
}

private ArrayList<EntryNested> entries;

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

public V get(K 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) {
if (containsKey(key)) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
entries.get(i).setValue(value);
}
}
} else {
entries.add(new EntryNested(key, value));
}
}

public void remove(K key) {

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

private boolean containsKey(K key) {
for (int i = 0; i < entries.size(); i++) {
if (entries.get(i).getKey().equals(key)) {
return true;
}
}
return false;
}

}
Loading