Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7058215
Implemented Method Headers/
Shyesta Feb 11, 2024
46506d4
Completed isEmpty() and size() methods
Shyesta Feb 12, 2024
de40504
Completed addFront() method, ArrayList constructor, added fields
Shyesta Feb 12, 2024
eeb8794
Added resize() method
Shyesta Feb 12, 2024
a0767b8
Completed addBack() and add() methods
Shyesta Feb 12, 2024
38556b6
Completed get() and set() methods.
Shyesta Feb 12, 2024
1d13e6a
Completed removeFront() and removeBack() methods.
Shyesta Feb 13, 2024
05febbb
Completed both remove() methods.
Shyesta Feb 13, 2024
0ef7d8c
Completed iterator() and contains() methods.
Shyesta Feb 13, 2024
637f36b
Added method headers, fields, and Node class.
Shyesta Feb 13, 2024
db4c0d9
Completed all add methods.
Shyesta Feb 13, 2024
6a357bd
Completed get() and set() methods.
Shyesta Feb 13, 2024
522fec4
Completed size() and isEmpty() methods.
Shyesta Feb 13, 2024
87f55de
Completed LinkedList removeFront() and removeBack() methods.
Shyesta Feb 13, 2024
c70fa58
Completed LinkedList remove() and contains() methods
Shyesta Feb 13, 2024
8e8d311
Added javadocs with Runtime analysis to LinkedList class
Shyesta Feb 13, 2024
521d2b8
Added more javadocs with Runtime analysis to LinkedList class
Shyesta Feb 13, 2024
acd7107
Updated .gitignore
Shyesta Feb 14, 2024
94e5453
Finished Runtime analysis
Shyesta Feb 14, 2024
7835ae8
Updated misc.xml
Shyesta Feb 14, 2024
1ed7239
Wrote unit tests for LinkedList class
Shyesta Feb 15, 2024
5ffbfdc
Added runtime analysis for ArrayList class.
Shyesta Feb 16, 2024
022d5cb
Created ArrayList test method headers.
Shyesta Feb 16, 2024
d51f980
Completed unit tests for ArrayList class.
Shyesta Feb 17, 2024
8df0ccf
Created method headers for Bag interface.
Shyesta Feb 18, 2024
61cfed4
Created StackTestClient.
Shyesta Feb 18, 2024
475aebe
Added methods for ResizingArrayStack and tested StackTestClient.
Shyesta Feb 18, 2024
35b0351
Completed LinkedStack methods.
Shyesta Feb 19, 2024
99722d7
Updated to test LinkedStack implementation.
Shyesta Feb 19, 2024
a3802e5
Updated to test LinkedStack implementation.
Shyesta Feb 19, 2024
e8e8f68
Removed ListIterator import statement
Shyesta Feb 19, 2024
6503d02
Completed LinkedQueue methods.
Shyesta Feb 19, 2024
0dcbdaa
Created and tested LinkedBag through Stats class.
Shyesta Feb 19, 2024
79d1426
Fixed warnings in Stats class.
Shyesta Feb 19, 2024
767577b
Added Runtime analysis for all methods.
Shyesta Feb 19, 2024
6c1d363
Fixed warnings in all methods.
Shyesta Feb 19, 2024
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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
out/
!**/src/main/**/out/
!**/src/test/**/out/
/.idea/vcs.xml
/.idea/misc.xml

### Eclipse ###
.apt_generated
Expand All @@ -26,4 +28,5 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions SDEV333-Term-Project.iml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library" scope="TEST">
<library name="JUnit5.8.1">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
305 changes: 305 additions & 0 deletions src/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,305 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayList<E> implements List<E>{

// fields
private int size;
private E[] buffer;

public ArrayList()
{
//initialize my fields
size = 0;
buffer = (E[]) new Object[10];
}

/**
* Runtime analysis: Worst case scenario, this will run at O(2n) time.
* This is because if we have a full buffer, we will need to resize.
* To resize, we go through every element and copy to a new buffer.
* We then have to addFront() by moving all elements over right one,
* and then add the item to the front.
* @param item the item to be added
*/
@java.lang.Override
public void addFront(E item) {
if(size == buffer.length)
{
resize(size * 2);
}

for(int i = size; i >= 1; i--)
{
buffer[i] = buffer[i-1];
}
buffer[0] = item;
size++;
}

/**
* Runtime analysis: Worst case scenario, this will run at O(n). This is because
* if our buffer is full, we will have to resize. Resizing means visiting all
* elements in our original buffer and copying them over to a new one.
* Then we will add our new item to the end of our buffer. Best case scenario,
* this runs in constant time since we can just add an item to the back
* and increment size.
* @param item the item to be added
*/
@java.lang.Override
public void addBack(E item) {
if(size == buffer.length)
{
resize(size * 2);
}

buffer[size] = item;
size++;
}

/**
* Worst case scenario, this would run at O(2n) time. This is because
* if our buffer is full, we would need to resize, meaning we have to
* loop through the entire buffer to add all items to a new, larger buffer.
* After that, the index could be the first index meaning we have to shift
* every element over to the right one index.
* @param i the index where the item should be added
* @param item the item to be added
*/
@java.lang.Override
public void add(int i, E item) {
if(i > size || i < 0)
{
throw new IndexOutOfBoundsException("Invalid index");
}
if(size == buffer.length)
{
resize(size * 2);
}
if (i != size) {
for (int j = size; j > i; j--) {
buffer[j] = buffer[j - 1];
}
}
buffer[i] = item;
size++;
}

/**
* In every scenario, this would run in constant time. Since we are just
* returning an element located at in index, this is a single line return
* statement. There is no need to do a loop, traversal, or shifting of elements.
* @param i the index where the item should be retrieved
* @return
*/
@java.lang.Override
public E get(int i) {
if(i > size)
{
throw new IndexOutOfBoundsException();
}
return buffer[i];
}

/**
* In every scenario, this would run in constant time. Since we are just
* changing a single elements value at an index, this is a single line,
* with no traversal, loop, or shifting of the buffer required.
* @param i the index where the item should be saved
* @param item the item to be saved
*/
@java.lang.Override
public void set(int i, E item) {
if(i > size)
{
throw new IndexOutOfBoundsException("Invalid index given!");
}
buffer[i] = item;
}

/**
* In every scenario, this would run at O(n). This is because since we are
* removing the front element, we have to shift every element over to the
* left one. This requires a loop to visit every element in the buffer.
* @return
*/
@java.lang.Override
public E removeFront() {
if(!isEmpty())
{
E returnedItem = buffer[0];
for (int i = 0; i <= size - 2; i++) {
buffer[i] = buffer[i + 1];
}
buffer[size - 1] = null;
size--;
return returnedItem;
}
throw new NoSuchElementException("There are no elements to remove!");
}

/**
* Best and worst case scenario, if not accounting for the exception thrown,
* this would run in constant time. This is because we have no need to
* shift elements over since we are removing the final index.
* We also do not need to loop through any elements since we can
* remove the item from the index of size-1.
* @return
*/
@java.lang.Override
public E removeBack() {
if(!isEmpty())
{
E returnedItem = buffer[size-1];
buffer[size-1] = null;
size--;
return returnedItem;
}
throw new NoSuchElementException("There are no elements to remove!");
}

/**
* Worst case scenario, this would be O(2n) runtime. This is because
* the worst case would be either that the item is at the front
* or at the back. If the item is at the front, we would find
* the item immediately, but then would need to shift every item
* over to the left. If the item is at the back, we still have to
* search through the entire buffer until we find it at the final
* index.
* @param item the item to be removed
*/
@java.lang.Override
public void remove(E item) {
if(!contains(item))
{
throw new NoSuchElementException("This element does not exist!");
}
for (int i = 0; i < size; i++) {
if(buffer[i].equals(item))
{
for (int j = i; j < size-2; j++) {
buffer[j] = buffer[j+1];
}
//remove the final index value
buffer[size - 1] = null;
//decrement size
size--;
}
}
}

/**
* Worst case scenario, this method will run at O(n). This is because the
* index it could be searching for could be the first index in the buffer.
* meaning it would have to reorder every index over to the left.
* Best case scenario, this would run at constant time, as
* it could be the very last index given, meaning it would have to do
* no reordering, or the index could not be valid, meaning an exception would
* be thrown.
* @param i the index where the item should be removed
* @return
*/
@java.lang.Override
public E remove(int i) {
//check to see if index is valid
if(i >= size || i < 0)
{
throw new IndexOutOfBoundsException("Invalid index");
}
// save a copy of the value to be returned later
E removedItem = buffer[i];

//shift values to the left
for (int j = i; j < size-2; j++) {
buffer[j] = buffer[j+1];
}
//remove the final index value
buffer[size - 1] = null;
//decrement size
size--;
//return the removed value
return removedItem;
}

/**
* Worst case scenario, this will run at linear time or O(n). This is because
* the contains method could loop to the last element in the buffer and find
* the item, or it could loop to the end of the buffer and not find the item.
* Either way, it could potentially have to search through every index.
* @param item the item to search for
* @return
*/
@java.lang.Override
public boolean contains(E item) {
for (int i = 0; i < size; i++) {
if(buffer[i].equals(item))
{
return true;
}
}
return false;
}

/**
* This will always run in constant time since we are just returning
* a true or false based on if size is equal to 0.
* @return
*/
@java.lang.Override
public boolean isEmpty() {
return size == 0;
}

/**
* Runtime analysis: This will always run in constant time since we are just
* returning the variable size.
* @return
*/
@java.lang.Override
public int size() {
return size;
}

/**
* In every scenario, this would run at O(n). This is because we have to
* create a new buffer, and go through the old buffer to copy all the
* elements over to the new one.
* @param newSize
*/
private void resize(int newSize)
{
E[] newBuffer = (E[])new Object[newSize];

for (int i = 0; i < size; i++) {
newBuffer[i] = buffer[i];
}
//set the new space into the buffer
buffer = newBuffer;

//the old space is no longer "pointed to" and will eventually
//be cleaned up by the garbage collector
}

@java.lang.Override
public Iterator<E> iterator() {
return new Iterator<E>() {
// fields
private int i;
@Override
public boolean hasNext() {
return i < size;
}

@Override
public E next() {
if(i >= size)
{
throw new NoSuchElementException("i is out of bounds");
}
E currentValue = buffer[i];
i++;
return currentValue;
}
};
}
}
22 changes: 22 additions & 0 deletions src/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Bag API
*/
public interface Bag<E> extends Iterable<E> {
/**
* Add an item to the bag.
* @param item the item to be added
*/
void add(E item);

/**
* Checks to see if the bag is emtpy
* @return true if the bag is empty, false otherwise.
*/
boolean isEmpty();

/**
* Returns a count of the number of items in the bag.
* @return the number of items in the bag
*/
int size();
}
Loading