Skip to content

completed full lab #60

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
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
16 changes: 14 additions & 2 deletions src/main/java/ArrayListCombiner/ArrayListCombiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@

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 class ArrayListCombiner<E> {


public static <E> void extendCombiner(ArrayList<E> firstArray, ArrayList<? extends E> secondArray){
firstArray.addAll(secondArray);
}

public static <E> void superCombiner(ArrayList<? super E> firstArray, ArrayList<E> secondArray){
firstArray.addAll(secondArray);
}

}
1 change: 1 addition & 0 deletions src/main/java/Employee/Employee.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package Employee;

public class Employee {

private String name;
private double salary;

Expand Down
1 change: 1 addition & 0 deletions src/main/java/Employee/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public void setBonus(double bonus) {
this.bonus = bonus;
}

@Override
public double getSalary() { // Overrides superclass method
return super.getSalary() + bonus;
}
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/MapFunc/MapFunc.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package MapFunc;


import java.util.ArrayList;
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.
*/
public class MapFunc {
public class MapFunc<T,R> {


public static <T,R> ArrayList<R> map(ArrayList<T> array, Function<T,R> function) {
ArrayList<R> entryPairs = new ArrayList<>();
for(T item: array){
R result = function.apply(item);
entryPairs.add(result);
}
return entryPairs;
}

}
35 changes: 33 additions & 2 deletions src/main/java/Pair/Arrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,38 @@
* 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<E extends Comparable<? super E>> {

public static <E extends Comparable<? super E>> 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<E> a) {
E min = a.get(0);
for (int i = 0; i < a.size(); i++) {
if (min.compareTo(a.get(i)) > 0) min = a.get(i);
}
return min;
}

public static <E extends Comparable<? super E>> E max(ArrayList<E> a) {
E max = a.get(0);
for (int i = 0; i < a.size(); i++) {
if (max.compareTo(a.get(i)) < 0) max = a.get(i);
}
return max;
}

public static <E extends Comparable<? super E>> Pair<E> minMax(ArrayList<E> a) {
E min = a.get(0);
E max = a.get(0);
for(int i = 0; i<a.size(); i++){
if(min.compareTo(a.get(i))>0) min = a.get(i);
if(max.compareTo(a.get(i))<0) max = a.get(i);
}
return new Pair<>(min,max);
}


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

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

/**
* 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<? super E>> implements Comparable<Pair<E>> {

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 second;
else return first;
}

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

@Override
public int compareTo(Pair<E> o) {
// if(first.equals(o.getFirst())){
// return 0;
// } else if (){
// return 1;
// }
return 0;
}


}
19 changes: 19 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.Arrays;
import java.lang.*;

/**
* Expand the ArrayList implementation of stack here to use an E[] array. Still implement push, pop, and isEmpty.
Expand All @@ -11,5 +13,22 @@ public class GenericStack<E> {
private E[] elements;

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

public void push(E object){
elements = Arrays.copyOf(elements, elements.length+1);
elements[elements.length-1] = object;
}

public E pop(){
E object = elements[elements.length-1];
elements = Arrays.copyOf(elements, elements.length-1);
return object;
}

public boolean isEmpty(){
if(elements.length == 0) return true;
else return false;
}
}
16 changes: 16 additions & 0 deletions src/main/java/StackArray/ObjectStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ public class ObjectStack<E> {
private Object[] elements;

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

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

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

public boolean isEmpty(){
if(elements.length == 0) return true;
else return false;
}
}
21 changes: 20 additions & 1 deletion src/main/java/StackArrayList/Stack.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,29 @@
* 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<E>();
}

public ArrayList getElements() {
return elements;
}

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

public E pop(){
return 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
49 changes: 48 additions & 1 deletion src/main/java/Table/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,55 @@
* Void return on `remove`.
*/
public class Table<K, V> {
private ArrayList entries;

private ArrayList<Entry<K, V>> entries;

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

public ArrayList<Entry<K, V>> getEntries() {
return entries;
}

public V get(K key) {
V value = null;
for (int i = 0; i < entries.size(); i++) {
K entryKey = entries.get(i).getKey();
if (entryKey.equals(key)) {
value = entries.get(i).getValue();
}
}
return value;
}
public int size(){
return entries.size();
}

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

}

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



}
69 changes: 69 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,71 @@
*/
public class TableNested<K, V> {

private ArrayList<Entry<K, V>> entries;

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

public ArrayList<Entry<K, V>> getEntries() {
return entries;
}

public V get(K key) {
V value = null;
for (int i = 0; i < entries.size(); i++) {
K entryKey = entries.get(i).getKey();
if (entryKey.equals(key)) {
value = entries.get(i).getValue();
}
}
return value;
}
public int size(){
return entries.size();
}

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

}

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

private class Entry<H,R> {
private H key;
private R value;

public Entry(H key, R value) {
this.key = key;
this.value = value;
}

public H getKey() {
return key;
}

public R getValue() {
return value;
}

}

}
Loading