From d1a252130a4fd43ef1522100a497b3acc2ac23f8 Mon Sep 17 00:00:00 2001
From: WinJ <winstonjodjana@gmail.com>
Date: Mon, 27 Feb 2023 14:47:14 -0800
Subject: [PATCH] cleaned up to be consistent and less confusing

---
 src/main/java/CountStrs.java           | 52 +++++++++----------
 src/main/java/LessThan7.java           | 49 +++++++++---------
 src/main/java/Parity.java              | 42 ++++++++-------
 src/main/java/PowMod.java              | 47 ++++++++---------
 src/main/java/SecondSmallest.java      | 71 +++++++++++---------------
 src/test/java/CountStrsTests.java      | 29 +++++++++--
 src/test/java/LessThan7Tests.java      | 16 ++++--
 src/test/java/ParityTests.java         | 30 +++++++++--
 src/test/java/PowModTests.java         | 21 ++++++--
 src/test/java/SecondSmallestTests.java | 25 +++++++--
 10 files changed, 222 insertions(+), 160 deletions(-)

diff --git a/src/main/java/CountStrs.java b/src/main/java/CountStrs.java
index d6826c1..f6ea4ed 100644
--- a/src/main/java/CountStrs.java
+++ b/src/main/java/CountStrs.java
@@ -2,54 +2,52 @@ import java.util.concurrent.ForkJoinPool;
 import java.util.concurrent.RecursiveTask;
 
 /**
- * countStrs returns the number of elements in arr that equal str.
+ * countStrs returns the number of elements in arr that equal targetStr.
  * For example, if arr is ["h", "ee", "llll", "llll", "oo", "llll"],
  * then countStrs(arr, "llll") == 3 and countStrs(arr, "h") == 1.
- *
  * Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
  */
 public class CountStrs {
     private static final ForkJoinPool POOL = new ForkJoinPool();
-    private static final int CUTOFF = 1;
-
-    public static int sequential(String[] arr, String str, int lo, int hi) {
-        // TODO: your code here
-
-
-
-
-        return 0; // TODO: you may want to change this
+    private static int CUTOFF;
+    private static String targetStr;
+
+    public static int parallelCountStrs(String[] arr, int cutoff, String targetStr) {
+        // TODO: Invoke the ForkJoinPool to call the LessThan7Task
+        CountStrs.CUTOFF = cutoff;
+        CountStrs.targetStr = targetStr;
+        return POOL.invoke(new CountStrsTask(arr, 0, arr.length));
     }
 
-    public static int parallel(String[] arr, String str) {
-        return 0; // TODO: you may want to change this
-    }
+    public static int sequentialCountStrs(String[] arr, int lo, int hi, String targetStr) {
+        // TODO: Step 1. Base Case (i.e. Sequential Case)
 
+        return 0; // TODO: you will want to change this
+    }
 
     private static class CountStrsTask extends RecursiveTask<Integer> {
-        String[] arr;
-        String str;
-        int lo, hi;
+        private final String[] arr;
+        private final int lo, hi;
 
-        public CountStrsTask(String[] arr, int lo, int hi, String str) {
+        public CountStrsTask(String[] arr, int lo, int hi) {
             this.arr = arr;
             this.lo = lo;
             this.hi = hi;
-            this.str = str;
         }
 
         @Override
         protected Integer compute() {
-            // TODO: your code here
-
-
-
-
-
+            if (hi - lo <= CountStrs.CUTOFF) {
+                // TODO: Step 1. Base Case (i.e. Sequential Case)
+                return 0; // TODO: you will want to change this
+            } else {
+                // TODO: Step 2. Recursive Case (i.e. Parallel/Forking case)
 
 
+                // TODO: Step 3. Combining the left and right tasks' results
 
-            return 0; // TODO: you may want to change this
+                return 0; // TODO: you will want to change this
+            }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/LessThan7.java b/src/main/java/LessThan7.java
index 59d50b9..fd5c6ce 100644
--- a/src/main/java/LessThan7.java
+++ b/src/main/java/LessThan7.java
@@ -4,48 +4,51 @@ import java.util.concurrent.RecursiveTask;
 /**
  * lessThan7 returns the number of elements in arr that are less than 7.
  * For example, if arr is [21, 7, 6, 8, 17, 1], then lessThan7(arr) == 2.
- *
  * Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
  */
 public class LessThan7 {
-    // Its static so that the compiler knows to not duplicate memory when we fork.
     private static final ForkJoinPool POOL = new ForkJoinPool();
-    private static final int CUTOFF = 1;
+    private static int CUTOFF;
 
-    public static int sequential(int[] arr, int lo, int hi) {
-        // TODO: your code here
-
-
-        return 0; // TODO: you may want to change this
+    public static int parallelLessThan7(int[] arr, int cutoff) {
+        // TODO: Invoke the ForkJoinPool to call the LessThan7Task
+        LessThan7.CUTOFF = cutoff;
+        return POOL.invoke(new LessThan7Task(arr, 0, arr.length));
     }
 
-    public static int parallel(int[] arr) {
-        return POOL.invoke(new LessThan7Task(arr, 0, arr.length));
+    public static int sequentialLessThan7(int[] arr, int lo, int hi) {
+        // TODO: Step 1. Base Case (i.e. Sequential Case)
+
+        return 0; // TODO: you will want to change this
     }
 
-    // Static for the same reasons above
     private static class LessThan7Task extends RecursiveTask<Integer> {
-        int[] arr;
-        int lo, hi;
-
+        private final int[] arr;
+        private final int lo, hi;
+        
         public LessThan7Task(int[] arr, int lo, int hi) {
             this.arr = arr;
             this.lo = lo;
             this.hi = hi;
         }
-
+        
         @Override
         protected Integer compute() {
-            // TODO: your code here
-
-
+            if (hi - lo <= LessThan7.CUTOFF) {
+                // TODO: Step 1. Base Case (i.e. Sequential Case)
+                return 0; // TODO: you will want to change this
+            } else {
+                // TODO: Step 2. Recursive Case (i.e. Parallel/Forking case)
+                // TODO: 1. Make sure to fork() the left task first
+                // TODO: 2. Then compute() the right task
+                // TODO: 3. Then wait for the leftResult by calling join()
+                // TODO:    on the left task before combining results
 
 
+                // TODO: Step 3. Combining the left and right tasks' results
 
-
-
-            return 0; // TODO: you may want to change this
+                return 0; // TODO: you will want to change this
+            }
         }
-
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/Parity.java b/src/main/java/Parity.java
index c250203..56d1033 100644
--- a/src/main/java/Parity.java
+++ b/src/main/java/Parity.java
@@ -5,30 +5,27 @@ import java.util.concurrent.RecursiveTask;
  * parity returns true if there are even number of even numbers and false otherwise.
  * For example, if arr is [1, 7, 4, 3, 6], then parity(arr) == true.
  * But, if arr is [6, 5, 4, 3, 2, 1], parity(arr) == false.
- *
  * Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
  */
 public class Parity {
     private static final ForkJoinPool POOL = new ForkJoinPool();
-    private static final int CUTOFF = 1;
+    private static int CUTOFF;
 
-    private static boolean sequential(int[] arr, int lo, int hi) {
-        // TODO: your code here
-
-
-
-
-
-        return false; // TODO: you may want to change this
+    public static boolean parallelParityTask(int[] arr, int cutoff) {
+        // TODO: Invoke the ForkJoinPool to call the LessThan7Task
+        Parity.CUTOFF = cutoff;
+        return POOL.invoke(new ParityTask(arr, 0, arr.length));
     }
 
-    public static boolean parallel(int[] arr) {
-        return false; // TODO: you may want to change this
+    public static boolean sequentialParityTask(int[] arr, int lo, int hi) {
+        // TODO: Step 1. Base Case (i.e. Sequential Case)
+
+        return true; // TODO: you will want to change this
     }
 
     private static class ParityTask extends RecursiveTask<Boolean> {
-        int[] arr;
-        int lo, hi;
+        private final int[] arr;
+        private final int lo, hi;
 
         public ParityTask(int[] arr, int lo, int hi) {
             this.arr = arr;
@@ -38,16 +35,17 @@ public class Parity {
 
         @Override
         protected Boolean compute() {
-            // TODO: your code here
-
-
-
-
-
+            if (hi - lo <= CUTOFF) {
+                // TODO: Step 1. Base Case (i.e. Sequential Case)
+                return true; // TODO: you will want to change this
+            } else {
+                // TODO: Step 2. Recursive Case (i.e. Parallel/Forking case)
 
 
+                // TODO: Step 3. Combining the left and right tasks' results
 
-            return false; // TODO: you may want to change this
+                return true; // TODO: you will want to change this
+            }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/PowMod.java b/src/main/java/PowMod.java
index dd77cc8..48f45ec 100644
--- a/src/main/java/PowMod.java
+++ b/src/main/java/PowMod.java
@@ -1,4 +1,3 @@
-import java.util.Arrays;
 import java.util.concurrent.ForkJoinPool;
 import java.util.concurrent.RecursiveAction;
 
@@ -6,50 +5,46 @@ import java.util.concurrent.RecursiveAction;
  * powMod replaces every element of arr with arr[i]^p mod m.
  * For example, if arr is [1, 7, 4, 3, 6], then powmod(arr, 2, 5)
  * would result in arr = [1, 4, 1, 4, 1].
- *
  * Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
  */
 public class PowMod {
     private static final ForkJoinPool POOL = new ForkJoinPool();
-    private static final int CUTOFF = 1;
-
-    public static void sequential(int[] arr, int pow, int mod, int lo, int hi) {
-        // TODO: your code here
-
-
-
-
-
-
+    private static int CUTOFF;
+    private static int pow, mod;
+
+    public static void parallelPowMod(int[] arr, int cutoff, int pow, int mod) {
+        // TODO: Invoke the ForkJoinPool to call the LessThan7Task
+        CUTOFF = cutoff;
+        PowMod.pow = pow;
+        PowMod.mod = mod;
+        POOL.invoke(new PowModTask(arr, 0, arr.length));
     }
 
-    public static void parallel(int[] arr, int pow, int mod) {
-        // TODO: your code here
+    public static void sequentialPowMod(int[] arr, int lo, int hi, int pow, int mod) {
+        // TODO: Step 1. Base Case (i.e. Sequential Case)
+
     }
 
     private static class PowModTask extends RecursiveAction {
-        int[] arr;
-        int lo, hi;
-        int pow, mod;
+        private final int[] arr;
+        private final int lo, hi;
 
-        public PowModTask(int[] arr, int pow, int mod, int lo, int hi) {
+        public PowModTask(int[] arr, int lo, int hi) {
             this.arr = arr;
             this.lo = lo;
             this.hi = hi;
-            this.pow = pow;
-            this.mod = mod;
         }
 
         @Override
         protected void compute() {
-            // TODO: your code here
-
-
-
-
+            if (hi - lo <= PowMod.CUTOFF) {
+                // TODO: Step 1. Base Case (i.e. Sequential Case)
 
+            } else {
+                // TODO: Step 2. Recursive Case (i.e. Parallel/Forking case)
 
 
+            }
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/main/java/SecondSmallest.java b/src/main/java/SecondSmallest.java
index 0bf9d0e..86d3690 100644
--- a/src/main/java/SecondSmallest.java
+++ b/src/main/java/SecondSmallest.java
@@ -1,55 +1,34 @@
+import java.util.Arrays;
 import java.util.concurrent.ForkJoinPool;
 import java.util.concurrent.RecursiveTask;
-import java.util.Arrays;
 
 /**
- * secondSmallest returns the second smallest unique element of arr.
- * Assume arr contains at least two unique elements.
+ * secondSmallest returns the second-smallest integer in arr.
+ * Assume arr contains only unique elements and has at least 2 integers.
  * For example, if arr is [1, 7, 4, 3, 6], then secondSmallest(arr) == 3.
- * But, if arr is [6, 1, 4, 3, 5, 2, 1], secondSmallest(arr) == 2.
- *
+ * But, if arr is [6, 1, 4, 3, 5, 2], secondSmallest(arr) == 2.
  * Your code must have O(n) work, O(lg(n)) span, where n is the length of arr
  */
 public class SecondSmallest {
     private static final ForkJoinPool POOL = new ForkJoinPool();
-    private static final int CUTOFF = 1;
-
-    private static class TwoSmallest {
-        int smallest, secondSmallest;
-        public TwoSmallest() {
-            smallest = secondSmallest = Integer.MAX_VALUE;
-        }
-    }
+    private static int CUTOFF;
 
-    public static TwoSmallest combine(TwoSmallest a, TwoSmallest b) {
-        // TODO: your code here
-
-
-
-
-
-
-        return new TwoSmallest(); // TODO: you may want to change this
+    public static int parallelSecondSmallest(int[] arr, int cutoff) {
+        // TODO: Invoke the ForkJoinPool to call the LessThan7Task
+        SecondSmallest.CUTOFF = cutoff;
+        TwoSmallest result = POOL.invoke(new SecondSmallestTask(arr, 0, arr.length));
+        return result.secondSmallest;
     }
 
-    public static TwoSmallest sequential(int[] arr, int lo, int hi) {
-        // TODO: your code here
-
-
-
-
-
-    	return new TwoSmallest(); // TODO: you may want to change this
-    }
+    public static TwoSmallest sequentialSecondSmallest(int[] arr, int lo, int hi) {
+        // TODO: Step 1. Base Case (i.e. Sequential Case)
 
-    public static int parallel(int[] arr) {
-        TwoSmallest result = POOL.invoke(new SecondSmallestTask(arr, 0, arr.length));
-        return result.secondSmallest;
+        return new TwoSmallest(); // TODO: you will want to change this
     }
 
     private static class SecondSmallestTask extends RecursiveTask<TwoSmallest> {
-        int[] arr;
-        int lo, hi;
+        private final int[] arr;
+        private final int lo, hi;
 
         public SecondSmallestTask(int[] arr, int lo, int hi) {
             this.arr = arr;
@@ -59,14 +38,26 @@ public class SecondSmallest {
 
         @Override
         protected TwoSmallest compute() {
-            // TODO: your code here
-
+            if (hi - lo <= SecondSmallest.CUTOFF) {
+                // TODO: Step 1. Base Case (i.e. Sequential Case)
+                return new TwoSmallest(); // TODO: you will want to change this
+            } else {
+                // TODO: Step 2. Recursive Case (i.e. Parallel/Forking case)
 
 
+                // TODO: Step 3. Combining the left and right tasks' results
 
+                return new TwoSmallest(); // TODO: you will want to change this
+            }
+        }
+    }
 
+    public static class TwoSmallest {
+        public int smallest;
+        public int secondSmallest;
 
-            return new TwoSmallest(); // TODO: you may want to change this
+        public TwoSmallest() {
+            smallest = secondSmallest = Integer.MAX_VALUE;
         }
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/CountStrsTests.java b/src/test/java/CountStrsTests.java
index 52db657..4a6c3df 100644
--- a/src/test/java/CountStrsTests.java
+++ b/src/test/java/CountStrsTests.java
@@ -1,13 +1,32 @@
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 public class CountStrsTests {
+    @Test
+    public void testCountStrsSequential1() {
+        String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
+        assertEquals(1, CountStrs.sequentialCountStrs(arr, 0, arr.length, "h"));
+    }
+
+    @Test
+    public void testCountStrsSequential2() {
+        String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
+        assertEquals(1, CountStrs.sequentialCountStrs(arr, 0, arr.length, "h"));
+    }
+
+    @Test
+    public void testCountStrsParallel1() {
+        String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        assertEquals(3, CountStrs.parallelCountStrs(arr, cutoff, "llll"));
+        assertEquals(1, CountStrs.parallelCountStrs(arr, cutoff, "h"));
+    }
 
     @Test
-    public void testCountStrs() {
+    public void testCountStrsParallel2() {
         String[] arr = {"h", "ee", "llll", "llll", "oo", "llll"};
-        assertEquals(3, CountStrs.parallel(arr, "llll"));
-        assertEquals(1, CountStrs.parallel(arr, "h"));
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        assertEquals(1, CountStrs.parallelCountStrs(arr, cutoff, "h"));
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/LessThan7Tests.java b/src/test/java/LessThan7Tests.java
index 61b0542..0805bf1 100644
--- a/src/test/java/LessThan7Tests.java
+++ b/src/test/java/LessThan7Tests.java
@@ -1,11 +1,19 @@
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 public class LessThan7Tests {
 
     @Test
-    public void testLessThan7() {
-        assertEquals(4, LessThan7.parallel(new int[]{21, 7, 6, 8, 17, 1, 7, 7, 1, 1, 7}));
+    public void testSequentialLessThan7() {
+        int[] arr = new int[]{21, 7, 6, 8, 17, 1, 7, 7, 1, 1, 7};
+        assertEquals(4, LessThan7.sequentialLessThan7(arr, 0, arr.length));
     }
-}
+
+    @Test
+    public void testParallelLessThan7() {
+        int[] arr = new int[]{21, 7, 6, 8, 17, 1, 7, 7, 1, 1, 7};
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        assertEquals(4, LessThan7.parallelLessThan7(arr, cutoff));
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/ParityTests.java b/src/test/java/ParityTests.java
index 99a3e6b..3f39857 100644
--- a/src/test/java/ParityTests.java
+++ b/src/test/java/ParityTests.java
@@ -1,12 +1,32 @@
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 public class ParityTests {
+    @Test
+    public void testParitySequentialTrue() {
+        int[] arr = new int[]{1, 7, 4, 3, 6};
+        assertTrue(Parity.sequentialParityTask(arr, 0, arr.length));
+    }
+
+    @Test
+    public void testParitySequentialFalse() {
+        int[] arr = new int[]{6, 5, 4, 3, 2, 1};
+        assertFalse(Parity.sequentialParityTask(arr, 0, arr.length));
+    }
+
+    @Test
+    public void testParityParallelTrue() {
+        int[] arr = new int[]{1, 7, 4, 3, 6};
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        assertTrue(Parity.parallelParityTask(arr, cutoff));
+    }
 
     @Test
-    public void testParity() {
-        assertTrue(Parity.parallel(new int[]{1, 7, 4, 3, 6}));
-        assertFalse(Parity.parallel(new int[]{6, 5, 4, 3, 2, 1}));
+    public void testParityParallelFalse() {
+        int[] arr = new int[]{6, 5, 4, 3, 2, 1};
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        assertFalse(Parity.parallelParityTask(arr, cutoff));
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/PowModTests.java b/src/test/java/PowModTests.java
index 4ce8f7f..ca4581d 100644
--- a/src/test/java/PowModTests.java
+++ b/src/test/java/PowModTests.java
@@ -1,15 +1,28 @@
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertArrayEquals;
 
 public class PowModTests {
+    @Test
+    public void testPowModSequential() {
+        int[] arr = {1, 7, 4, 3, 6};
+        int pow = 6;
+        int mod = 5000;
+        int[] expected = {1, 2649, 4096, 729, 1656};
+
+        PowMod.sequentialPowMod(arr, 0, arr.length, pow, mod);
+        assertArrayEquals(expected, arr);
+    }
 
     @Test
-    public void testPowMod() {
+    public void testPowModParallel() {
         int[] arr = {1, 7, 4, 3, 6};
+        int cutoff = 1; // Putting the cutoff as 1 makes it FULLY parallel
+        int pow = 6;
+        int mod = 5000;
         int[] expected = {1, 2649, 4096, 729, 1656};
 
-        PowMod.parallel(arr, 6, 5000);
+        PowMod.parallelPowMod(arr, cutoff, pow, mod);
         assertArrayEquals(expected, arr);
     }
-}
+}
\ No newline at end of file
diff --git a/src/test/java/SecondSmallestTests.java b/src/test/java/SecondSmallestTests.java
index 5d98e98..8b5956a 100644
--- a/src/test/java/SecondSmallestTests.java
+++ b/src/test/java/SecondSmallestTests.java
@@ -1,12 +1,29 @@
 import org.junit.Test;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 public class SecondSmallestTests {
+    @Test
+    public void testSecondSmallestSequential1() {
+        int[] arr = new int[]{1, 7, 4, 3, 6};
+        assertEquals(3, SecondSmallest.sequentialSecondSmallest(arr, 0, arr.length).secondSmallest);
+    }
+
+    @Test
+    public void testSecondSmallestSequential2() {
+        int[] arr = new int[]{6, 1, 4, 3, 5, 2};
+        assertEquals(2, SecondSmallest.sequentialSecondSmallest(arr, 0, arr.length).secondSmallest);
+    }
+
+    @Test
+    public void testSecondSmallestParallel1() {
+        int[] arr = new int[]{1, 7, 4, 3, 6};
+        assertEquals(3, SecondSmallest.parallelSecondSmallest(arr, 1));
+    }
 
     @Test
-    public void testSecondSmallest() {
-        assertEquals(3, SecondSmallest.parallel(new int[]{1, 7, 4, 3, 6}));
-        assertEquals(2, SecondSmallest.parallel(new int[]{6, 1, 4, 3, 5, 2, 1}));
+    public void testSecondSmallestParallel2() {
+        int[] arr = new int[]{6, 1, 4, 3, 5, 2};
+        assertEquals(2, SecondSmallest.parallelSecondSmallest(arr, 1));
     }
 }
-- 
GitLab