diff --git a/.DS_Store b/.DS_Store
index 5fb0757..7621eb9 100644
Binary files a/.DS_Store and b/.DS_Store differ
diff --git a/hw/.DS_Store b/hw/.DS_Store
new file mode 100644
index 0000000..370f22d
Binary files /dev/null and b/hw/.DS_Store differ
diff --git a/hw/hw-02/.DS_Store b/hw/hw-02/.DS_Store
new file mode 100644
index 0000000..92f2c59
Binary files /dev/null and b/hw/hw-02/.DS_Store differ
diff --git a/hw/hw-02/HW02_65b21a17a4544/CDLinkedList.java b/hw/hw-02/HW02_65b21a17a4544/CDLinkedList.java
new file mode 100644
index 0000000..10b168c
--- /dev/null
+++ b/hw/hw-02/HW02_65b21a17a4544/CDLinkedList.java
@@ -0,0 +1,164 @@
+public class CDLinkedList {
+ DListNode header;
+ int size;
+ static final int HEADERVALUE = -9999999;
+
+ public CDLinkedList() {
+ header = new DListNode(HEADERVALUE);
+ makeEmpty();// necessary, otherwise next/previous node will be null
+ }
+
+ public boolean isEmpty() {
+ return header.nextNode == header;
+ }
+
+ public boolean isFull() {
+ return false;
+ }
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = header;
+ header.previousNode = header;
+ size =0;
+ }
+
+ // put in new data after the position of p.
+ public void insert(int value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof DListIterator))
+ throw new Exception();
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode);
+ DListNode n = new DListNode(value, p3.currentNode, p2.currentNode);
+ p2.currentNode.nextNode = n;
+ p3.currentNode.previousNode = n;
+ size++;
+ }
+
+ // return position number of value found in the list.
+ // otherwise, return -1.
+ public int find(int value) throws Exception {
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ return -1;
+ if (v == value)
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ // return data stored at kth position.
+ public int findKth(int kthPosition) throws Exception {
+ if (kthPosition < 0 || kthPosition > size-1)
+ throw new Exception();// exit the method if the position is
+ // beyond the first/last possible
+ // position, throwing exception in the process.
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ throw new Exception();
+ if (index == kthPosition)
+ return v;
+ }
+ throw new Exception();
+ }
+
+ // Return iterator at position before the first position that stores value.
+ // If the value is not found, return null.
+ public Iterator findPrevious(int value) throws Exception {
+ if (isEmpty())
+ return null;
+ Iterator itr1 = new DListIterator(header);
+ Iterator itr2 = new DListIterator(header);
+ int currentData = itr2.next();
+ while (currentData != value) {
+ currentData = itr2.next();
+ itr1.next();
+ if (((DListIterator) itr2).currentNode == header)
+ return null;
+ }
+ return itr1;
+ }
+
+ // remove content at position just after the given iterator. Skip header if
+ // found.
+ public void remove(Iterator p) {
+ if (isEmpty())
+ return;
+ if (p == null || !(p instanceof DListIterator))
+ return;
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ return;
+ if (p2.currentNode.nextNode == header)
+ p2.currentNode = header;
+ if (p2.currentNode.nextNode == null)
+ return;
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode);
+ p2.currentNode.nextNode = p3.currentNode;
+ p3.currentNode.previousNode = p2.currentNode;
+ size--;
+ }
+
+ // remove the first instance of the given data.
+ public void remove(int value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // remove data at position p.
+ // if p points to header or the list is empty, do nothing.
+ public void removeAt(Iterator p) throws Exception {
+ if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null
+ || ((DListIterator) p).currentNode == header)
+ return;
+
+ DListIterator p2 = (DListIterator) (findPrevious(p));
+ remove(p2);
+
+ }
+
+ // Print each contact out, one by one.
+ // To be completed by students.
+ public void printList() throws Exception {
+ Iterator itr = new DListIterator(header);
+ while (itr.hasNext()) {
+ Object data = itr.next();
+
+ System.out.println(data);
+
+ }
+ }
+
+ public int size() throws Exception {
+ return size;
+ }
+
+ // return iterator pointing to location before position.
+ public Iterator findPrevious(Iterator position) throws Exception {
+ if (position == null)
+ return null;
+ if (!(position instanceof DListIterator))
+ return null;
+ if (((DListIterator) position).currentNode == null)
+ return null;
+
+ DListIterator p = ((DListIterator) position);
+ DListIterator p2 = new DListIterator(p.currentNode.previousNode);
+ return p2;
+
+ }
+
+}
\ No newline at end of file
diff --git a/hw/hw-02/HW02_65b21a17a4544/CDLinkedListTest.java b/hw/hw-02/HW02_65b21a17a4544/CDLinkedListTest.java
new file mode 100644
index 0000000..90391ed
--- /dev/null
+++ b/hw/hw-02/HW02_65b21a17a4544/CDLinkedListTest.java
@@ -0,0 +1,233 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CDLinkedListTest {
+ CDLinkedList l1 = new CDLinkedList();
+ DListIterator[] itrs = new DListIterator[4];
+ DListIterator itr = new DListIterator(l1.header);
+ int[] a = new int[12];
+
+ void setIterators(int p1, int p2, int p3, int p4) throws Exception {
+ for (int i = 1; i <= 12; i++) {
+ itr.next();
+ if (i == p1) {
+ itrs[0] = new DListIterator(itr.currentNode);
+ }
+ if (i == p2) {
+ itrs[1] = new DListIterator(itr.currentNode);
+ }
+ if (i == p3) {
+ itrs[2] = new DListIterator(itr.currentNode);
+ }
+ if (i == p4) {
+ itrs[3] = new DListIterator(itr.currentNode);
+ }
+
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ void storeInArray() throws Exception {
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ a[i-1] = v;
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ @BeforeEach
+ void setUp() throws Exception {
+ l1 = new CDLinkedList();
+ itrs = new DListIterator[4];
+ itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ l1.insert(i, itr);
+ itr.next();
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ @Test
+ void testSwapRangeNull() throws Exception {
+ l1.swapRange(null, null, null, null);
+ DListIterator itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ assertEquals(i, v);
+ }
+
+ }
+
+ @Test
+ void testSwapRangeNotDListIterator() throws Exception{
+ setIterators(2,4,8,11);
+ Iterator it = new Iterator() {
+
+ @Override
+ public void set(int value) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public int previous() throws Exception {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public int next() throws Exception {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean hasNext() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+ };
+
+ l1.swapRange(itrs[0], itrs[1], it, itrs[3]);
+ DListIterator itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ assertEquals(i, v);
+ }
+ }
+
+ @Test
+ void testSwapRangeGeneric() throws Exception {
+ setIterators(2,4,8,11);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+ itr = new DListIterator(l1.header);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(8,a[1]);
+ assertEquals(9,a[2]);
+ assertEquals(10,a[3]);
+ assertEquals(11,a[4]);
+ assertEquals(5,a[5]);
+ assertEquals(6,a[6]);
+ assertEquals(7,a[7]);
+ assertEquals(2,a[8]);
+ assertEquals(3,a[9]);
+ assertEquals(4,a[10]);
+ assertEquals(12,a[11]);
+
+ }
+
+ @Test
+ void testSwapRangeOneDataBoth() throws Exception {
+ setIterators(2,2,8,8);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(8,a[1]);
+ assertEquals(3,a[2]);
+ assertEquals(4,a[3]);
+ assertEquals(5,a[4]);
+ assertEquals(6,a[5]);
+ assertEquals(7,a[6]);
+ assertEquals(2,a[7]);
+ assertEquals(9,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+
+ }
+
+ @Test
+ void testSwapRangeOneData1stRange() throws Exception {
+ setIterators(3,3,6,9);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(2,a[1]);
+ assertEquals(6,a[2]);
+ assertEquals(7,a[3]);
+ assertEquals(8,a[4]);
+ assertEquals(9,a[5]);
+ assertEquals(4,a[6]);
+ assertEquals(5,a[7]);
+ assertEquals(3,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeOneData2NdRange() throws Exception {
+ setIterators(2,6,10,10);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(10,a[1]);
+ assertEquals(7,a[2]);
+ assertEquals(8,a[3]);
+ assertEquals(9,a[4]);
+ assertEquals(2,a[5]);
+ assertEquals(3,a[6]);
+ assertEquals(4,a[7]);
+ assertEquals(5,a[8]);
+ assertEquals(6,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeAdjacent() throws Exception {
+ setIterators(1,4,5,7);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(5,a[0]);
+ assertEquals(6,a[1]);
+ assertEquals(7,a[2]);
+ assertEquals(1,a[3]);
+ assertEquals(2,a[4]);
+ assertEquals(3,a[5]);
+ assertEquals(4,a[6]);
+ assertEquals(8,a[7]);
+ assertEquals(9,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeEndList() throws Exception {
+ setIterators(4,7,9,12);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(2,a[1]);
+ assertEquals(3,a[2]);
+ assertEquals(9,a[3]);
+ assertEquals(10,a[4]);
+ assertEquals(11,a[5]);
+ assertEquals(12,a[6]);
+ assertEquals(8,a[7]);
+ assertEquals(4,a[8]);
+ assertEquals(5,a[9]);
+ assertEquals(6,a[10]);
+ assertEquals(7,a[11]);
+
+
+ }
+
+
+}
diff --git a/hw/hw-02/HW02_65b21a17a4544/DListIterator.java b/hw/hw-02/HW02_65b21a17a4544/DListIterator.java
new file mode 100644
index 0000000..aad3e48
--- /dev/null
+++ b/hw/hw-02/HW02_65b21a17a4544/DListIterator.java
@@ -0,0 +1,39 @@
+import java.util.NoSuchElementException;
+
+public class DListIterator implements Iterator {
+ DListNode currentNode; // interested position
+
+ DListIterator(DListNode theNode) {
+ currentNode = theNode;
+ }
+
+ public boolean hasNext() { // always true for circular list.
+ return currentNode.nextNode != null;
+ }
+
+ public boolean hasPrevious() { // always true for circular list.
+ return currentNode.previousNode != null;
+ }
+
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ public int previous() throws Exception{
+ if (!hasPrevious())
+ throw new NoSuchElementException();
+ int data = currentNode.data;
+ currentNode = currentNode.previousNode;
+ return data;
+ }
+
+ public void set(int value) {
+ currentNode.data = value;
+ }
+}
diff --git a/hw/hw-02/HW02_65b21a17a4544/DListNode.java b/hw/hw-02/HW02_65b21a17a4544/DListNode.java
new file mode 100644
index 0000000..04a3895
--- /dev/null
+++ b/hw/hw-02/HW02_65b21a17a4544/DListNode.java
@@ -0,0 +1,16 @@
+
+public class DListNode {
+ int data;
+ DListNode nextNode, previousNode;
+
+ DListNode(int data) {
+ this(data, null, null);
+ }
+
+ DListNode(int theElement, DListNode n, DListNode p) {
+ data = theElement;
+ nextNode = n;
+ previousNode = p;
+ }
+
+}
diff --git a/hw/hw-02/HW02_65b21a17a4544/HW02.docx b/hw/hw-02/HW02_65b21a17a4544/HW02.docx
new file mode 100644
index 0000000..679a966
Binary files /dev/null and b/hw/hw-02/HW02_65b21a17a4544/HW02.docx differ
diff --git a/hw/hw-02/HW02_65b21a17a4544/HW02.pdf b/hw/hw-02/HW02_65b21a17a4544/HW02.pdf
new file mode 100644
index 0000000..e4fdca0
Binary files /dev/null and b/hw/hw-02/HW02_65b21a17a4544/HW02.pdf differ
diff --git a/hw/hw-02/HW02_65b21a17a4544/Iterator.java b/hw/hw-02/HW02_65b21a17a4544/Iterator.java
new file mode 100644
index 0000000..bbb54ef
--- /dev/null
+++ b/hw/hw-02/HW02_65b21a17a4544/Iterator.java
@@ -0,0 +1,17 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-02/hw-02/.DS_Store b/hw/hw-02/hw-02/.DS_Store
new file mode 100644
index 0000000..fb1ca9e
Binary files /dev/null and b/hw/hw-02/hw-02/.DS_Store differ
diff --git a/hw/hw-02/hw-02/.classpath b/hw/hw-02/hw-02/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-02/hw-02/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-02/hw-02/.gitignore b/hw/hw-02/hw-02/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-02/hw-02/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-02/hw-02/.project b/hw/hw-02/hw-02/.project
new file mode 100644
index 0000000..5905fef
--- /dev/null
+++ b/hw/hw-02/hw-02/.project
@@ -0,0 +1,17 @@
+
+
+ hw-02
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-02/hw-02/.settings/org.eclipse.core.resources.prefs b/hw/hw-02/hw-02/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-02/hw-02/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-02/hw-02/.settings/org.eclipse.jdt.core.prefs b/hw/hw-02/hw-02/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-02/hw-02/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-02/hw-02/src/CDLinkedList.java b/hw/hw-02/hw-02/src/CDLinkedList.java
new file mode 100644
index 0000000..036aff1
--- /dev/null
+++ b/hw/hw-02/hw-02/src/CDLinkedList.java
@@ -0,0 +1,209 @@
+public class CDLinkedList {
+ DListNode header;
+ int size;
+ static final int HEADERVALUE = -9999999;
+
+ public CDLinkedList() {
+ header = new DListNode(HEADERVALUE);
+ makeEmpty();// necessary, otherwise next/previous node will be null
+ }
+
+ public boolean isEmpty() {
+ return header.nextNode == header;
+ }
+
+ public boolean isFull() {
+ return false;
+ }
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = header;
+ header.previousNode = header;
+ size =0;
+ }
+
+ // put in new data after the position of p.
+ public void insert(int value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof DListIterator))
+ throw new Exception();
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode);
+ DListNode n = new DListNode(value, p3.currentNode, p2.currentNode);
+ p2.currentNode.nextNode = n;
+ p3.currentNode.previousNode = n;
+ size++;
+ }
+
+ // return position number of value found in the list.
+ // otherwise, return -1.
+ public int find(int value) throws Exception {
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ return -1;
+ if (v == value)
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ // return data stored at kth position.
+ public int findKth(int kthPosition) throws Exception {
+ if (kthPosition < 0 || kthPosition > size-1)
+ throw new Exception();// exit the method if the position is
+ // beyond the first/last possible
+ // position, throwing exception in the process.
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ throw new Exception();
+ if (index == kthPosition)
+ return v;
+ }
+ throw new Exception();
+ }
+
+ // Return iterator at position before the first position that stores value.
+ // If the value is not found, return null.
+ public Iterator findPrevious(int value) throws Exception {
+ if (isEmpty())
+ return null;
+ Iterator itr1 = new DListIterator(header);
+ Iterator itr2 = new DListIterator(header);
+ int currentData = itr2.next();
+ while (currentData != value) {
+ currentData = itr2.next();
+ itr1.next();
+ if (((DListIterator) itr2).currentNode == header)
+ return null;
+ }
+ return itr1;
+ }
+
+ // remove content at position just after the given iterator. Skip header if
+ // found.
+ public void remove(Iterator p) {
+ if (isEmpty())
+ return;
+ if (p == null || !(p instanceof DListIterator))
+ return;
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ return;
+ if (p2.currentNode.nextNode == header)
+ p2.currentNode = header;
+ if (p2.currentNode.nextNode == null)
+ return;
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode);
+ p2.currentNode.nextNode = p3.currentNode;
+ p3.currentNode.previousNode = p2.currentNode;
+ size--;
+ }
+
+ // remove the first instance of the given data.
+ public void remove(int value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // remove data at position p.
+ // if p points to header or the list is empty, do nothing.
+ public void removeAt(Iterator p) throws Exception {
+ if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null
+ || ((DListIterator) p).currentNode == header)
+ return;
+
+ DListIterator p2 = (DListIterator) (findPrevious(p));
+ remove(p2);
+
+ }
+
+ // Print each contact out, one by one.
+ // To be completed by students.
+ public void printList() throws Exception {
+ Iterator itr = new DListIterator(header);
+ while (itr.hasNext()) {
+ Object data = itr.next();
+
+ System.out.println(data);
+
+ }
+ }
+
+ public int size() throws Exception {
+ return size;
+ }
+
+ // return iterator pointing to location before position.
+ public Iterator findPrevious(Iterator position) throws Exception {
+ if (position == null)
+ return null;
+ if (!(position instanceof DListIterator))
+ return null;
+ if (((DListIterator) position).currentNode == null)
+ return null;
+
+ DListIterator p = ((DListIterator) position);
+ DListIterator p2 = new DListIterator(p.currentNode.previousNode);
+ return p2;
+
+ }
+
+ //Assignment
+ public void swapRange(Iterator s1, Iterator f1, Iterator s2, Iterator f2) throws Exception {
+ if(s1 == null || !(s1 instanceof DListIterator) || f1 == null || !(f1 instanceof DListIterator) || s2 == null || !(s2 instanceof DListIterator) || f2 == null || !(f2 instanceof DListIterator)) return;
+
+ // all points: preceding and following
+ DListIterator s1p = new DListIterator(((DListIterator) s1).currentNode.previousNode);
+ DListIterator s1d = (DListIterator) s1;
+ DListIterator f1d = (DListIterator) f1;
+ DListIterator f1f = new DListIterator(((DListIterator) f1).currentNode.nextNode);
+ DListIterator s2p = new DListIterator(((DListIterator) s2).currentNode.previousNode);
+ DListIterator s2d = (DListIterator) s2;
+ DListIterator f2d = (DListIterator) f2;
+ DListIterator f2f = new DListIterator(((DListIterator) f2).currentNode.nextNode);
+
+
+
+ if(f1f.currentNode == s2d.currentNode) {
+ // 1
+ s1p.currentNode.nextNode = s2d.currentNode;
+ s2d.currentNode.previousNode = s1p.currentNode;
+ // 4
+ f1d.currentNode.nextNode = f2f.currentNode;
+ f2f.currentNode.previousNode = f1d.currentNode;
+ // inner
+ f2d.currentNode.nextNode = s1d.currentNode;
+ s1d.currentNode.previousNode = f2d.currentNode;
+ } else {
+ // 1
+ s1p.currentNode.nextNode = s2d.currentNode;
+ s2d.currentNode.previousNode = s1p.currentNode;
+ // 2
+ f2d.currentNode.nextNode = f1f.currentNode;
+ f1f.currentNode.previousNode = f2d.currentNode;
+ // 3
+ s2p.currentNode.nextNode = s1d.currentNode;
+ s1d.currentNode.previousNode = s2p.currentNode;
+ // 4
+ f1d.currentNode.nextNode = f2f.currentNode;
+ f2f.currentNode.previousNode = f1d.currentNode;
+ }
+
+
+ }
+
+
+}
\ No newline at end of file
diff --git a/hw/hw-02/hw-02/src/CDLinkedListTest.java b/hw/hw-02/hw-02/src/CDLinkedListTest.java
new file mode 100644
index 0000000..90391ed
--- /dev/null
+++ b/hw/hw-02/hw-02/src/CDLinkedListTest.java
@@ -0,0 +1,233 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class CDLinkedListTest {
+ CDLinkedList l1 = new CDLinkedList();
+ DListIterator[] itrs = new DListIterator[4];
+ DListIterator itr = new DListIterator(l1.header);
+ int[] a = new int[12];
+
+ void setIterators(int p1, int p2, int p3, int p4) throws Exception {
+ for (int i = 1; i <= 12; i++) {
+ itr.next();
+ if (i == p1) {
+ itrs[0] = new DListIterator(itr.currentNode);
+ }
+ if (i == p2) {
+ itrs[1] = new DListIterator(itr.currentNode);
+ }
+ if (i == p3) {
+ itrs[2] = new DListIterator(itr.currentNode);
+ }
+ if (i == p4) {
+ itrs[3] = new DListIterator(itr.currentNode);
+ }
+
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ void storeInArray() throws Exception {
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ a[i-1] = v;
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ @BeforeEach
+ void setUp() throws Exception {
+ l1 = new CDLinkedList();
+ itrs = new DListIterator[4];
+ itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ l1.insert(i, itr);
+ itr.next();
+ }
+ itr = new DListIterator(l1.header);
+ }
+
+ @Test
+ void testSwapRangeNull() throws Exception {
+ l1.swapRange(null, null, null, null);
+ DListIterator itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ assertEquals(i, v);
+ }
+
+ }
+
+ @Test
+ void testSwapRangeNotDListIterator() throws Exception{
+ setIterators(2,4,8,11);
+ Iterator it = new Iterator() {
+
+ @Override
+ public void set(int value) {
+ // TODO Auto-generated method stub
+
+ }
+
+ @Override
+ public int previous() throws Exception {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public int next() throws Exception {
+ // TODO Auto-generated method stub
+ return 0;
+ }
+
+ @Override
+ public boolean hasPrevious() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+
+ @Override
+ public boolean hasNext() {
+ // TODO Auto-generated method stub
+ return false;
+ }
+ };
+
+ l1.swapRange(itrs[0], itrs[1], it, itrs[3]);
+ DListIterator itr = new DListIterator(l1.header);
+ for (int i = 1; i <= 12; i++) {
+ int v = itr.next();
+ assertEquals(i, v);
+ }
+ }
+
+ @Test
+ void testSwapRangeGeneric() throws Exception {
+ setIterators(2,4,8,11);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+ itr = new DListIterator(l1.header);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(8,a[1]);
+ assertEquals(9,a[2]);
+ assertEquals(10,a[3]);
+ assertEquals(11,a[4]);
+ assertEquals(5,a[5]);
+ assertEquals(6,a[6]);
+ assertEquals(7,a[7]);
+ assertEquals(2,a[8]);
+ assertEquals(3,a[9]);
+ assertEquals(4,a[10]);
+ assertEquals(12,a[11]);
+
+ }
+
+ @Test
+ void testSwapRangeOneDataBoth() throws Exception {
+ setIterators(2,2,8,8);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(8,a[1]);
+ assertEquals(3,a[2]);
+ assertEquals(4,a[3]);
+ assertEquals(5,a[4]);
+ assertEquals(6,a[5]);
+ assertEquals(7,a[6]);
+ assertEquals(2,a[7]);
+ assertEquals(9,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+
+ }
+
+ @Test
+ void testSwapRangeOneData1stRange() throws Exception {
+ setIterators(3,3,6,9);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(2,a[1]);
+ assertEquals(6,a[2]);
+ assertEquals(7,a[3]);
+ assertEquals(8,a[4]);
+ assertEquals(9,a[5]);
+ assertEquals(4,a[6]);
+ assertEquals(5,a[7]);
+ assertEquals(3,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeOneData2NdRange() throws Exception {
+ setIterators(2,6,10,10);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(10,a[1]);
+ assertEquals(7,a[2]);
+ assertEquals(8,a[3]);
+ assertEquals(9,a[4]);
+ assertEquals(2,a[5]);
+ assertEquals(3,a[6]);
+ assertEquals(4,a[7]);
+ assertEquals(5,a[8]);
+ assertEquals(6,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeAdjacent() throws Exception {
+ setIterators(1,4,5,7);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(5,a[0]);
+ assertEquals(6,a[1]);
+ assertEquals(7,a[2]);
+ assertEquals(1,a[3]);
+ assertEquals(2,a[4]);
+ assertEquals(3,a[5]);
+ assertEquals(4,a[6]);
+ assertEquals(8,a[7]);
+ assertEquals(9,a[8]);
+ assertEquals(10,a[9]);
+ assertEquals(11,a[10]);
+ assertEquals(12,a[11]);
+ }
+
+ @Test
+ void testSwapRangeEndList() throws Exception {
+ setIterators(4,7,9,12);
+ l1.swapRange(itrs[0], itrs[1], itrs[2], itrs[3]);
+
+ storeInArray();
+ assertEquals(1,a[0]);
+ assertEquals(2,a[1]);
+ assertEquals(3,a[2]);
+ assertEquals(9,a[3]);
+ assertEquals(10,a[4]);
+ assertEquals(11,a[5]);
+ assertEquals(12,a[6]);
+ assertEquals(8,a[7]);
+ assertEquals(4,a[8]);
+ assertEquals(5,a[9]);
+ assertEquals(6,a[10]);
+ assertEquals(7,a[11]);
+
+
+ }
+
+
+}
diff --git a/hw/hw-02/hw-02/src/DListIterator.java b/hw/hw-02/hw-02/src/DListIterator.java
new file mode 100644
index 0000000..aad3e48
--- /dev/null
+++ b/hw/hw-02/hw-02/src/DListIterator.java
@@ -0,0 +1,39 @@
+import java.util.NoSuchElementException;
+
+public class DListIterator implements Iterator {
+ DListNode currentNode; // interested position
+
+ DListIterator(DListNode theNode) {
+ currentNode = theNode;
+ }
+
+ public boolean hasNext() { // always true for circular list.
+ return currentNode.nextNode != null;
+ }
+
+ public boolean hasPrevious() { // always true for circular list.
+ return currentNode.previousNode != null;
+ }
+
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ public int previous() throws Exception{
+ if (!hasPrevious())
+ throw new NoSuchElementException();
+ int data = currentNode.data;
+ currentNode = currentNode.previousNode;
+ return data;
+ }
+
+ public void set(int value) {
+ currentNode.data = value;
+ }
+}
diff --git a/hw/hw-02/hw-02/src/DListNode.java b/hw/hw-02/hw-02/src/DListNode.java
new file mode 100644
index 0000000..04a3895
--- /dev/null
+++ b/hw/hw-02/hw-02/src/DListNode.java
@@ -0,0 +1,16 @@
+
+public class DListNode {
+ int data;
+ DListNode nextNode, previousNode;
+
+ DListNode(int data) {
+ this(data, null, null);
+ }
+
+ DListNode(int theElement, DListNode n, DListNode p) {
+ data = theElement;
+ nextNode = n;
+ previousNode = p;
+ }
+
+}
diff --git a/hw/hw-02/hw-02/src/Iterator.java b/hw/hw-02/hw-02/src/Iterator.java
new file mode 100644
index 0000000..bbb54ef
--- /dev/null
+++ b/hw/hw-02/hw-02/src/Iterator.java
@@ -0,0 +1,17 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-03/.DS_Store b/hw/hw-03/.DS_Store
new file mode 100644
index 0000000..6774b82
Binary files /dev/null and b/hw/hw-03/.DS_Store differ
diff --git a/hw/hw-03/HW03_toStudent_65b22cd161561.zip b/hw/hw-03/HW03_toStudent_65b22cd161561.zip
new file mode 100644
index 0000000..84c111e
Binary files /dev/null and b/hw/hw-03/HW03_toStudent_65b22cd161561.zip differ
diff --git a/hw/hw-03/HW03_toStudent_65b22cd161561/HW_LinkedList02.pdf b/hw/hw-03/HW03_toStudent_65b22cd161561/HW_LinkedList02.pdf
new file mode 100644
index 0000000..a44106a
Binary files /dev/null and b/hw/hw-03/HW03_toStudent_65b22cd161561/HW_LinkedList02.pdf differ
diff --git a/hw/hw-03/HW03_toStudent_65b22cd161561/HW_Zooma_student.jar b/hw/hw-03/HW03_toStudent_65b22cd161561/HW_Zooma_student.jar
new file mode 100644
index 0000000..e9ed880
Binary files /dev/null and b/hw/hw-03/HW03_toStudent_65b22cd161561/HW_Zooma_student.jar differ
diff --git a/hw/hw-03/hw-03/.classpath b/hw/hw-03/hw-03/.classpath
new file mode 100644
index 0000000..86473c8
--- /dev/null
+++ b/hw/hw-03/hw-03/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-03/hw-03/.gitignore b/hw/hw-03/hw-03/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/hw/hw-03/hw-03/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/hw/hw-03/hw-03/.project b/hw/hw-03/hw-03/.project
new file mode 100644
index 0000000..4119e96
--- /dev/null
+++ b/hw/hw-03/hw-03/.project
@@ -0,0 +1,17 @@
+
+
+ hw-03
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-03/hw-03/.settings/org.eclipse.core.resources.prefs b/hw/hw-03/hw-03/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/hw/hw-03/hw-03/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/hw/hw-03/hw-03/.settings/org.eclipse.jdt.core.prefs b/hw/hw-03/hw-03/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/hw/hw-03/hw-03/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/hw/hw-03/hw-03/src/.classpath b/hw/hw-03/hw-03/src/.classpath
new file mode 100644
index 0000000..4a4692a
--- /dev/null
+++ b/hw/hw-03/hw-03/src/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/hw/hw-03/hw-03/src/.project b/hw/hw-03/hw-03/src/.project
new file mode 100644
index 0000000..4c0a4c6
--- /dev/null
+++ b/hw/hw-03/hw-03/src/.project
@@ -0,0 +1,17 @@
+
+
+ HW_Zooma_student
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/hw/hw-03/hw-03/src/CDLinkedList.java b/hw/hw-03/hw-03/src/CDLinkedList.java
new file mode 100644
index 0000000..e726ba2
--- /dev/null
+++ b/hw/hw-03/hw-03/src/CDLinkedList.java
@@ -0,0 +1,167 @@
+public class CDLinkedList {
+ DListNode header;
+ int size;
+ static final int HEADERVALUE = -9999999;
+
+ public CDLinkedList() {
+ header = new DListNode(HEADERVALUE);
+ makeEmpty();// necessary, otherwise next/previous node will be null
+ }
+
+ public boolean isEmpty() {
+ return header.nextNode == header;
+ }
+
+ public boolean isFull() {
+ return false;
+ }
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = header;
+ header.previousNode = header;
+ size = 0;
+ }
+
+ // put in new data after the position of p.
+ public void insert(int value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof DListIterator))
+ throw new Exception();
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode);
+ DListNode n = new DListNode(value, p3.currentNode, p2.currentNode);
+ p2.currentNode.nextNode = n;
+ p3.currentNode.previousNode = n;
+ size++;
+ }
+
+ // return position number of value found in the list.
+ // otherwise, return -1.
+ public int find(int value) throws Exception {
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ return -1;
+ if (v == value)
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ // return data stored at kth position.
+ public int findKth(int kthPosition) throws Exception {
+ if (kthPosition < 0 || kthPosition > size - 1)
+ throw new Exception();// exit the method if the position is
+ // beyond the first/last possible
+ // position, throwing exception in the process.
+ DListIterator itr = new DListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ int v = itr.next();
+ index++;
+ if (itr.currentNode == header)
+ throw new Exception();
+ if (index == kthPosition)
+ return v;
+ }
+ throw new Exception();
+ }
+
+ // Return iterator at position before the first position that stores value.
+ // If the value is not found, return null.
+ public Iterator findPrevious(int value) throws Exception {
+ if (isEmpty())
+ return null;
+ Iterator itr1 = new DListIterator(header);
+ Iterator itr2 = new DListIterator(header);
+ int currentData = itr2.next();
+ while (currentData != value) {
+ currentData = itr2.next();
+ itr1.next();
+ if (((DListIterator) itr2).currentNode == header)
+ return null;
+ }
+ return itr1;
+ }
+
+ // remove content at position just after the given iterator. Skip header if
+ // found.
+ public void remove(Iterator p) {
+ if (isEmpty())
+ return;
+ if (p == null || !(p instanceof DListIterator))
+ return;
+ DListIterator p2 = (DListIterator) p;
+ if (p2.currentNode == null)
+ return;
+ if (p2.currentNode.nextNode == header)
+ p2.currentNode = header;
+ if (p2.currentNode.nextNode == null)
+ return;
+ DListIterator p3 = new DListIterator(p2.currentNode.nextNode.nextNode);
+ p2.currentNode.nextNode = p3.currentNode;
+ p3.currentNode.previousNode = p2.currentNode;
+ size--;
+ }
+
+ // remove the first instance of the given data.
+ public void remove(int value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // remove data at position p.
+ // if p points to header or the list is empty, do nothing.
+ public void removeAt(Iterator p) throws Exception {
+ if (isEmpty() || p == null || !(p instanceof DListIterator) || ((DListIterator) p).currentNode == null
+ || ((DListIterator) p).currentNode == header)
+ return;
+
+ DListIterator p2 = (DListIterator) (findPrevious(p));
+ remove(p2);
+
+ }
+
+ // Print each contact out into a string, one by one.
+ public String printList() throws Exception {
+ String ans = "";
+ DListIterator itr = new DListIterator(header);
+ while (itr.hasNext() && itr.currentNode.nextNode != header) {
+ Object data = itr.next();
+
+ ans += data +" ";
+ }
+ ans = ans.trim();
+ return ans;
+ }
+
+ public int size() throws Exception {
+ return size;
+ }
+
+ // return iterator pointing to location before position.
+ public Iterator findPrevious(Iterator position) throws Exception {
+ if (position == null)
+ return null;
+ if (!(position instanceof DListIterator))
+ return null;
+ if (((DListIterator) position).currentNode == null)
+ return null;
+
+ DListIterator p = ((DListIterator) position);
+ DListIterator p2 = new DListIterator(p.currentNode.previousNode);
+ return p2;
+
+ }
+
+
+
+}
\ No newline at end of file
diff --git a/hw/hw-03/hw-03/src/DListIterator.java b/hw/hw-03/hw-03/src/DListIterator.java
new file mode 100644
index 0000000..aad3e48
--- /dev/null
+++ b/hw/hw-03/hw-03/src/DListIterator.java
@@ -0,0 +1,39 @@
+import java.util.NoSuchElementException;
+
+public class DListIterator implements Iterator {
+ DListNode currentNode; // interested position
+
+ DListIterator(DListNode theNode) {
+ currentNode = theNode;
+ }
+
+ public boolean hasNext() { // always true for circular list.
+ return currentNode.nextNode != null;
+ }
+
+ public boolean hasPrevious() { // always true for circular list.
+ return currentNode.previousNode != null;
+ }
+
+ public int next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ public int previous() throws Exception{
+ if (!hasPrevious())
+ throw new NoSuchElementException();
+ int data = currentNode.data;
+ currentNode = currentNode.previousNode;
+ return data;
+ }
+
+ public void set(int value) {
+ currentNode.data = value;
+ }
+}
diff --git a/hw/hw-03/hw-03/src/DListNode.java b/hw/hw-03/hw-03/src/DListNode.java
new file mode 100644
index 0000000..04a3895
--- /dev/null
+++ b/hw/hw-03/hw-03/src/DListNode.java
@@ -0,0 +1,16 @@
+
+public class DListNode {
+ int data;
+ DListNode nextNode, previousNode;
+
+ DListNode(int data) {
+ this(data, null, null);
+ }
+
+ DListNode(int theElement, DListNode n, DListNode p) {
+ data = theElement;
+ nextNode = n;
+ previousNode = p;
+ }
+
+}
diff --git a/hw/hw-03/hw-03/src/Iterator.java b/hw/hw-03/hw-03/src/Iterator.java
new file mode 100644
index 0000000..bbb54ef
--- /dev/null
+++ b/hw/hw-03/hw-03/src/Iterator.java
@@ -0,0 +1,17 @@
+public interface Iterator {
+
+ public boolean hasNext();
+
+ public boolean hasPrevious();
+
+ // Move iterator to the next position,
+ // then returns the value at that position.
+ public int next() throws Exception;
+
+ // Return the value at current position,
+ // then move the iterator back one position.
+ public int previous() throws Exception;
+
+ public void set(int value);
+
+}
diff --git a/hw/hw-03/hw-03/src/TestNoLoop.java b/hw/hw-03/hw-03/src/TestNoLoop.java
new file mode 100644
index 0000000..fafc57c
--- /dev/null
+++ b/hw/hw-03/hw-03/src/TestNoLoop.java
@@ -0,0 +1,37 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.util.Scanner;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestNoLoop {
+ @Test
+ void testNoLoopRemoveBetween() throws FileNotFoundException {
+ File f = new File("./src/ZoomaList.java");
+ Scanner s = new Scanner(f);
+
+ boolean foundLoop = false;
+ String line = "";
+ while (s.hasNextLine()) {
+ line = s.nextLine();
+ if (line.contains("removeBetween(DListIterator ")) {
+ break;
+ }
+ }
+
+ while (s.hasNextLine()) {
+ if (line.contains("for(") || line.contains("while(")) {
+ foundLoop = true;
+ break;
+ }
+ line = s.nextLine();
+ }
+ s.close();
+ assertFalse(foundLoop);
+
+ }
+
+}
diff --git a/hw/hw-03/hw-03/src/TestRemoveBetween.java b/hw/hw-03/hw-03/src/TestRemoveBetween.java
new file mode 100644
index 0000000..0ade1ce
--- /dev/null
+++ b/hw/hw-03/hw-03/src/TestRemoveBetween.java
@@ -0,0 +1,166 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.util.Iterator;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestRemoveBetween {
+
+ @BeforeEach
+ void setUp() throws Exception {
+ }
+
+ @Test
+ void testremoveEmptyRange01() throws Exception {
+ // list 1 -> 1
+
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(1, dTemp);
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+ d1.next();
+ d2.next();
+
+ l.removeBetween(d1, d2, 0);
+ assertEquals(1, l.size());
+ assertEquals("1", l.printList());
+
+ }
+
+ @Test
+ void testremoveEmptyRange02() throws Exception {
+ // list 1,2 -> 1,2
+
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(2, dTemp);
+ lTemp.insert(1, dTemp);
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+ d1.next();
+ d2.next();
+ d2.next();
+
+ l.removeBetween(d1, d2, 0);
+ assertEquals(2, l.size());
+ assertEquals("1 2", l.printList());
+
+ }
+
+ @Test
+ void testremoveFrontGone() throws Exception {
+ // list 1,2,3,4 -> 3,4
+ // ^ ^
+ // d1 d2
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(4, dTemp);
+ lTemp.insert(3, dTemp);
+ lTemp.insert(2, dTemp);
+ lTemp.insert(1, dTemp);
+
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+
+ for (int i = 1; i <= 3; i++) {
+ d2.next();
+ }
+ l.removeBetween(d1, d2, 2);
+ assertEquals(2, l.size());
+ assertEquals("3 4", l.printList());
+
+ }
+
+ @Test
+ void testremoveBackGone() throws Exception {
+ // list 1,2,3,4 -> 1,2
+ // ^ ^
+ // d2 d1
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(4, dTemp);
+ lTemp.insert(3, dTemp);
+ lTemp.insert(2, dTemp);
+ lTemp.insert(1, dTemp);
+
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+
+ for (int i = 1; i <= 2; i++) {
+ d1.next();
+ }
+ l.removeBetween(d1, d2, 3);
+ assertEquals(1, l.size());
+ assertEquals("1 2", l.printList());
+
+ }
+
+ @Test
+ void testremoveOneData() throws Exception {
+ // list 1,2,3,4,5 -> 1,2,4,5
+ // ^ ^
+ // d1 d2
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(5, dTemp);
+ lTemp.insert(4, dTemp);
+ lTemp.insert(3, dTemp);
+ lTemp.insert(2, dTemp);
+ lTemp.insert(1, dTemp);
+
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+
+ for (int i = 1; i <= 2; i++) {
+ d1.next();
+ }
+ for (int i = 1; i <= 4; i++) {
+ d2.next();
+ }
+
+ l.removeBetween(d1, d2, 1);
+ assertEquals(4, l.size());
+ assertEquals("1 2 4 5", l.printList());
+
+ }
+
+ @Test
+ void testremoveGeneral() throws Exception {
+ // list 1,2,3,4,5,6,7 -> 1,2,6,7
+ // ^ ^
+ // d1 d2
+ CDLinkedList lTemp = new CDLinkedList();
+ DListIterator dTemp = new DListIterator(lTemp.header);
+ lTemp.insert(7, dTemp);
+ lTemp.insert(6, dTemp);
+ lTemp.insert(5, dTemp);
+ lTemp.insert(4, dTemp);
+ lTemp.insert(3, dTemp);
+ lTemp.insert(2, dTemp);
+ lTemp.insert(1, dTemp);
+
+ ZoomaList l = new ZoomaList(lTemp);
+ DListIterator d1 = new DListIterator(l.header);
+ DListIterator d2 = new DListIterator(l.header);
+
+ for (int i = 1; i <= 2; i++) {
+ d1.next();
+ }
+ for (int i = 1; i <= 6; i++) {
+ d2.next();
+ }
+
+ l.removeBetween(d1, d2, 3);
+ assertEquals(4, l.size());
+ assertEquals("1 2 6 7", l.printList());
+
+ }
+
+}
diff --git a/hw/hw-03/hw-03/src/TestZoomaList.java b/hw/hw-03/hw-03/src/TestZoomaList.java
new file mode 100644
index 0000000..4b1760b
--- /dev/null
+++ b/hw/hw-03/hw-03/src/TestZoomaList.java
@@ -0,0 +1,197 @@
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestZoomaList {
+
+
+ @Test
+ void testInsertEmptyList() throws Exception {
+ ZoomaList l = new ZoomaList();
+ DListIterator d = new DListIterator(l.header);
+
+ l.insert(5,d);
+
+ assertEquals(1,l.size());
+ assertEquals(5,l.header.nextNode.data);
+
+ }
+
+ @Test
+ void testInsertOneElementList() throws Exception {
+ ZoomaList l = new ZoomaList();
+ DListIterator d = new DListIterator(l.header);
+
+ l.insert(5,d);
+ l.insert(5,d);
+ assertEquals(2,l.size());
+ assertEquals(5,l.header.nextNode.data);
+ assertEquals(5,l.header.previousNode.data);
+ }
+
+ @Test
+ void testInsertScoreOnce_01() throws Exception {
+ ZoomaList l = new ZoomaList();
+ DListIterator d = new DListIterator(l.header);
+ l.insert(1,d); //list 4,3,5,5,2,1 -> 4,3,(5),5,5,2,1 -> 4,3,2,1
+ l.insert(2,d);
+ l.insert(5,d);
+ l.insert(5,d);
+ l.insert(3,d);
+ l.insert(4,d);
+
+ d.next();
+ d.next();
+
+ l.insert(5,d);
+ assertEquals(4,l.size());
+ assertEquals(3,l.score);
+ assertEquals("4 3 2 1", l.printList());
+ }
+
+ @Test
+ void testInsertScoreOnce_02() throws Exception {
+ CDLinkedList l2 = new CDLinkedList();
+ DListIterator d2 = new DListIterator(l2.header);
+ l2.insert(1,d2); //list 4,3,5,5,5,1 -> 4,3,5,(5),5,5,1 -> 4,3,1
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(3,d2);
+ l2.insert(4,d2);
+
+ ZoomaList l = new ZoomaList(l2);
+ DListIterator d = new DListIterator(l.header);
+
+ d.next();
+ d.next();
+ d.next();
+ l.insert(5,d);
+ assertEquals(3,l.size());
+ assertEquals(4,l.score);
+ assertEquals("4 3 1", l.printList());
+
+ }
+
+ @Test
+ void testInsertScoreFrontGone() throws Exception {
+ CDLinkedList l2 = new CDLinkedList();
+ DListIterator d2 = new DListIterator(l2.header);
+ l2.insert(4,d2); //list 3,3,5,5,3,4,4,4 -> 3,3,5,5,(5),3,4,4,4 -> 4,4,4
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+ l2.insert(3,d2);
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(3,d2);
+ l2.insert(3,d2);
+
+ ZoomaList l = new ZoomaList(l2);
+ DListIterator d = new DListIterator(l.header);
+
+ d.next();
+ d.next();
+ d.next();
+ d.next();
+
+ l.insert(5,d);
+ assertEquals(3,l.size());
+ assertEquals(6,l.score);
+ assertEquals("4 4 4", l.printList());
+
+ }
+
+ @Test
+ void testInsertScoreBackGone() throws Exception {
+ CDLinkedList l2 = new CDLinkedList();
+ DListIterator d2 = new DListIterator(l2.header);
+ l2.insert(3,d2); //list 4,4,4,3,3,5,5,3 -> 4,4,4,3,3,(5),5,5,3 -> 4,4,4
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(3,d2);
+ l2.insert(3,d2);
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+
+ ZoomaList l = new ZoomaList(l2);
+ DListIterator d = new DListIterator(l.header);
+
+ d.next();
+ d.next();
+ d.next();
+ d.next();
+ d.next();
+
+ l.insert(5,d);
+ assertEquals(3,l.size());
+ assertEquals(6,l.score);
+ assertEquals("4 4 4", l.printList());
+
+ }
+
+ @Test
+ void testInsertScoreNoRemove() throws Exception {
+ CDLinkedList l2 = new CDLinkedList();
+ DListIterator d2 = new DListIterator(l2.header);
+ l2.insert(3,d2); //list 4,4,4,3,3,5,5,3 -> 4,4,4,3,3,(4),5,5,3
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(3,d2);
+ l2.insert(3,d2);
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+
+ ZoomaList l = new ZoomaList(l2);
+ DListIterator d = new DListIterator(l.header);
+
+ d.next();
+ d.next();
+ d.next();
+ d.next();
+ d.next();
+
+ l.insert(4,d);
+ assertEquals(9,l.size());
+ assertEquals(0,l.score);
+ assertEquals("4 4 4 3 3 4 5 5 3", l.printList());
+
+ }
+
+ @Test
+ void testInsertScoreGeneral() throws Exception {
+ CDLinkedList l2 = new CDLinkedList();
+ DListIterator d2 = new DListIterator(l2.header);
+ l2.insert(2,d2); //list -> 1,6,6,4,3,3,5,5,3,3,4,4,6,6,2
+ l2.insert(6,d2); // -> 1,6,6,4,3,3,5,(5),5,3,3,4,4,6,6,2
+ l2.insert(6,d2); // -> 1,2
+ l2.insert(4,d2);
+ l2.insert(4,d2);
+ l2.insert(3,d2);
+ l2.insert(3,d2);
+ l2.insert(5,d2);
+ l2.insert(5,d2);
+ l2.insert(3,d2);
+ l2.insert(3,d2);
+ l2.insert(4,d2);
+ l2.insert(6,d2);
+ l2.insert(6,d2);
+ l2.insert(1,d2);
+
+ ZoomaList l = new ZoomaList(l2);
+ DListIterator d = new DListIterator(l.header);
+
+ for(int i =1;i<=7;i++)
+ d.next();
+
+
+ l.insert(5,d);
+ assertEquals(2,l.size());
+ assertEquals(14,l.score);
+ assertEquals("1 2", l.printList());
+
+ }
+
+}
diff --git a/hw/hw-03/hw-03/src/ZoomaList.java b/hw/hw-03/hw-03/src/ZoomaList.java
new file mode 100644
index 0000000..83a8e0c
--- /dev/null
+++ b/hw/hw-03/hw-03/src/ZoomaList.java
@@ -0,0 +1,71 @@
+
+public class ZoomaList extends CDLinkedList {
+ int score = 0;
+
+ public ZoomaList() {
+ super();
+ }
+
+ public ZoomaList(CDLinkedList l) {
+ header = l.header;
+ size = l.size;
+ }
+
+ public void insert(int value, Iterator p) throws Exception {
+ //fill code
+
+ if (p == null || !(p instanceof DListIterator)) {
+ return;
+ } else if (this.size == 0 || this.size == 1) {
+ super.insert(value, p);
+ } else {
+ super.insert(value, p);
+ if (((DListIterator) p).currentNode != header) {
+ DListIterator l = (DListIterator) p;
+ l.next();
+ DListIterator r = new DListIterator(((DListIterator) p).currentNode);
+ while (l.currentNode.data == r.currentNode.data) {
+ if (l.currentNode.data == header.data) {
+ l.currentNode = r.currentNode;
+ } else {
+ r.currentNode = l.currentNode;
+ }
+ int i = -1;
+ while (l.currentNode.data == value) {
+ l.currentNode = l.currentNode.previousNode;
+ i++;
+ }
+ while (r.currentNode.data == value) {
+ r.currentNode = r.currentNode.nextNode;
+ i++;
+ }
+ if (i >= 3) {
+ this.removeBetween(l, r, i);
+ this.score += i;
+ value = l.currentNode.data;
+ } else {
+ break;
+ }
+
+ }
+ }
+
+ }
+
+
+ }
+
+
+ public void removeBetween(DListIterator left, DListIterator right, int inc) {
+ //fill code
+
+ if (left.currentNode == right.currentNode) {
+ return;
+ }
+ left.currentNode.nextNode = right.currentNode;
+ right.currentNode.previousNode = left.currentNode;
+ this.size -= inc;
+
+ }
+
+}
diff --git a/lab/.DS_Store b/lab/.DS_Store
index be2ab48..c5b2a85 100644
Binary files a/lab/.DS_Store and b/lab/.DS_Store differ
diff --git a/lab/lab-01/.DS_Store b/lab/lab-01/.DS_Store
index 36a9567..7d0206d 100644
Binary files a/lab/lab-01/.DS_Store and b/lab/lab-01/.DS_Store differ
diff --git a/lab/lab-02/.DS_Store b/lab/lab-02/.DS_Store
new file mode 100644
index 0000000..9c04b53
Binary files /dev/null and b/lab/lab-02/.DS_Store differ
diff --git a/lab/lab-02/Lab02_6538075121.jar b/lab/lab-02/Lab02_6538075121.jar
new file mode 100644
index 0000000..4655499
Binary files /dev/null and b/lab/lab-02/Lab02_6538075121.jar differ
diff --git a/lab/lab-02/Lab02_Student.jar b/lab/lab-02/Lab02_Student.jar
new file mode 100644
index 0000000..b038ff0
Binary files /dev/null and b/lab/lab-02/Lab02_Student.jar differ
diff --git a/lab/lab-02/lab-02/.DS_Store b/lab/lab-02/lab-02/.DS_Store
new file mode 100644
index 0000000..d9adc75
Binary files /dev/null and b/lab/lab-02/lab-02/.DS_Store differ
diff --git a/lab/lab-02/lab-02/.classpath b/lab/lab-02/lab-02/.classpath
new file mode 100644
index 0000000..092bd63
--- /dev/null
+++ b/lab/lab-02/lab-02/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-02/lab-02/.gitignore b/lab/lab-02/lab-02/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-02/lab-02/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-02/lab-02/.project b/lab/lab-02/lab-02/.project
new file mode 100644
index 0000000..0ee44c3
--- /dev/null
+++ b/lab/lab-02/lab-02/.project
@@ -0,0 +1,17 @@
+
+
+ lab-02
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-02/lab-02/.settings/org.eclipse.core.resources.prefs b/lab/lab-02/lab-02/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-02/lab-02/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-02/lab-02/.settings/org.eclipse.jdt.core.prefs b/lab/lab-02/lab-02/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-02/lab-02/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-02/lab-02/src/ATMTest.java b/lab/lab-02/lab-02/src/ATMTest.java
new file mode 100644
index 0000000..b627048
--- /dev/null
+++ b/lab/lab-02/lab-02/src/ATMTest.java
@@ -0,0 +1,119 @@
+import static org.junit.Assert.*;
+import org.junit.*;
+
+public class ATMTest {
+
+ // Creating a sample account for testing
+ private Account testAccount;
+
+ @Before
+ public void setUp() {
+ // Initialize a test account before each test
+ testAccount = new Account("123456", "Test Account");
+ }
+
+ @Test
+ public void testDeposit() {
+ try {
+ TransactionManager.getInstance().clear();
+ testAccount.deposit(100.0);
+ assertEquals(100.0, testAccount.getBalance(), 0.001);
+
+ // Ensure that the deposit transaction is logged
+ assertEquals(1, TransactionManager.getInstance().getTransactionList().size());
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(0).contains("DEPOSIT"));
+
+ testAccount.deposit(50.0);
+ assertEquals(150.0, testAccount.getBalance(), 0.001);
+ // Ensure that the deposit transaction is logged
+ assertEquals(2, TransactionManager.getInstance().getTransactionList().size());
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(0).contains("DEPOSIT"));
+
+ } catch (NegativeAmountException e) {
+ fail("Unexpected exception: " + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testWithdraw() {
+ try {
+ TransactionManager.getInstance().clear();
+ // Deposit initial amount
+ testAccount.deposit(200.0);
+
+ // Withdraw a valid amount
+ testAccount.withdraw(50.0);
+ assertEquals(150.0, testAccount.getBalance(), 0.001);
+
+ // Ensure that the withdrawal transaction is logged
+ assertEquals(2, TransactionManager.getInstance().getTransactionList().size());
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(0).contains("DEPOSIT"));
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(1).contains("WITHDRAW"));
+
+ // Attempt to withdraw an invalid amount (more than the balance)
+ try {
+ testAccount.withdraw(200.0);
+ fail("Expected InsufficientAmount exception");
+ } catch (InsufficientAmountException e) {
+ // Expected exception
+ }
+
+ } catch (NegativeAmountException | InsufficientAmountException e) {
+ fail("Unexpected exception: " + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testTransfer() {
+ try {
+ // Create a second account for testing transfer
+ Account targetAccount = new Account("654321", "Target Account");
+ TransactionManager.getInstance().clear();
+ // Deposit initial amount in the source account
+ testAccount.deposit(300.0);
+
+ // Transfer a valid amount
+ testAccount.transfer(targetAccount, 100.0);
+ assertEquals(200.0, testAccount.getBalance(), 0.001);
+ assertEquals(100.0, targetAccount.getBalance(), 0.001);
+
+ // Ensure that the transfer transactions are logged
+ assertEquals(2, TransactionManager.getInstance().getTransactionList().size());
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(0).contains("DEPOSIT"));
+ assertTrue(TransactionManager.getInstance().getTransactionList().get(1).contains("TRANSFER"));
+
+ // Attempt to transfer an invalid amount (more than the balance)
+ try {
+ testAccount.transfer(targetAccount, 250.0);
+ fail("Expected InsufficientAmount exception");
+ } catch (InsufficientAmountException e) {
+ // Expected exception
+ }
+
+ } catch (NegativeAmountException | InsufficientAmountException e) {
+ fail("Unexpected exception: " + e.getMessage());
+ }
+ }
+
+ @Test
+ public void testNegativeAmountException() {
+ try {
+ testAccount.deposit(-50.0);
+ fail("Expected NegativeAmount exception");
+ } catch (NegativeAmountException e) {
+ // Expected exception
+ }
+ }
+
+ @Test
+ public void testInsufficientAmountException() {
+ try {
+ testAccount.withdraw(100.0);
+ fail("Expected InsufficientAmount exception");
+ } catch (NegativeAmountException e) {
+ fail("Unexpected NegativeAmount exception");
+ } catch (InsufficientAmountException e) {
+ // Expected exception
+ }
+ }
+}
diff --git a/lab/lab-02/lab-02/src/Account.java b/lab/lab-02/lab-02/src/Account.java
new file mode 100644
index 0000000..8fa4bc9
--- /dev/null
+++ b/lab/lab-02/lab-02/src/Account.java
@@ -0,0 +1,73 @@
+public class Account {
+ private String accountId;
+ private String accountName;
+ private TransactionManager transactionManager;
+ private double balance;
+
+ public Account(String accountId, String accountName) {
+ // Put your code here
+
+ this.accountId = accountId;
+ this.accountName = accountName;
+ this.transactionManager = TransactionManager.getInstance();
+ this.balance = 0;
+
+
+ }
+
+ public void deposit(double amount) throws NegativeAmountException {
+ // Put your code here
+
+ if(amount < 0) {
+ throw new NegativeAmountException("Deposit must not be negative");
+ }
+
+ String msg = String.format("DEPOSIT - Amount: %f - Account ID: %s", amount, this.accountId);
+ this.transactionManager.logTransaction(msg);
+
+ this.balance += amount;
+ }
+
+ public void withdraw(double amount) throws NegativeAmountException, InsufficientAmountException {
+ // Put your code here
+
+ if(amount < 0) {
+ throw new NegativeAmountException("Withdrawal must not be negative");
+ }
+ if(amount > this.balance) {
+ throw new InsufficientAmountException("Insufficient funds");
+ }
+
+ String msg = String.format("WITHDRAWAL - Amount: %f - Account ID: %s", amount, this.accountId);
+ this.transactionManager.logTransaction(msg);
+
+ this.balance -= amount;
+ }
+
+ public void transfer(Account targetAccount, double amount) throws NegativeAmountException, InsufficientAmountException {
+ // Put your code here
+
+ if(amount < 0) {
+ throw new NegativeAmountException("Withdrawal must not be negative");
+ }
+ if(amount > this.balance) {
+ throw new InsufficientAmountException("Insufficient funds");
+ }
+
+ String msg = String.format("TRANSFER - Amount: %f - From Account ID: %s - To Account ID: %s", amount, this.accountId, targetAccount.accountId);
+ this.transactionManager.logTransaction(msg);
+
+ this.balance -= amount;
+ targetAccount.balance += amount;
+
+ }
+
+ public double getBalance() {
+ return balance;
+ }
+
+ public String getAccountId() {
+ return accountId;
+ }
+
+}
\ No newline at end of file
diff --git a/lab/lab-02/lab-02/src/InsufficientAmountException.java b/lab/lab-02/lab-02/src/InsufficientAmountException.java
new file mode 100644
index 0000000..738cd49
--- /dev/null
+++ b/lab/lab-02/lab-02/src/InsufficientAmountException.java
@@ -0,0 +1,6 @@
+
+public class InsufficientAmountException extends Exception {
+ public InsufficientAmountException(String message) {
+ super(message);
+ }
+}
\ No newline at end of file
diff --git a/lab/lab-02/lab-02/src/Main.java b/lab/lab-02/lab-02/src/Main.java
new file mode 100644
index 0000000..50a1d65
--- /dev/null
+++ b/lab/lab-02/lab-02/src/Main.java
@@ -0,0 +1,25 @@
+import java.util.ArrayList;
+
+public class Main {
+ public static void main(String[] args) {
+ // Sample usage
+ try {
+ Account sourceAccount = new Account("123456", "Source Account");
+ Account targetAccount = new Account("654321", "Target Account");
+
+ sourceAccount.deposit(500.0);
+ sourceAccount.withdraw(200.0);
+ sourceAccount.transfer(targetAccount, 100.0);
+
+ System.out.println("Source Account Balance: " + sourceAccount.getBalance());
+ System.out.println("Target Account Balance: " + targetAccount.getBalance());
+
+ // Display logged transactions
+ System.out.println("Logged Transactions:");
+ TransactionManager.getInstance().getTransactionList().forEach(System.out::println);
+
+ } catch (NegativeAmountException | InsufficientAmountException e) {
+ e.printStackTrace();
+ }
+ }
+}
\ No newline at end of file
diff --git a/lab/lab-02/lab-02/src/NegativeAmountException.java b/lab/lab-02/lab-02/src/NegativeAmountException.java
new file mode 100644
index 0000000..47045d5
--- /dev/null
+++ b/lab/lab-02/lab-02/src/NegativeAmountException.java
@@ -0,0 +1,6 @@
+// Exception for negative amounts
+public class NegativeAmountException extends Exception {
+ public NegativeAmountException(String message) {
+ super(message);
+ }
+}
\ No newline at end of file
diff --git a/lab/lab-02/lab-02/src/TransactionManager.java b/lab/lab-02/lab-02/src/TransactionManager.java
new file mode 100644
index 0000000..4605dcb
--- /dev/null
+++ b/lab/lab-02/lab-02/src/TransactionManager.java
@@ -0,0 +1,38 @@
+import java.util.ArrayList;
+
+class TransactionManager {
+ private static TransactionManager instance;
+ private ArrayList transactionList;
+
+ private TransactionManager() {
+ // Put your code here
+
+ this.transactionList = new ArrayList();
+
+ }
+
+ public void clear() {
+ this.transactionList.clear();
+ }
+
+ public static TransactionManager getInstance() {
+ // Put your code here
+
+ if(instance == null) {
+ instance = new TransactionManager();
+ }
+
+ return instance;
+ }
+
+ public void logTransaction(String transaction) {
+ // Put your code here
+
+ this.transactionList.add(transaction);
+
+ }
+
+ public ArrayList getTransactionList() {
+ return new ArrayList<>(transactionList);
+ }
+}
diff --git a/lab/lab-03/.DS_Store b/lab/lab-03/.DS_Store
new file mode 100644
index 0000000..977539c
Binary files /dev/null and b/lab/lab-03/.DS_Store differ
diff --git a/lab/lab-03/Lab03_6538075121.pdf b/lab/lab-03/Lab03_6538075121.pdf
new file mode 100644
index 0000000..a1e47db
Binary files /dev/null and b/lab/lab-03/Lab03_6538075121.pdf differ
diff --git a/lab/lab-03/Lab03_65b0ade71b5e3/Lab03.java b/lab/lab-03/Lab03_65b0ade71b5e3/Lab03.java
new file mode 100644
index 0000000..088ed31
--- /dev/null
+++ b/lab/lab-03/Lab03_65b0ade71b5e3/Lab03.java
@@ -0,0 +1,33 @@
+public class Lab03 {
+ private static int[] genData(int n,int min,int max) {
+ int[] data = new int[n];
+ for(int i=0;i data[j+1]) {
+ // c++;
+ int temp = data[j];
+ data[j] = data[j+1];
+ data[j+1] = temp;
+ }
+ }
+ // if(c == 0)
+ // break;
+ }
+ return data;
+ }
+ public static void main(String[] args) {
+ for(int n=0;n<=500000;n+=10000) {
+ int[] data = genData(n,0,n*100);
+ long startTime = System.nanoTime();
+ bubbleSort(data);
+ System.out.println(""+n+","+(System.nanoTime()-startTime));
+ }
+ }
+}
diff --git a/lab/lab-03/Lab03_65b0ade71b5e3/instructions.docx b/lab/lab-03/Lab03_65b0ade71b5e3/instructions.docx
new file mode 100644
index 0000000..44e1a5d
Binary files /dev/null and b/lab/lab-03/Lab03_65b0ade71b5e3/instructions.docx differ
diff --git a/lab/lab-03/lab-03/.DS_Store b/lab/lab-03/lab-03/.DS_Store
new file mode 100644
index 0000000..254d119
Binary files /dev/null and b/lab/lab-03/lab-03/.DS_Store differ
diff --git a/lab/lab-03/lab-03/.classpath b/lab/lab-03/lab-03/.classpath
new file mode 100644
index 0000000..7b57c0d
--- /dev/null
+++ b/lab/lab-03/lab-03/.classpath
@@ -0,0 +1,10 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-03/lab-03/.gitignore b/lab/lab-03/lab-03/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-03/lab-03/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-03/lab-03/.project b/lab/lab-03/lab-03/.project
new file mode 100644
index 0000000..dcf8a38
--- /dev/null
+++ b/lab/lab-03/lab-03/.project
@@ -0,0 +1,17 @@
+
+
+ lab-03
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-03/lab-03/.settings/org.eclipse.core.resources.prefs b/lab/lab-03/lab-03/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-03/lab-03/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-03/lab-03/.settings/org.eclipse.jdt.core.prefs b/lab/lab-03/lab-03/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-03/lab-03/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-03/lab-03/src/Lab03.java b/lab/lab-03/lab-03/src/Lab03.java
new file mode 100644
index 0000000..2afed21
--- /dev/null
+++ b/lab/lab-03/lab-03/src/Lab03.java
@@ -0,0 +1,33 @@
+public class Lab03 {
+ private static int[] genData(int n,int min,int max) {
+ int[] data = new int[n];
+ for(int i=0;i data[j+1]) {
+ c++;
+ int temp = data[j];
+ data[j] = data[j+1];
+ data[j+1] = temp;
+ }
+ }
+ if(c == 0)
+ break;
+ }
+ return data;
+ }
+ public static void main(String[] args) {
+ for(int n=0;n<=500000;n+=10000) {
+ int[] data = genData(n,0,n*100);
+ long startTime = System.nanoTime();
+ bubbleSort(data);
+ System.out.println(""+n+","+(System.nanoTime()-startTime));
+ }
+ }
+}
diff --git a/lab/lab-03/time0.csv b/lab/lab-03/time0.csv
new file mode 100644
index 0000000..0c6663e
--- /dev/null
+++ b/lab/lab-03/time0.csv
@@ -0,0 +1,51 @@
+0,2458
+"10,000",76279625
+"20,000",516930416
+"30,000",1381342500
+"40,000",2593280042
+"50,000",4219792625
+"60,000",6206949250
+"70,000",8571978500
+"80,000",11357967625
+"90,000",14499331666
+"100,000",17918258583
+"110,000",21787483917
+"120,000",25993024708
+"130,000",30788070417
+"140,000",35813096083
+"150,000",41192704583
+"160,000",47269328875
+"170,000",53230168000
+"180,000",60236228833
+"190,000",67013257333
+"200,000",74208051625
+"210,000",81945365625
+"220,000",89843482083
+"230,000",98845023708
+"240,000",1.07331E+11
+"250,000",1.16373E+11
+"260,000",1.26189E+11
+"270,000",1.36402E+11
+"280,000",1.47125E+11
+"290,000",1.57901E+11
+"300,000",1.69139E+11
+"310,000",1.80376E+11
+"320,000",1.92996E+11
+"330,000",2.04617E+11
+"340,000",2.17288E+11
+"350,000",2.44427E+11
+"360,000",2.474E+11
+"370,000",2.63121E+11
+"380,000",2.77109E+11
+"390,000",2.89467E+11
+"400,000",3.03896E+11
+"410,000",3.19418E+11
+"420,000",3.35994E+11
+"430,000",3.49574E+11
+"440,000",3.67424E+11
+"450,000",3.8408E+11
+"460,000",4.03035E+11
+"470,000",4.18413E+11
+"480,000",4.3566E+11
+"490,000",4.54178E+11
+"500,000",4.73143E+11
\ No newline at end of file
diff --git a/lab/lab-03/time0.txt b/lab/lab-03/time0.txt
new file mode 100644
index 0000000..d803139
--- /dev/null
+++ b/lab/lab-03/time0.txt
@@ -0,0 +1,52 @@
+
+0, 2458
+10000, 76279625
+20000, 516930416
+30000, 1381342500
+40000, 2593280042
+50000, 4219792625
+60000, 6206949250
+70000, 8571978500
+80000, 11357967625
+90000, 14499331666
+100000, 17918258583
+110000, 21787483917
+120000, 25993024708
+130000, 30788070417
+140000, 35813096083
+150000, 41192704583
+160000, 47269328875
+170000, 53230168000
+180000, 60236228833
+190000, 67013257333
+200000, 74208051625
+210000, 81945365625
+220000, 89843482083
+230000, 98845023708
+240000, 107330647042
+250000, 116372893166
+260000, 126188664708
+270000, 136402173125
+280000, 147124525875
+290000, 157900572000
+300000, 169138653208
+310000, 180375777750
+320000, 192995714250
+330000, 204616552708
+340000, 217288324042
+350000, 244426936125
+360000, 247400208542
+370000, 263120542000
+380000, 277108646791
+390000, 289466980500
+400000, 303896489958
+410000, 319417752666
+420000, 335993809166
+430000, 349574032792
+440000, 367423843792
+450000, 384079918792
+460000, 403034577209
+470000, 418412635625
+480000, 435660300375
+490000, 454178052417
+500000, 473143404041
\ No newline at end of file
diff --git a/lab/lab-03/time1.csv b/lab/lab-03/time1.csv
new file mode 100644
index 0000000..dbbfe2d
--- /dev/null
+++ b/lab/lab-03/time1.csv
@@ -0,0 +1,51 @@
+0,2125
+10000,83540292
+20000,563342000
+30000,1370531041
+40000,2596734250
+50000,4199852042
+60000,6171286583
+70000,8576922583
+80000,11271147458
+90000,14335459042
+100000,18377269666
+110000,22313930542
+120000,26605926542
+130000,31385555375
+140000,36423103875
+150000,41907280166
+160000,47978553125
+170000,54077659666
+180000,60516990042
+190000,67816916209
+200000,75459948709
+210000,83038969084
+220000,91043925875
+230000,99682308416
+240000,1.10446E+11
+250000,1.18587E+11
+260000,1.30108E+11
+270000,1.38544E+11
+280000,1.48863E+11
+290000,1.59632E+11
+300000,1.71206E+11
+310000,1.8321E+11
+320000,1.94827E+11
+330000,2.07588E+11
+340000,2.2026E+11
+350000,2.3409E+11
+360000,2.47599E+11
+370000,2.61546E+11
+380000,2.76132E+11
+390000,2.89829E+11
+400000,3.05679E+11
+410000,3.20956E+11
+420000,3.37939E+11
+430000,3.5374E+11
+440000,3.70478E+11
+450000,3.87697E+11
+460000,4.05384E+11
+470000,4.23412E+11
+480000,4.4179E+11
+490000,4.60403E+11
+500000,4.79239E+11
\ No newline at end of file
diff --git a/lab/lab-03/time1.txt b/lab/lab-03/time1.txt
new file mode 100644
index 0000000..65791c0
--- /dev/null
+++ b/lab/lab-03/time1.txt
@@ -0,0 +1,51 @@
+0, 2125
+10000, 83540292
+20000, 563342000
+30000, 1370531041
+40000, 2596734250
+50000, 4199852042
+60000, 6171286583
+70000, 8576922583
+80000, 11271147458
+90000, 14335459042
+100000, 18377269666
+110000, 22313930542
+120000, 26605926542
+130000, 31385555375
+140000, 36423103875
+150000, 41907280166
+160000, 47978553125
+170000, 54077659666
+180000, 60516990042
+190000, 67816916209
+200000, 75459948709
+210000, 83038969084
+220000, 91043925875
+230000, 99682308416
+240000, 110445633584
+250000, 118587297458
+260000, 130107603875
+270000, 138543770375
+280000, 148863416250
+290000, 159631553208
+300000, 171206393500
+310000, 183209914250
+320000, 194827359625
+330000, 207588317625
+340000, 220259813667
+350000, 234090465291
+360000, 247599335292
+370000, 261546046083
+380000, 276131976459
+390000, 289829155500
+400000, 305679095167
+410000, 320956406042
+420000, 337939026083
+430000, 353740296125
+440000, 370478254459
+450000, 387697427959
+460000, 405383933208
+470000, 423412247458
+480000, 441790005500
+490000, 460402664083
+500000, 479239457584
\ No newline at end of file
diff --git a/lab/lab-04/.DS_Store b/lab/lab-04/.DS_Store
new file mode 100644
index 0000000..73e3f1c
Binary files /dev/null and b/lab/lab-04/.DS_Store differ
diff --git a/lab/lab-04/6538075121_Lab04.jar b/lab/lab-04/6538075121_Lab04.jar
new file mode 100644
index 0000000..1e412b4
Binary files /dev/null and b/lab/lab-04/6538075121_Lab04.jar differ
diff --git a/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04 instructions_61f0e0cb0e2f1.docx b/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04 instructions_61f0e0cb0e2f1.docx
new file mode 100644
index 0000000..1cf96fa
Binary files /dev/null and b/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04 instructions_61f0e0cb0e2f1.docx differ
diff --git a/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04_Student.jar b/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04_Student.jar
new file mode 100644
index 0000000..ddf3eb3
Binary files /dev/null and b/lab/lab-04/Lab04_Student_65b9e835bb9a8/Lab04_Student.jar differ
diff --git a/lab/lab-04/lab-04/.classpath b/lab/lab-04/lab-04/.classpath
new file mode 100644
index 0000000..092bd63
--- /dev/null
+++ b/lab/lab-04/lab-04/.classpath
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/lab/lab-04/lab-04/.gitignore b/lab/lab-04/lab-04/.gitignore
new file mode 100644
index 0000000..ae3c172
--- /dev/null
+++ b/lab/lab-04/lab-04/.gitignore
@@ -0,0 +1 @@
+/bin/
diff --git a/lab/lab-04/lab-04/.project b/lab/lab-04/lab-04/.project
new file mode 100644
index 0000000..2eb8b0c
--- /dev/null
+++ b/lab/lab-04/lab-04/.project
@@ -0,0 +1,17 @@
+
+
+ lab-04
+
+
+
+
+
+ org.eclipse.jdt.core.javabuilder
+
+
+
+
+
+ org.eclipse.jdt.core.javanature
+
+
diff --git a/lab/lab-04/lab-04/.settings/org.eclipse.core.resources.prefs b/lab/lab-04/lab-04/.settings/org.eclipse.core.resources.prefs
new file mode 100644
index 0000000..99f26c0
--- /dev/null
+++ b/lab/lab-04/lab-04/.settings/org.eclipse.core.resources.prefs
@@ -0,0 +1,2 @@
+eclipse.preferences.version=1
+encoding/=UTF-8
diff --git a/lab/lab-04/lab-04/.settings/org.eclipse.jdt.core.prefs b/lab/lab-04/lab-04/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 0000000..ccfd559
--- /dev/null
+++ b/lab/lab-04/lab-04/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,14 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=20
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=20
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
+org.eclipse.jdt.core.compiler.release=enabled
+org.eclipse.jdt.core.compiler.source=20
diff --git a/lab/lab-04/lab-04/src/Contact.java b/lab/lab-04/lab-04/src/Contact.java
new file mode 100644
index 0000000..a5c161a
--- /dev/null
+++ b/lab/lab-04/lab-04/src/Contact.java
@@ -0,0 +1,18 @@
+import java.util.Scanner;
+
+class Contact {
+ String name;
+ String phone;
+ String email;
+
+ public boolean equals(Object x) {
+ Contact c = (Contact) x;
+ return (name.equals(c.name) || phone.equals(c.phone) || email
+ .equals(c.email));
+ }
+
+ public String toString() {
+ return "Name:" + name + " Phone:" + phone + " Email:" + email;
+ }
+}
+
diff --git a/lab/lab-04/lab-04/src/Iterator.java b/lab/lab-04/lab-04/src/Iterator.java
new file mode 100644
index 0000000..ef188fd
--- /dev/null
+++ b/lab/lab-04/lab-04/src/Iterator.java
@@ -0,0 +1,8 @@
+public interface Iterator {
+ public boolean hasNext();
+
+ public Object next() throws Exception;
+
+ public void set(Object value);
+
+}
diff --git a/lab/lab-04/lab-04/src/LinkedList.java b/lab/lab-04/lab-04/src/LinkedList.java
new file mode 100644
index 0000000..742ed48
--- /dev/null
+++ b/lab/lab-04/lab-04/src/LinkedList.java
@@ -0,0 +1,160 @@
+class ListNode {
+ // Constructors
+ ListNode(Object theElement) {
+ this(theElement, null);
+ }
+
+ ListNode(Object theElement, ListNode n) {
+ data = theElement;
+ nextNode = n;
+ }
+
+ // Friendly data; accessible by other package routines
+ Object data;
+ ListNode nextNode;
+}
+
+public class LinkedList {
+ ListNode header;
+
+ public LinkedList() {
+ header = new ListNode(null);
+ }
+//
+
+ /** make the list empty. */
+ public void makeEmpty() {
+ header.nextNode = null;
+ }
+
+ public void insert(Object value, Iterator p) throws Exception {
+ if (p == null || !(p instanceof MyListIterator))
+ throw new Exception();
+ MyListIterator p2 = (MyListIterator) p;
+ if (p2.currentNode == null)
+ throw new Exception();
+ ListNode n = new ListNode(value, p2.currentNode.nextNode);
+ p2.currentNode.nextNode = n;
+ }
+
+ public int find(Object value) throws Exception {
+ Iterator itr = new MyListIterator(header);
+ int index = -1;
+ while (itr.hasNext()) {
+ Object v = itr.next();
+ index++;
+ if (v.equals(value))
+ return index; // return the position of value.
+ }
+ return -1;
+ }
+
+ public Iterator findPrevious(Object value) throws Exception {
+ Iterator itr1 = new MyListIterator(header);
+ Iterator itr2 = new MyListIterator(header);
+ if (!itr2.hasNext())
+ return null;
+ Object currentData = itr2.next();
+ while (!currentData.equals(value) && itr2.hasNext()) {
+ currentData = itr2.next();
+ itr1.next();
+ }
+ if (currentData.equals(value))
+ return itr1;
+ return null;
+
+ }
+
+ public void remove(Iterator p) {
+ if (p == null || !(p instanceof MyListIterator))
+ return;
+ MyListIterator p2 = (MyListIterator) p;
+ if (p2.currentNode == null || p2.currentNode.nextNode == null)
+ return;
+ p2.currentNode.nextNode = p2.currentNode.nextNode.nextNode;
+ }
+
+ public void remove(Object value) throws Exception {
+ Iterator p = findPrevious(value);
+ if (p == null)
+ return;
+ remove(p);
+ }
+
+ // Returns the number of data stored in this list.
+ // To be completed by students.
+ public int size() throws Exception{
+ Iterator itr = new MyListIterator(header);
+ int index = 0;
+ while (itr.hasNext()) {
+ itr.next();
+ index++;
+ }
+ return index;
+
+ }
+
+ // Print each contact out, one by one.
+ // To be completed by students.
+ public void printList() throws Exception{
+ Iterator itr = new MyListIterator(header);
+ while (itr.hasNext()) {
+ Contact c = (Contact) itr.next();
+ System.out.println(String.format("%s %s %s", c.name, c.phone, c.email));
+ }
+ }
+
+ //Return iterator pointing to value that stores a given name, or null otherwise.
+ //To be completed by students.
+ public Iterator findPosition(String name) throws Exception{
+ Iterator itr = new MyListIterator(header);
+ while (itr.hasNext()) {
+ Contact c = (Contact) itr.next();
+ if (c.name.equals(name))
+ return itr;
+ }
+ return null;
+ }
+
+ //add a new contact to the list the contact must be added in such a way that each contact is sorted by name, in alphabetical order.
+ public void add(Object c) throws Exception{
+ MyListIterator p = new MyListIterator(header);
+
+ while(p.hasNext()) {
+ Contact x = (Contact) p.next();
+ if((x.name).compareTo(((Contact) c).name) > 0) {
+ p = (MyListIterator) findPrevious(x);
+ break;
+ }
+ }
+ insert((Object)c, p);
+
+ }
+
+ //Remove the contact pointed by the iterator, i, and then returns an iterator pointing to the next contact.
+ //If the removed contact is the last one, return the iterator pointing to the first contact
+ // (if there is no first contact, make the iterator point to header).
+ //If i is marking an illegal position that cannot be removed, just return null.
+ //To be completed by students.
+ public Iterator removeAt(Iterator i) throws Exception{
+ MyListIterator h = new MyListIterator(header);
+ MyListIterator itr = (MyListIterator) i;
+ if(itr == null || itr.currentNode == null || itr.currentNode.data == null) {
+ return null;
+ }
+ Iterator p = findPrevious(itr.currentNode.data);
+ remove(p);
+
+ if(p.hasNext()) {
+ p.next();
+ return p;
+ }else if(h.hasNext()){
+ h.next();
+ return h;
+ }else {
+ return h;
+ }
+ }
+
+
+}
\ No newline at end of file
diff --git a/lab/lab-04/lab-04/src/MyListIterator.java b/lab/lab-04/lab-04/src/MyListIterator.java
new file mode 100644
index 0000000..6ef64ca
--- /dev/null
+++ b/lab/lab-04/lab-04/src/MyListIterator.java
@@ -0,0 +1,35 @@
+import java.util.NoSuchElementException;
+
+public class MyListIterator implements Iterator {
+ ListNode currentNode; // interested position
+
+ /**
+ * @param theNode
+ * any node in the list
+ */
+ MyListIterator(ListNode theNode) {
+ currentNode = theNode;
+ }
+
+ @Override
+ public boolean hasNext() {
+ return currentNode.nextNode != null;
+ }
+
+ @Override
+ public Object next() throws Exception {
+ // Throw exception if the next data
+ // does not exist.
+ if (!hasNext())
+ throw new NoSuchElementException();
+ currentNode = currentNode.nextNode;
+ return currentNode.data;
+
+ }
+
+ @Override
+ public void set(Object value) {
+ currentNode.data = value;
+ }
+
+}
diff --git a/lab/lab-04/lab-04/src/TestLinkedList.java b/lab/lab-04/lab-04/src/TestLinkedList.java
new file mode 100644
index 0000000..47b761b
--- /dev/null
+++ b/lab/lab-04/lab-04/src/TestLinkedList.java
@@ -0,0 +1,272 @@
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class TestLinkedList {
+
+ @Test
+ public void testSize() throws Exception {
+ LinkedList list = new LinkedList();
+ assertEquals(0, list.size());
+
+ Contact a = new Contact();
+ Contact b = new Contact();
+ list.insert(a, new MyListIterator(list.header));
+ list.insert(b, new MyListIterator(list.header));
+ assertEquals(2, list.size());
+
+ Contact c = new Contact();
+ list.insert(c, new MyListIterator(list.header));
+ assertEquals(3, list.size());
+ }
+
+ @Test // This one you must compare the print result with what you think is
+ // correct
+ public void testPrintList() throws Exception {
+ System.out.println("Test empty list.");
+ LinkedList list = new LinkedList();
+ list.printList(); // expect "The list has no data."
+
+ Contact a = new Contact();
+ a.name = "Ann";
+ a.email = "ann@gmail.com";
+ a.phone = "0812345678";
+
+ Contact b = new Contact();
+ b.name = "Kate";
+ b.email = "kato@gmail.com";
+ b.phone = "0856723232";
+
+ list.insert(a, new MyListIterator(list.header));
+ list.insert(b, new MyListIterator(list.header));
+ System.out.println("Test list with 2 data.");
+ list.printList(); // expect Kate info and then Ann info.
+
+ }
+
+ @Test
+ public void testFindPosition() throws Exception {
+
+ LinkedList list = new LinkedList();
+ Iterator i = list.findPosition("Kate");
+ assertNull(i);
+
+ Contact ann = new Contact();
+ ann.name = "Ann";
+ ann.email = "ann@gmail.com";
+ ann.phone = "0812345678";
+
+ Contact kate = new Contact();
+ kate.name = "Kate";
+ kate.email = "kato@gmail.com";
+ kate.phone = "0856723232";
+
+ Contact jane = new Contact();
+ jane.name = "Jane";
+ jane.email = "jane@gmail.com";
+ jane.phone = "0802340078";
+
+ Contact jojo = new Contact();
+ jojo.name = "Jojo";
+ jojo.email = "jojo@gmail.com";
+ jojo.phone = "0836527732";
+ list.insert(jojo, new MyListIterator(list.header));
+ list.insert(jane, new MyListIterator(list.header));
+ list.insert(ann, new MyListIterator(list.header));
+ list.insert(kate, new MyListIterator(list.header));
+
+ MyListIterator itr = (MyListIterator) (list.findPosition("Jojo"));
+ Contact d = (Contact) (itr.currentNode.data);
+ assertEquals("Jojo", d.name);
+ assertEquals("jojo@gmail.com", d.email);
+ assertEquals("0836527732", d.phone);
+
+ MyListIterator itr2 = (MyListIterator) (list.findPosition("Ann"));
+ Contact d2 = (Contact) (itr2.currentNode.data);
+ assertEquals("Ann", d2.name);
+ assertEquals("ann@gmail.com", d2.email);
+ assertEquals("0812345678", d2.phone);
+
+ MyListIterator itr3 = (MyListIterator) (list.findPosition("Tora"));
+ assertNull(itr3);
+
+ }
+
+ @Test
+ public void testAdd() throws Exception {
+ LinkedList list = new LinkedList();
+
+ Contact ann = new Contact();
+ ann.name = "Ann";
+ ann.email = "ann@gmail.com";
+ ann.phone = "0812345678";
+
+ Contact kate = new Contact();
+ kate.name = "Kate";
+ kate.email = "kato@gmail.com";
+ kate.phone = "0856723232";
+
+ Contact jane = new Contact();
+ jane.name = "Jane";
+ jane.email = "jane@gmail.com";
+ jane.phone = "0802340078";
+
+ Contact jojo = new Contact();
+ jojo.name = "Jojo";
+ jojo.email = "jojo@gmail.com";
+ jojo.phone = "0836527732";
+
+ list.add(jojo);
+ Contact data = (Contact) (list.header.nextNode.data);
+ assertEquals("Jojo", data.name);
+ assertEquals("jojo@gmail.com", data.email);
+ assertEquals("0836527732", data.phone);
+
+ list.add(kate);
+ list.add(ann);
+ list.add(jane);
+
+ Contact first = (Contact) (list.header.nextNode.data);
+ assertEquals("Ann", first.name);
+ assertEquals("ann@gmail.com", first.email);
+ assertEquals("0812345678", first.phone);
+
+ Contact second = (Contact) (list.header.nextNode.nextNode.data);
+ assertEquals("Jane", second.name);
+ assertEquals("jane@gmail.com", second.email);
+ assertEquals("0802340078", second.phone);
+
+ Contact third = (Contact) (list.header.nextNode.nextNode.nextNode.data);
+ assertEquals("Jojo", third.name);
+ assertEquals("jojo@gmail.com", third.email);
+ assertEquals("0836527732", third.phone);
+
+ Contact forth = (Contact) (list.header.nextNode.nextNode.nextNode.nextNode.data);
+ assertEquals("Kate", forth.name);
+ assertEquals("kato@gmail.com", forth.email);
+ assertEquals("0856723232", forth.phone);
+
+ assertNull(list.header.nextNode.nextNode.nextNode.nextNode.nextNode);
+
+ }
+
+ @Test
+ public void testremoveAt() throws Exception {
+ LinkedList list = new LinkedList();
+
+ Contact ann = new Contact();
+ ann.name = "Ann";
+ ann.email = "ann@gmail.com";
+ ann.phone = "0812345678";
+
+ Contact kate = new Contact();
+ kate.name = "Kate";
+ kate.email = "kato@gmail.com";
+ kate.phone = "0856723232";
+
+ Contact jane = new Contact();
+ jane.name = "Jane";
+ jane.email = "jane@gmail.com";
+ jane.phone = "0802340078";
+
+ Contact jojo = new Contact();
+ jojo.name = "Jojo";
+ jojo.email = "jojo@gmail.com";
+ jojo.phone = "0836527732";
+
+
+ assertNull(null,list.removeAt(null));
+
+ MyListIterator itr = new MyListIterator(list.header.nextNode);
+ assertNull(null,list.removeAt(itr));
+
+ itr = new MyListIterator(list.header);
+ assertNull(null,list.removeAt(itr));
+
+
+ list.insert(kate, new MyListIterator(list.header));
+ list.insert(jojo, new MyListIterator(list.header));
+ list.insert(jane, new MyListIterator(list.header));
+ list.insert(ann, new MyListIterator(list.header));
+
+ LinkedList list2 = new LinkedList();
+
+ MyListIterator itr2 = new MyListIterator(list2.header);
+ assertNull(null,list.removeAt(itr2));
+
+ itr.next();
+ itr.next();
+
+
+ //remove jane
+ MyListIterator j1 = (MyListIterator)(list.removeAt(itr));
+ Contact c = (Contact)(j1.currentNode.data);
+ assertEquals("Jojo", c.name);
+ assertEquals("jojo@gmail.com", c.email);
+ assertEquals("0836527732", c.phone);
+
+
+ Contact first = (Contact) (list.header.nextNode.data);
+ assertEquals("Ann", first.name);
+ assertEquals("ann@gmail.com", first.email);
+ assertEquals("0812345678", first.phone);
+
+ Contact second = (Contact) (list.header.nextNode.nextNode.data);
+ assertEquals("Jojo", second.name);
+ assertEquals("jojo@gmail.com", second.email);
+ assertEquals("0836527732", second.phone);
+
+ Contact third = (Contact) (list.header.nextNode.nextNode.nextNode.data);
+ assertEquals("Kate", third.name);
+ assertEquals("kato@gmail.com", third.email);
+ assertEquals("0856723232", third.phone);
+
+ assertEquals(3,list.size());
+
+
+ //remove kate
+ j1.next();
+ j1 = (MyListIterator)(list.removeAt(j1));
+ c = (Contact)(j1.currentNode.data);
+ assertEquals("Ann", c.name);
+ assertEquals("ann@gmail.com", c.email);
+ assertEquals("0812345678", c.phone);
+
+ first = (Contact) (list.header.nextNode.data);
+ assertEquals("Ann", first.name);
+ assertEquals("ann@gmail.com", first.email);
+ assertEquals("0812345678", first.phone);
+
+ second = (Contact) (list.header.nextNode.nextNode.data);
+ assertEquals("Jojo", second.name);
+ assertEquals("jojo@gmail.com", second.email);
+ assertEquals("0836527732", second.phone);
+
+ assertEquals(2,list.size());
+
+
+ // remove ann
+ j1 = (MyListIterator)(list.removeAt(j1));
+ c = (Contact)(j1.currentNode.data);
+ assertEquals("Jojo", c.name);
+ assertEquals("jojo@gmail.com", c.email);
+ assertEquals("0836527732", c.phone);
+
+ first = (Contact) (list.header.nextNode.data);
+ assertEquals("Jojo", first.name);
+ assertEquals("jojo@gmail.com", first.email);
+ assertEquals("0836527732", first.phone);
+
+ assertEquals(1,list.size());
+
+ //remove jojo
+ j1 = (MyListIterator)(list.removeAt(j1));
+ assertEquals(list.header, j1.currentNode);
+ assertEquals(0,list.size());
+
+
+
+
+ }
+
+}