Skip to content
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
1 change: 0 additions & 1 deletion .idea/misc.xml

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

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.

39 changes: 39 additions & 0 deletions SDEV333-Term-Project-main-2 2/SDEV333-Term-Project.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/tests/test" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/src/tests" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<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>
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>
221 changes: 221 additions & 0 deletions SDEV333-Term-Project-main-2 2/src/ArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
import java.util.Iterator;
import java.util.NoSuchElementException;



public class ArrayList <E> implements List<E>{
private static final int BUFFER_CAPACITY=10;//
private Object[] elements;//array to store elements
private int size;// current size of the list

//constructor to initialize arraylist
public ArrayList(){
elements = new Object[BUFFER_CAPACITY];//initialize the array with default capacity
size=0;// setting size to 0
}
/**
* Add item to the front.
*
* @param item the item to be added
*/

//o(n)
//because it involves shifting all elements to right and has to make space for new item
@Override
public void addFront(E item) {
add(0, item);// add to insert at index 0
}

/**
* Add item to the back.
*
* @param item the item to be added
*/

//o(1)-adding an elements to the end of array
@Override
public void addBack(E item) {
if(size== elements.length){// check if the array is full
throw new IllegalArgumentException("arraylist is full");

}
elements[size++]=item;// adding the item at the end of list
}

/**
* Add an item at specified index (position).
*
* @param index the index where the item should be added
* @param item the item to be added
*/

//o(n)worst case -have to shift the elements to right by one position
@Override
public void add(int index, E item) {
if(index<0||index>size){
throw new IndexOutOfBoundsException("index out of range");

}
if(size==elements.length){
throw new IllegalArgumentException("array full");

}
//shifting elements to the right to make space for the new item
for (int i =size; i >index; i--){
elements[i]=elements[i-1];
}
elements[index]=item;// adding item at an index
size++;//
}

/**
* Get the item at a specified index.
*
* @param index the index where the item should be retrieved
* @return the item located at that index
*/
//O(1) this directly accesses the elements at the specified index
@Override
public E get(int index) {
if(index<0||index>=size){
throw new IndexOutOfBoundsException("index out of range");
}
return (E) elements[index];
}

/**
* Set (save) an item at a specified index. Previous
* item at that index is overwritten.
*
* @param index the index where the item should be saved
* @param item the item to be saved
*/
@Override
public void set(int index, E item) {
if(index<0||index>=size){
throw new IndexOutOfBoundsException("out of range");

}
elements[index]=item;// set the item at specified index
}

/**
* Remove item at the front of the list.
*
* @return the item that was removed
*/
@Override
public E removeFront() {
return remove(0);// to remove method to remove from index 0
}

/**
* Remove item at the back of the list
*
* @return the item that was removed
*/
@Override
public E removeBack() {
return remove(size-1);//remove form last index
}

/**
* Remove item from the list
*
* @param item the item to be removed
*/
@Override
public void remove(E item) {
for(int i=0; i <size; i++){
if(elements[i].equals(item)){
remove(i);//remove the item at index i
return;
}
}
}

/**
* 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(index<0||index>=size){
throw new IndexOutOfBoundsException("out of range");

}
E removedItem =(E) elements[index];// store the item to remove
for (int i = index; i<size-1; i++){
elements[i]=elements[i+1];
}
elements[--size]=null;
return removedItem;// return the remove item
}

/**
* 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
*/
//o(n) worst case cause it needs to traverse the entire array to check if item is present
@Override
public boolean contains(E item) {
for(int i=0;i<size; i++){
if(elements[i].equals(item)){
return true;// item found then true
}
}
return false;// else false
}

/**
* Checks if the list is empty.
*
* @return true if the list is empty, false otherwise
*/
//o(1) ebcuase it just checks the size of the array to know if it is empty
@Override
public boolean isEmpty() {
return size==0;// return true if size is 0 meaning list empty
}

/**
* Provides a count of the number of items in the list.
*
* @return number of items in the list
*/
//O(1)directly returns the size of array
@Override
public int size() {
return size;// returning the current size of the list
}

/**
* Returns an iterator over elements of type {@code T}.
*
* @return an Iterator.
*/
//
@Override
public Iterator<E> iterator() {
return new Iterator<E>() {
private int currentIndex=0;//
@Override
public boolean hasNext(){
return currentIndex<size;//check if there are more elemetns
}

@Override
public E next() {
if (!hasNext()){
throw new NoSuchElementException();// if no more elements left

}
return (E) elements[currentIndex++];//
}
};
}
}
File renamed without changes.
Loading