-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
831 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Oops, something went wrong.