Skip to content

working on table #67

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


import java.util.ArrayList;


/**
* 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 static <T> void superCombiner(ArrayList<? super T> first, ArrayList<T> second){
first.addAll(second);
}
public static <T> void extendCombiner(ArrayList<T> first, ArrayList<? extends T> second){
first.addAll(second);
}

}
9 changes: 9 additions & 0 deletions src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@
*/
public class MapFunc {


public static <T,R> ArrayList map(ArrayList<T> list, Function<T, R> object){
ArrayList<R> newList = new ArrayList<>();
for(T t : list){
newList.add(object.apply(t));
}
return newList;
}

}
23 changes: 21 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Pair;

import org.omg.CORBA.CODESET_INCOMPATIBLE;

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

Expand All @@ -10,7 +12,24 @@
* 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{

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> a){
Collections.sort(a);
return a.get(0);
}
public static <E extends Comparable> E max(ArrayList<E> a){
Collections.sort(a);
return a.get(a.size()-1);
}
public static <E extends Comparable> Pair<E> minMax(ArrayList<E> a){
Collections.sort(a);
return new Pair<E>(a.get(0), a.get(a.size()-1));
}

}

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;


/**
* 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;
Pair<E> p;

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(){
int result = compareTo(first);
boolean secondIsGreater = result == -1;
if(secondIsGreater){
return second;
}else return first;
}

public E max(){
int result = compareTo(first);
boolean secondIsGreater = result == 1;
if(secondIsGreater){
return first;
}else return second;
}
public int compareTo(E e){
return this.first.compareTo(e);
}
}
27 changes: 27 additions & 0 deletions src/main/java/StackArray/GenericStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,34 @@
*/
public class GenericStack<E> {
private E[] elements;
private int size = 0;

public GenericStack() {
elements = (E[]) new Object[0];
}
public void push(E element){
if(size == elements.length){
ensureCapacity();
}
elements[size++] = element;
}
public E pop(){
if(isEmpty()){
throw new IndexOutOfBoundsException();
}
E e = elements[--size];
elements[size] = null;
return e;
}
public boolean isEmpty(){
if(elements.length == 0){
return true;
}
return false;
}

private void ensureCapacity(){
int newSize = elements.length + 1;
elements = Arrays.copyOf(elements, newSize);
}
}
27 changes: 27 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package StackArray;


import java.util.Arrays;

/**
Expand All @@ -9,8 +10,34 @@
*/
public class ObjectStack<E> {
private Object[] elements;
private int size = 0;

public ObjectStack() {
elements = new Object[0];
}
public void push(E element){
if(size == elements.length){
ensureCapacity();
}
elements[size++] = element;
}
public Object pop(){
if(isEmpty()){
throw new IndexOutOfBoundsException();
}
Object e = elements[--size];
elements[size] = null;
return e;

}
public boolean isEmpty(){
if(elements.length == 0){
return true;
}
return false;
}
private void ensureCapacity(){
int newSize = elements.length + 1;
elements = Arrays.copyOf(elements, newSize);
}
}
18 changes: 16 additions & 2 deletions src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,29 @@

import java.util.ArrayList;


/**
* 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> {
public class Stack<E> {
private ArrayList elements;


public Stack(){

elements = new ArrayList();
}
public void push(E element){
elements.add(element);
}
public E pop() {
if (isEmpty()) {
throw new IndexOutOfBoundsException();
}
return (E) elements.remove(elements.size() - 1);
}
public boolean isEmpty(){
return elements.isEmpty();
}

}
1 change: 1 addition & 0 deletions src/main/java/Swap/Swap.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Keep this. Just make it so the tests pass.
*/
public class Swap {

public static <T> T[] swap(int i, int j, T... values) {
T temp = values[i];
values[i] = values[j];
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,40 @@
* 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 e : entries){
if(e.getKey().equals(key)){
return (V) e.getValue();
}
}
return null;
}

public void put(K key, V value){
int numberOfEntries = 0;
for(Entry e : entries){
if(e.getKey().equals(key)){
entries.set(numberOfEntries, new Entry(key, value));
}
numberOfEntries++;
}
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);
}
}
}
}
41 changes: 40 additions & 1 deletion src/main/java/TableNested/TableNested.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,43 @@
*/
public class TableNested<K, V> {

}

private ArrayList<Table.Entry> entries;


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

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

public void put(K key, V value){
int numberOfEntries = 0;
for(Table.Entry e : entries){
if(e.getKey().equals(key)){
entries.set(numberOfEntries, new Table.Entry(key, value));
}
numberOfEntries++;
}
entries.add(new Table.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);
}
}
}
}


Loading