Skip to content
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

Term Project: Part 1 and Part 2 #4

Open
wants to merge 5 commits into
base: main
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
6 changes: 6 additions & 0 deletions .idea/vcs.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$/test" 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://$MODULE_DIR$/lib/junit-jupiter-5.8.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-api-5.8.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/opentest4j-1.2.0.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-platform-commons-1.8.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/apiguardian-api-1.1.2.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-params-5.8.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-jupiter-engine-5.8.1.jar!/" />
<root url="jar://$MODULE_DIR$/lib/junit-platform-engine-1.8.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
297 changes: 297 additions & 0 deletions src/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
import java.util.Iterator;
import java.util.NoSuchElementException;

public class ArrayList<E> implements List<E> {
private int size;
private E[] buffer;

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

/**
* Add item to the front.
*
* @param item the item to be added
*/
@Override
public void addFront(E item) {
// If the buffer is already full, we increase the size by 2 times
if (buffer.length == size) {
increaseSize(size * 2);
}

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

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

/**
* Add item to the back.
*
* @param item the item to be added
*/
@Override
public void addBack(E item) {
// If the buffer is already full, we increase the size by 2 times
if (buffer.length == size) {
increaseSize(size * 2);
}

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

/**
* Add an item at specified index (position).
*
* @param index the index where the item should be added
* @param item the item to be added
*/
@Override
public void add(int index, E item) {
// If the buffer is already full, we increase the size by 2 times
if (buffer.length == size) {
increaseSize(size * 2);
}

size++;

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

buffer[index] = item;
}

/**
* Get the item at a specified index.
*
* @param i the index where the item should be retrieved
* @return the item located at that index
*/
@Override
public E get(int i) {
// If the i is greater than the current size or less than 0 than we know that it's not possible
if (i > size || i < 0) {
return null;
}
else {
return buffer[i];
}
}

/**
* Set (save) an item at a specified index. Previous
* item at that index is overwritten.
*
* @param i the index where the item should be saved
* @param item the item to be saved
*/
@Override
public void set(int i, E item) {
// If i is greater than 0 but equal or less than the size, we will know it's within size
if (i >= 0 && i <= size) {
buffer[i] = item;
}
}

/**
* Remove item at the front of the list.
*
* @return the item that was removed
*/
@Override
public E removeFront() {
// If the size is 0, that means that it's empty so there's no need
if (size == 0) {
return null;
}

E removed = buffer[0];

buffer[0] = null;

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

buffer[size] = null; // Changes the back to null since we would move everything to the left so the index should be nothing now
size--;

return removed;
}

/**
* Remove item at the back of the list
*
* @return the item that was removed
*/
@Override
public E removeBack() {
// If the size is 0, that means that it's empty so there's no need
if (size == 0) {
return null;
}

E removed = buffer[size];
buffer[size] = null;
size--;
return removed;
}

/**
* Remove item from the list
*
* @param item the item to be removed
*/
@Override
public void remove(E item) {
// If the size is not 0, run it
if (size != 0) {
// for each item in the array
for (int i = 0; i < size; i++) {
// if the item is the same remove it
if (buffer[i] == item) {
buffer[i] = null;

// for each of the remaining index we will shift it to the left
for (int j = i; j < size; j++) {
buffer[i] = buffer[i + 1];
}

// Lower size by 1
size--;
}
}
}
}

/**
* Remove item at a specified index.
*
* @param index the index where the item should be removed
* @return the item that was removed
*/
@Override
public E remove(int index) {
// If the size is 0, that means that it's empty so there's no need
if (size == 0) {
return null;
}
else {
E removedInt = buffer[index];

for (int i = index; i <= size; i++) {
buffer[i] = buffer[i + 1];
}

size--;
return removedInt;
}
}

/**
* Checks if an item is in the list.
*
* @param item the item to search for
* @return true if the item is in the list, false otherwise
*/
@Override
public boolean contains(E item) {
boolean doseItContains = false;

for (int i = 0; i <= size; i++) {
if (buffer[i] == item) {
doseItContains = true;
}
}

return doseItContains;
}

/**
* Checks if the list is empty.
*
* @return true if the list is empty, false otherwise
*/
@Override
public boolean isEmpty() {
if (size == 0) {
return true;
}
else {
return false;
}
}

/**
* Provides a count of the number of items in the list.
*
* @return number of items in the list
*/
@Override
public int size() {
return size;
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
@Override
public Iterator<E> iterator() {
return new ArrayListIterator();
}

private class ArrayListIterator implements Iterator<E> {
//private fields
private int i;

private ArrayListIterator() {
i = 0;
}

/**
* Returns true if the iteration has more elements.
*
* @return true if the iteration has more elements
*/
@Override
public boolean hasNext() {
return i < size;
}

/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
@Override
public E next() {
if (i >= size) {
throw new NoSuchElementException("i is now out of bounds");
}

E currentValue = buffer[i];
i++;

return currentValue;
}
}

// resizing
private void increaseSize(int size) {
E[] placeholder = (E[]) new Object[size];
System.arraycopy(buffer, 0, placeholder, 0, buffer.length);
buffer = placeholder;
}
}
23 changes: 23 additions & 0 deletions src/Bag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SDEV333 Best class :D
* Ming Li
*/
public interface Bag<E> extends Iterable<E> {
/**
* Add an item
* @param item the item to be added
*/
void add(E item);

/**
* Checks to see if the bad is empty
* @return true of the stack 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