From 3ad5a687cc986a323c47e7cdcbb6eaa3739356eb Mon Sep 17 00:00:00 2001 From: Eileen Parle Date: Sun, 8 Jan 2017 07:35:11 +0000 Subject: [PATCH] coursework 2 --- AbstractStack.java | 36 +++++++++++++ ArrayList.java | 70 ++++++++++++++++++++++++++ ArrayListTest.java | 31 ++++++++++++ ErrorMessage.java | 25 +++++++++ FunctionalArrayList.java | 22 ++++++++ FunctionalLinkedList.java | 21 ++++++++ FunctionalLinkedListTest.java | 11 ++++ FunctionalList.java | 35 +++++++++++++ ImprovedStack.java | 38 ++++++++++++++ ImprovedStackImpl.java | 56 +++++++++++++++++++++ ImprovedStackImplTest.java | 27 ++++++++++ LinkedList.java | 89 ++++++++++++++++++++++++++++++++ LinkedListTest.java | 29 +++++++++++ List.java | 95 +++++++++++++++++++++++++++++++++++ ReturnObject.java | 37 ++++++++++++++ ReturnObjectImpl.java | 33 ++++++++++++ ReturnObjectImplTest.java | 20 ++++++++ SampleableList.java | 14 ++++++ SampleableListImpl.java | 12 +++++ SampleableListTest.java | 13 +++++ Stack.java | 58 +++++++++++++++++++++ StackImpl.java | 35 +++++++++++++ StackImplTest.java | 24 +++++++++ 23 files changed, 831 insertions(+) create mode 100644 AbstractStack.java create mode 100644 ArrayList.java create mode 100644 ArrayListTest.java create mode 100644 ErrorMessage.java create mode 100644 FunctionalArrayList.java create mode 100644 FunctionalLinkedList.java create mode 100644 FunctionalLinkedListTest.java create mode 100644 FunctionalList.java create mode 100644 ImprovedStack.java create mode 100644 ImprovedStackImpl.java create mode 100644 ImprovedStackImplTest.java create mode 100644 LinkedList.java create mode 100644 LinkedListTest.java create mode 100644 List.java create mode 100644 ReturnObject.java create mode 100644 ReturnObjectImpl.java create mode 100644 ReturnObjectImplTest.java create mode 100644 SampleableList.java create mode 100644 SampleableListImpl.java create mode 100644 SampleableListTest.java create mode 100644 Stack.java create mode 100644 StackImpl.java create mode 100644 StackImplTest.java diff --git a/AbstractStack.java b/AbstractStack.java new file mode 100644 index 0000000..05e615e --- /dev/null +++ b/AbstractStack.java @@ -0,0 +1,36 @@ +/** + * An implementation of a stack that uses a {@List} as the underlying + * data structure. + * + * Not all operations on a stack will always be successful. For + * example, a programmer may try to pop an element from an empty + * stack. Since we hace not covered exceptions yet, we need another + * mechanism to report errors. In order to do that, methods of this + * list will return a {@see ReturnObject} that will contain either an + * object or an error value of the right kind (as defined in {@see + * ErrorMessage}). + * + * @author PiJ + */ +public abstract class AbstractStack implements Stack { + /** + * The data structure where the elements on the stack are stored. + */ + protected List internalList; + + /** + * Creates a new abstract stack using the provided list as the + * underlying data structure. + * + * Note: This constructor does not check whether the provided list + * is null. Programmers must do their own checks. If a null list + * is provided, a NullPointerException will be thrown at runtime + * as soon as any operation is attempted on the underlying list. + * + * @param list the list to be used + */ + public AbstractStack(List list) { + this.internalList = list; + } +} + diff --git a/ArrayList.java b/ArrayList.java new file mode 100644 index 0000000..46acdd2 --- /dev/null +++ b/ArrayList.java @@ -0,0 +1,70 @@ +public class ArrayList implements List { + protected Object[] data; + + public ArrayList() { + data = new Object[100]; + } + + public boolean isEmpty() { + if (data[0] == null) { + return true; + } else { + return false; + } + } + + public int size() { + int count = 0; + for (Object x : data) { + if (x != null) { + count++; + } + } + return count; + } + + public ReturnObject get(int index) { + if ((index < 0) || (index > size())) { + return new ReturnObjectImpl(null,ErrorMessage.INDEX_OUT_OF_BOUNDS); + } else { + return new ReturnObjectImpl(data[index],ErrorMessage.NO_ERROR); + } + } + + public ReturnObject remove(int index) { + Object removed = data[index]; + if ((index < 0) || (index > size())) { + return new ReturnObjectImpl(null, ErrorMessage.INDEX_OUT_OF_BOUNDS); + } + for (int i = index; i < size(); i++) { + if (data[i] != null) { + data[i] = data[i + 1]; + } + } + return new ReturnObjectImpl(removed,ErrorMessage.NO_ERROR); + } + + public ReturnObject add(int index, Object item) { + if (item == null) { + return new ReturnObjectImpl(null, ErrorMessage.INVALID_ARGUMENT); + } + if ((index < 0) || (index > size())) { + return new ReturnObjectImpl(null, ErrorMessage.INDEX_OUT_OF_BOUNDS); + } + for (int i = size(); i > index; i--) { + data[i] = data[i-1]; //dist from end + } + data[index] = item; + return new ReturnObjectImpl(null,ErrorMessage.NO_ERROR); + } + + public ReturnObject add(Object item) { + if (item == null) { + return new ReturnObjectImpl(null, ErrorMessage.INVALID_ARGUMENT); + } else { + data[size()] = item; + return new ReturnObjectImpl(null,ErrorMessage.NO_ERROR); + } + } + +} diff --git a/ArrayListTest.java b/ArrayListTest.java new file mode 100644 index 0000000..dd4bec1 --- /dev/null +++ b/ArrayListTest.java @@ -0,0 +1,31 @@ +public class ArrayListTest { + public static void main (String[] args) { + ArrayList arrayList1 = new ArrayList(); + arrayList1.add(2); + arrayList1.add(3); + System.out.println (arrayList1.size()); + arrayList1.add(4); + System.out.println(arrayList1.add(null).getError()); + ArrayList arrayList2 = new ArrayList(); + System.out.println (arrayList1.isEmpty()); + System.out.println (arrayList1.size()); +// System.out.println (arrayList1.get(1)); + System.out.println (arrayList2.isEmpty()); + System.out.println (arrayList2.size()); +// System.out.println (arrayList2.get(1)); + arrayList2.add(0,9); + System.out.println (arrayList2.isEmpty()); + System.out.println (arrayList2.size()); + arrayList1.add(1,7); + System.out.println (arrayList1.get(1).getReturnValue()); + arrayList1.remove(1); + System.out.println (arrayList1.get(1).getReturnValue()); + System.out.println(arrayList1.remove(6).getError()); + FunctionalArrayList farrayList1 = new FunctionalArrayList(); + farrayList1.add(2); + farrayList1.add(3); + System.out.println(farrayList1.head().getReturnValue()); + System.out.println(farrayList1.rest().head().getReturnValue()); + System.out.println(farrayList1.rest().rest().head().getReturnValue()); + } +} \ No newline at end of file diff --git a/ErrorMessage.java b/ErrorMessage.java new file mode 100644 index 0000000..c8490c4 --- /dev/null +++ b/ErrorMessage.java @@ -0,0 +1,25 @@ +public enum ErrorMessage { + /** + * No error. + */ + NO_ERROR, + + /** + * This error is produced e.g. when the programmer tries to + * retrieve an element from an empty data structure. + */ + EMPTY_STRUCTURE, + + /** + * This error is produced e.g. when the programmer tries to + * insert an element on a negative index of a list. + */ + + INDEX_OUT_OF_BOUNDS, + + /** + * This error is produced e.g. when the programmer tries to insert + * a null element in a list that does not allow null elements. + */ + INVALID_ARGUMENT; +} diff --git a/FunctionalArrayList.java b/FunctionalArrayList.java new file mode 100644 index 0000000..6ee04b6 --- /dev/null +++ b/FunctionalArrayList.java @@ -0,0 +1,22 @@ +public class FunctionalArrayList extends ArrayList implements FunctionalList { + + public ReturnObject head() { + if (isEmpty()) { + return new ReturnObjectImpl(null, ErrorMessage.EMPTY_STRUCTURE); + } + return new ReturnObjectImpl(data[0],ErrorMessage.NO_ERROR); + } + + public FunctionalList rest() { + if (isEmpty()) { + return new FunctionalArrayList(); + } + FunctionalArrayList dataCopy = new FunctionalArrayList(); + for (int i = 0; i < data.length; i++) { + dataCopy.add(data[i]); + } + dataCopy.remove(0); + return dataCopy; + } + +} diff --git a/FunctionalLinkedList.java b/FunctionalLinkedList.java new file mode 100644 index 0000000..3c9deaa --- /dev/null +++ b/FunctionalLinkedList.java @@ -0,0 +1,21 @@ +public class FunctionalLinkedList extends LinkedList implements FunctionalList { + + public ReturnObject head() { + if (isEmpty()) { + return new ReturnObjectImpl(null, ErrorMessage.EMPTY_STRUCTURE); + } + return new ReturnObjectImpl(element,ErrorMessage.NO_ERROR); + } + + public FunctionalList rest() { + if (isEmpty()) { + return new FunctionalLinkedList(); + } + FunctionalLinkedList dataCopy = new FunctionalLinkedList(); + for (int i = 1; i < size(); i++) { + dataCopy.add(get(i).getReturnValue()); + } + return dataCopy; + } + +} diff --git a/FunctionalLinkedListTest.java b/FunctionalLinkedListTest.java new file mode 100644 index 0000000..9d087b9 --- /dev/null +++ b/FunctionalLinkedListTest.java @@ -0,0 +1,11 @@ +public class FunctionalLinkedListTest { + + public static void main (String[] args) { + FunctionalLinkedList test = new FunctionalLinkedList(); + test.add(2); + test.add(3); + System.out.println(test.head().getReturnValue()); + System.out.println(test.rest().head().getReturnValue()); + System.out.println(test.rest().rest().head().getReturnValue()); + } +} \ No newline at end of file diff --git a/FunctionalList.java b/FunctionalList.java new file mode 100644 index 0000000..d2327c4 --- /dev/null +++ b/FunctionalList.java @@ -0,0 +1,35 @@ +/** + * A functional list is a list with a few additional methods that are + * common in functional languages (such as Lisp, Clojure, or Haskell) + * to work with lists in a recursive way. + * + * Not all operations on a recursive list will always be + * successful. For example, a programmer may try to extract the head + * from an empty list. Since we hace not covered exceptions yet, we + * need another mechanism to report errors. In order to do that, + * methods of this list will return a {@see ReturnObject} that will + * contain either an object or an error value of the right kind (as + * defined in {@see ErrorMessage}). + * + * @author PiJ + */ +public interface FunctionalList extends List { + /** + * Returns the element at the beginning of the list. + * + * If the list is empty, an appropriate error is returned. + * + * @return a copy of the element at the beginning of the list or + * an error if the list is empty. + */ + public ReturnObject head(); + + /** + * Returns a list with the elements in this list except the + * head. The elements must be in the same order. The original list + * must not change or be affected by changes in the new list. + * + * If the list is empty, another empty list is returned. + */ + public FunctionalList rest(); +} diff --git a/ImprovedStack.java b/ImprovedStack.java new file mode 100644 index 0000000..e4c7cc2 --- /dev/null +++ b/ImprovedStack.java @@ -0,0 +1,38 @@ +/** + * An implementation of a stack with additional methods. + * + * Classes implementing this interface must use a {@see List} as the + * underlying data structure to store the elements on the stack. + * + * Not all operations on a stack will always be successful. For + * example, a programmer may try to pop an element from an empty + * stack. Since we hace not covered exceptions yet, we need another + * mechanism to report errors. In order to do that, methods of this + * list will return a {@see ReturnObject} that will contain either an + * object or an error value of the right kind (as defined in {@see + * ErrorMessage}). + * + * @author PiJ + */ +public interface ImprovedStack extends Stack { + /** + * Returns a copy of this stack with the items reversed, the top + * elements on the original stack is at the bottom of the new + * stack and viceversa. + * + * @return a copy of this stack with the items reversed. + */ + public ImprovedStack reverse(); + + /** + * Removes the given object from the stack if it is + * there. Multiple instances of the object are all removed. + * + * Classes implementing this method must use method .equals() to + * check whether the item is in the stack or not. + * + * @param object the object to remove + */ + public void remove(Object object); +} + diff --git a/ImprovedStackImpl.java b/ImprovedStackImpl.java new file mode 100644 index 0000000..0e18973 --- /dev/null +++ b/ImprovedStackImpl.java @@ -0,0 +1,56 @@ +public class ImprovedStackImpl implements ImprovedStack { + protected List list; + + public ImprovedStackImpl(List list) { + this.list = list; + } + + public boolean isEmpty() { + return list.isEmpty(); + } + + public int size() { + return list.size(); + } + +//top of the list is the same as the first item in the list - LIFO + + public void push(Object item) { + list.add(0,item); + } + + public ReturnObject top() { + if (isEmpty()) { + return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE); + } + return list.get(0); + } + + public ReturnObject pop() { + if (isEmpty()) { + return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE); + } + return list.remove(0); + } + + public ImprovedStack reverse() { + if (isEmpty()) { + return this; + } + List listCopy = new ArrayList(); + for (int i = list.size() - 1; i >= 0; i--) { //iterating backwards through list + listCopy.add(list.get(i).getReturnValue()); //adding list's last element to the beginning of copy etc + } + return new ImprovedStackImpl(listCopy); + } + + public void remove(Object object) { + List listCopy = new ArrayList(); + for (int i = 0; i < list.size(); i++) { + if (!list.get(i).getReturnValue().equals(object)) { + listCopy.add(list.get(i).getReturnValue()); + } + } + list = listCopy; + } +} \ No newline at end of file diff --git a/ImprovedStackImplTest.java b/ImprovedStackImplTest.java new file mode 100644 index 0000000..30d648d --- /dev/null +++ b/ImprovedStackImplTest.java @@ -0,0 +1,27 @@ +public class ImprovedStackImplTest { + + public static void main (String[] args) { + ArrayList list = new ArrayList(); + ImprovedStack test = new ImprovedStackImpl(list); + System.out.println (test.isEmpty()); + System.out.println (test.size()); + test.push(null); + test.push(1); + test.push(2); + System.out.println (test.size()); + test.push(2); + test.push(3); + System.out.println (test.size()); + test = test.reverse(); + test.remove(2); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getError()); + System.out.println (test.top().getError()); + } +} \ No newline at end of file diff --git a/LinkedList.java b/LinkedList.java new file mode 100644 index 0000000..5314431 --- /dev/null +++ b/LinkedList.java @@ -0,0 +1,89 @@ +public class LinkedList implements List { + protected Object element; + protected LinkedList remainder; + + public boolean isEmpty() { + if (element == null) { + return true; + } + return false; + } + + public int size() { + if (isEmpty()) { + return 0; + } + if (remainder == null) { + return 1; + } + return 1 + remainder.size(); +//recurse through items in list increasing count for each item + } + + public ReturnObject get(int index) { + if ((size() - 1 < index) || (index < 0)) { + return new ReturnObjectImpl (null, ErrorMessage.INDEX_OUT_OF_BOUNDS); + } + if (index == 0) { + return new ReturnObjectImpl (element, ErrorMessage.NO_ERROR); + } + return remainder.get(index - 1); +//iterate through to index and return object + } + + public ReturnObject remove(int index) { + if ((size() - 1 < index) || (index < 0)) { + return new ReturnObjectImpl (null, ErrorMessage.INDEX_OUT_OF_BOUNDS); + } + // set this list equal to the remainder linked list, because it's the same as removing + // the element + if ((index == 0) && (remainder == null)) { + Object elementCopy = element; + element = null; + return new ReturnObjectImpl(elementCopy, ErrorMessage.NO_ERROR); + } + if ((index == 0) && (remainder != null)) { + Object elementCopy = element; + this.element = remainder.element; + this.remainder = remainder.remainder; + return new ReturnObjectImpl(elementCopy, ErrorMessage.NO_ERROR); + } + //the element of remainder is being removed, so remainder's remainder should move up + return remainder.remove(index - 1); + } + + public ReturnObject add(int index, Object item) { + if (item == null) { + return new ReturnObjectImpl(null, ErrorMessage.INVALID_ARGUMENT); + } + if ((index < 0) || (index > size())) { + return new ReturnObjectImpl(null, ErrorMessage.INDEX_OUT_OF_BOUNDS); + } + if (index == 0) { + LinkedList copy = new LinkedList(); + copy.element = element; + copy.remainder = remainder; + remainder = copy; + element = item; + return new ReturnObjectImpl(null, ErrorMessage.NO_ERROR); + } + if (remainder == null) { + remainder = new LinkedList(); + } + return remainder.add(index - 1,item); + } + + public ReturnObject add(Object item) { + if (item == null) { + return new ReturnObjectImpl(null, ErrorMessage.INVALID_ARGUMENT); + } + if (element == null) { + element = item; + return new ReturnObjectImpl(null, ErrorMessage.NO_ERROR); + } + if (remainder == null) { + remainder = new LinkedList(); + } + return remainder.add(item); + } +} \ No newline at end of file diff --git a/LinkedListTest.java b/LinkedListTest.java new file mode 100644 index 0000000..1065602 --- /dev/null +++ b/LinkedListTest.java @@ -0,0 +1,29 @@ +public class LinkedListTest { + + public static void main (String[] args) { + LinkedList test = new LinkedList(); + System.out.println(test.isEmpty()); + System.out.println(test.size()); + test.add(7); + System.out.println(test.size()); + test.add(6); + System.out.println(test.size()); + System.out.println(test.get(1).getReturnValue()); + System.out.println(test.get(2).getError()); + test.add(2,5); + System.out.println(test.size()); + System.out.println(test.get(1).getReturnValue()); + System.out.println(test.get(2).getReturnValue()); + System.out.println(test.remove(0).getReturnValue()); + System.out.println(test.get(1).getReturnValue()); + System.out.println(test.get(2).getReturnValue()); + System.out.println(test.remove(1).getReturnValue()); + System.out.println(test.get(0).getReturnValue()); + + test.add("Foo"); + test.add("Bar"); + System.out.println(test.remove(1).getReturnValue()); + System.out.println(test.get(0).getReturnValue()); + System.out.println(test.get(1).getError()); + } +} \ No newline at end of file diff --git a/List.java b/List.java new file mode 100644 index 0000000..1c5a4f9 --- /dev/null +++ b/List.java @@ -0,0 +1,95 @@ +/** + * A list is a collection of objects that are sorted and can be + * accessed by index. The first element in the list is at index 0. + * + * A list can store objects of any kind, and they can be of different + * types: Integers, Doubles, String, or even other lists. However, + * this list cannot store null objects. + * + * There is no limit to the number of elements in the list (provided + * that there is free memory in the Java Virtual Machine). + * + * Not all operations on a list will always be successful. For + * example, a programmer may try to remove an element from an empty + * list, or from a position where there is nothing. Since we hace not + * covered exceptions yet, we need another mechanism to report + * errors. In order to do that, methods of this list will return a + * {@see ReturnObject} that will contain either an object or an error + * value of the right kind (as defined in {@see ErrorMessage}). + * + * @author PiJ + */ +public interface List { + /** + * Returns true if the list is empty, false otherwise. + * + * @return true if the list is empty, false otherwise. + */ + public boolean isEmpty(); + + /** + * Returns the number of items currently in the list. + * + * @return the number of items currently in the list + */ + public int size(); + + /** + * Returns the element at the given position. + * + * If the index is negative or greater or equal than the size of + * the list, then an appropriate error must be returned. + * + * @param index the position in the list of the item to be retrieved + * @return the element or an appropriate error message, + * encapsulated in a ReturnObject + */ + public ReturnObject get(int index); + + /** + * Returns the elements at the given position and removes it + * from the list. The indeces of elements after the removed + * element must be updated accordignly. + * + * If the index is negative or greater or equal than the size of + * the list, then an appropriate error must be returned. + * + * @param index the position in the list of the item to be retrieved + * @return the element or an appropriate error message, + * encapsulated in a ReturnObject + */ + public ReturnObject remove(int index); + + /** + * Adds an element to the list, inserting it at the given + * position. The indeces of elements at and after that position + * must be updated accordignly. + * + * If the index is negative or greater or equal than the size of + * the list, then an appropriate error must be returned. + * + * If a null object is provided to insert in the list, the + * request must be ignored and an appropriate error must be + * returned. + * + * @param index the position at which the item should be inserted in + * the list + * @param item the value to insert into the list + * @return an ReturnObject, empty if the operation is successful + * or containing an appropriate error message otherwise + */ + public ReturnObject add(int index, Object item); + + /** + * Adds an element at the end of the list. + * + * If a null object is provided to insert in the list, the + * request must be ignored and an appropriate error must be + * returned. + * + * @param item the value to insert into the list + * @return an ReturnObject, empty if the operation is successful + * or containing an appropriate error message otherwise + */ + public ReturnObject add(Object item); +} diff --git a/ReturnObject.java b/ReturnObject.java new file mode 100644 index 0000000..c7c7a35 --- /dev/null +++ b/ReturnObject.java @@ -0,0 +1,37 @@ +/** + * A wrapper containing either an object (the result of an operation + * on a data structure) or an error value. + * + * @author PiJ + */ +public interface ReturnObject { + /** + * Returns whether there has been an error + * @return whether there has been an error + */ + public boolean hasError(); + + /** + * Returns the error message. + * + * This method must return NO_ERROR if and only if + * {@hasError} returns false. + * + * @return the error message + */ + public ErrorMessage getError(); + + /** + * Returns the object wrapped in this ReturnObject, i.e. the + * result of the operation if it was successful, or null if + * there has been an error. + * + * Note that the output of this method must be null if {@see + * hasError} returns true, but the opposite is not true: if + * {@see hasError} returns false, this method may or may not + * return null. + * + * @return the return value from the method or null if there has been an error + */ + public Object getReturnValue(); +} diff --git a/ReturnObjectImpl.java b/ReturnObjectImpl.java new file mode 100644 index 0000000..a9fe9d5 --- /dev/null +++ b/ReturnObjectImpl.java @@ -0,0 +1,33 @@ +public class ReturnObjectImpl implements ReturnObject { + + private Object returnValue; + private ErrorMessage error; + + public ReturnObjectImpl(Object returnValue, ErrorMessage error) { + this.returnValue = returnValue; + this.error = error; //note if error = null, treat it as an actual error + //in this case getError returns null + } + + public boolean hasError() { + if (error == ErrorMessage.NO_ERROR) { + return false; + } else { + return true; + } + } + + public ErrorMessage getError() { + return error; + } + + public Object getReturnValue() { + if (hasError()) { + return null; + } else { + return returnValue; + } + + } + +} \ No newline at end of file diff --git a/ReturnObjectImplTest.java b/ReturnObjectImplTest.java new file mode 100644 index 0000000..5f281b6 --- /dev/null +++ b/ReturnObjectImplTest.java @@ -0,0 +1,20 @@ +public class ReturnObjectImplTest { + public static void main (String[] args) { + ReturnObjectImpl returnObject1 = new ReturnObjectImpl(5,ErrorMessage.NO_ERROR); + ReturnObjectImpl returnObject2 = new ReturnObjectImpl(5,null); + ReturnObjectImpl returnObject3 = new ReturnObjectImpl(5,ErrorMessage.EMPTY_STRUCTURE); + ReturnObjectImpl returnObject4 = new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE); + System.out.println (returnObject1.hasError()); + System.out.println (returnObject1.getError()); + System.out.println (returnObject1.getReturnValue()); + System.out.println (returnObject2.hasError()); + System.out.println (returnObject2.getError()); + System.out.println (returnObject2.getReturnValue()); + System.out.println (returnObject3.hasError()); + System.out.println (returnObject3.getError()); + System.out.println (returnObject3.getReturnValue()); + System.out.println (returnObject4.hasError()); + System.out.println (returnObject4.getError()); + System.out.println (returnObject4.getReturnValue()); + } +} \ No newline at end of file diff --git a/SampleableList.java b/SampleableList.java new file mode 100644 index 0000000..d003299 --- /dev/null +++ b/SampleableList.java @@ -0,0 +1,14 @@ +/** + * A sampleable list can be sampled. + * + * @author PiJ + */ +public interface SampleableList extends List { + /** + * Returns a list containing the first, third, fifth... + * items of this list, or an empty list if the list is empty. + * + * @return a list containing the first, third, fifth... items of this list + */ + public SampleableList sample(); +} diff --git a/SampleableListImpl.java b/SampleableListImpl.java new file mode 100644 index 0000000..8a6e8a3 --- /dev/null +++ b/SampleableListImpl.java @@ -0,0 +1,12 @@ +public class SampleableListImpl extends ArrayList implements SampleableList { + public SampleableList sample() { + //create blank sampleablelist copy + SampleableListImpl selectionCopy = new SampleableListImpl(); + //use get to get each of the odd indexed elements of the list and add(Object) them to copy + for (int i = 0; i < size(); i += 2) { + selectionCopy.add(get(i).getReturnValue()); + } + //return copy + return selectionCopy; + } +} \ No newline at end of file diff --git a/SampleableListTest.java b/SampleableListTest.java new file mode 100644 index 0000000..92dabb3 --- /dev/null +++ b/SampleableListTest.java @@ -0,0 +1,13 @@ +public class SampleableListTest { + public static void main (String[] args) { + SampleableList sList = new SampleableListImpl(); + sList.add(3); + sList.add(4); + sList.add(5); + sList = sList.sample(); + System.out.println (sList.size()); + System.out.println (sList.get(0).getReturnValue()); + System.out.println (sList.get(1).getReturnValue()); + System.out.println (sList.get(2).getReturnValue()); + } +} \ No newline at end of file diff --git a/Stack.java b/Stack.java new file mode 100644 index 0000000..f618a85 --- /dev/null +++ b/Stack.java @@ -0,0 +1,58 @@ +/** + * An implementation of a stack, a data structure of type LIFO (Last + * In, First Out). + * + * Classes implementing this interface must use a {@see List} as the + * underlying data structure to store the elements on the stack. + * + * Not all operations on a stack will always be successful. For + * example, a programmer may try to pop an element from an empty + * stack. Since we hace not covered exceptions yet, we need another + * mechanism to report errors. In order to do that, methods of this + * list will return a {@see ReturnObject} that will contain either an + * object or an error value of the right kind (as defined in {@see + * ErrorMessage}). + * + * @author PiJ + */ +public interface Stack { + /** + * Returns true if the stack is empty, false otherwise. + * + * @return true if the stack is empty, false otherwise. + */ + public boolean isEmpty(); + + /** + * Returns the number of items currently in the stack. + * + * @return the number of items currently in the stack + */ + public int size(); + + /** + * Adds an element at the top of the stack. + * + * @param item the new item to be added + */ + public void push(Object item); + + /** + * Returns the element at the top of the stack. The stack is + * left unchanged. + * + * @return If stack is not empty, the item on the top is returned. If the + * stack is empty, an appropriate error. + */ + public ReturnObject top(); + + /** + * Returns the element at the top of the stack. The element is + * removed frmo the stack. + * + * @return If stack is not empty, the item on the top is returned. If the + * stack is empty, an appropriate error. + */ + public ReturnObject pop(); +} + diff --git a/StackImpl.java b/StackImpl.java new file mode 100644 index 0000000..4cd2c68 --- /dev/null +++ b/StackImpl.java @@ -0,0 +1,35 @@ +public class StackImpl extends AbstractStack { + + public StackImpl(List list) { + super (list); + } + + public boolean isEmpty() { + return internalList.isEmpty(); + } + + public int size() { + return internalList.size(); + } + +//top of the list is the same as the first item in the list - LIFO + + public void push(Object item) { + internalList.add(0,item); + } + + public ReturnObject top() { + if (isEmpty()) { + return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE); + } + return(internalList.get(0)); + } + + public ReturnObject pop() { + if (isEmpty()) { + return new ReturnObjectImpl(null,ErrorMessage.EMPTY_STRUCTURE); + } + return(internalList.remove(0)); + } + +} \ No newline at end of file diff --git a/StackImplTest.java b/StackImplTest.java new file mode 100644 index 0000000..d1f3a5a --- /dev/null +++ b/StackImplTest.java @@ -0,0 +1,24 @@ +public class StackImplTest { + + public static void main (String[] args) { + ArrayList list = new ArrayList(); + StackImpl test = new StackImpl(list); + System.out.println (test.isEmpty()); + System.out.println (test.size()); + test.push(null); + test.push(1); + System.out.println (test.size()); + test.push(2); + test.push(3); + System.out.println (test.size()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getReturnValue()); + System.out.println (test.top().getReturnValue()); + System.out.println (test.pop().getError()); + System.out.println (test.top().getError()); + } +} \ No newline at end of file