Repository: TheAlgorithms/Java
Pull request #5153 (2024-05-10 19:58:44)

style: make `SubsetCount` a proper utility
<!--
Thank you for your contribution!
In order to reduce the number of notifications sent to the maintainers, please:
- create your PR as draft, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/about-pull-requests#draft-pull-requests,
- make sure that all of the CI checks pass,
- mark your PR as ready for review, cf. https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-stage-of-a-pull-request#marking-a-pull-request-as-ready-for-review
-->

This change will allow to _include_ `MS_SHOULD_BE_FINAL`. Continuation of #5122.

<!-- For completed items, change [ ] to [x] -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized it.
- [x] All filenames are in PascalCase.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
- [x] All new code is formatted with `clang-format -i --style=file path/to/your/file.java`

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5153?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 38.24%. Comparing base [(`52f15b2`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/52f15b2b082f617c93bf43c6a337ac993d8b18fa?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`482a974`)](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5153?dropdown=coverage&src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff              @@
##             master    #5153      +/-   ##
============================================
- Coverage     38.26%   38.24%   -0.02%     
+ Complexity     2362     2360       -2     
============================================
  Files           516      516              
  Lines         15310    15309       -1     
  Branches       2958     2958              
============================================
- Hits           5858     5855       -3     
- Misses         9165     9166       +1     
- Partials        287      288       +1     
```



</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5153?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).


Patch:
diff --git a/spotbugs-exclude.xml b/spotbugs-exclude.xml
index 0e8a67c7..1f53feaf 100644
--- a/spotbugs-exclude.xml
+++ b/spotbugs-exclude.xml
@@ -74,9 +74,6 @@
     <Match>
         <Bug pattern="DM_BOXED_PRIMITIVE_FOR_PARSING" />
     </Match>
-    <Match>
-        <Bug pattern="MS_SHOULD_BE_FINAL" />
-    </Match>
     <Match>
         <Bug pattern="UWF_UNWRITTEN_FIELD" />
     </Match>
diff --git a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
index ef1c6c8f..af31294f 100644
--- a/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
+++ b/src/main/java/com/thealgorithms/dynamicprogramming/SubsetCount.java
@@ -6,7 +6,9 @@ package com.thealgorithms.dynamicprogramming;
  * StackOverflow(https://stackoverflow.com/questions/22891076/count-number-of-subsets-with-sum-equal-to-k)
  * @author Samrat Podder(https://github.com/samratpodder)
  */
-public class SubsetCount {
+public final class SubsetCount {
+    private SubsetCount() {
+    }
 
     /**
      * Dynamic Programming Implementation.
@@ -16,7 +18,7 @@ public class SubsetCount {
      * @param target is the sum of each element of the subset taken together
      *
      */
-    public int getCount(int[] arr, int target) {
+    public static int getCount(int[] arr, int target) {
         /**
          * Base Cases - If target becomes zero, we have reached the required sum for the subset
          * If we reach the end of the array arr then, either if target==arr[end], then we add one to
@@ -46,7 +48,7 @@ public class SubsetCount {
      * @param arr is the input array on which subsets are  to searched
      * @param target is the sum of each element of the subset taken together
      */
-    public int getCountSO(int[] arr, int target) {
+    public static int getCountSO(int[] arr, int target) {
         int n = arr.length;
         int[] prev = new int[target + 1];
         prev[0] = 1;


Test patch:
diff --git a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
index 13f7b6f9..c76f89de 100644
--- a/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
+++ b/src/test/java/com/thealgorithms/dynamicprogramming/SubsetCountTest.java
@@ -5,27 +5,25 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import org.junit.jupiter.api.Test;
 
 public class SubsetCountTest {
-    public static SubsetCount obj = new SubsetCount();
-
     @Test
     void hasMultipleSubset() {
         int[] arr = new int[] {1, 2, 3, 3};
-        assertEquals(3, obj.getCount(arr, 6));
+        assertEquals(3, SubsetCount.getCount(arr, 6));
     }
     @Test
     void singleElementSubset() {
         int[] arr = new int[] {1, 1, 1, 1};
-        assertEquals(4, obj.getCount(arr, 1));
+        assertEquals(4, SubsetCount.getCount(arr, 1));
     }
 
     @Test
     void hasMultipleSubsetSO() {
         int[] arr = new int[] {1, 2, 3, 3};
-        assertEquals(3, obj.getCountSO(arr, 6));
+        assertEquals(3, SubsetCount.getCountSO(arr, 6));
     }
     @Test
     void singleSubsetSO() {
         int[] arr = new int[] {1, 1, 1, 1};
-        assertEquals(1, obj.getCountSO(arr, 4));
+        assertEquals(1, SubsetCount.getCountSO(arr, 4));
     }
 }