Repository: TheAlgorithms/Java
Pull request #5773 (2024-10-13 11:00:42)

Add tests, remove main in PalindromePrime
<!--
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
-->

<!-- 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/5773?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) Report
Attention: Patch coverage is `93.33333%` with `1 line` in your changes missing coverage. Please review.
> Project coverage is 64.27%. Comparing base [(`596c614`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/596c6147af0af91d1ca8990ed91820df1fe3b8ea?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`22f735a`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/22f735a0067f993ffbe0a307c204f297c121d6a2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms).
> Report is 2 commits behind head on master.

| [Files with missing lines](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5773?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) | Patch % | Lines |
|---|---|---|
| [...n/java/com/thealgorithms/misc/PalindromePrime.java](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5773?src=pr&el=tree&filepath=src%2Fmain%2Fjava%2Fcom%2Fthealgorithms%2Fmisc%2FPalindromePrime.java&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms#diff-c3JjL21haW4vamF2YS9jb20vdGhlYWxnb3JpdGhtcy9taXNjL1BhbGluZHJvbWVQcmltZS5qYXZh) | 93.33% | [0 Missing and 1 partial :warning: ](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5773?src=pr&el=tree&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    #5773      +/-   ##
============================================
+ Coverage     64.05%   64.27%   +0.22%     
- Complexity     4177     4209      +32     
============================================
  Files           584      586       +2     
  Lines         16332    16368      +36     
  Branches       3152     3162      +10     
============================================
+ Hits          10461    10521      +60     
+ Misses         5445     5420      -25     
- Partials        426      427       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5773?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/DIRECTORY.md b/DIRECTORY.md
index 11ce5b74..d0de7071 100644
--- a/DIRECTORY.md
+++ b/DIRECTORY.md
@@ -304,6 +304,7 @@
             * [WildcardMatching](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WildcardMatching.java)
             * [WineProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/WineProblem.java)
           * geometry
+            * [BresenhamLine](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/BresenhamLine.java)
             * [ConvexHull](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/ConvexHull.java)
             * [GrahamScan](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/GrahamScan.java)
             * [Point](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/geometry/Point.java)
@@ -902,6 +903,7 @@
             * [WildcardMatchingTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WildcardMatchingTest.java)
             * [WineProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/WineProblemTest.java)
           * geometry
+            * [BresenhamLineTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/BresenhamLineTest.java)
             * [ConvexHullTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/ConvexHullTest.java)
             * [GrahamScanTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/geometry/GrahamScanTest.java)
           * greedyalgorithms
@@ -1026,6 +1028,7 @@
             * [MedianOfMatrixtest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfMatrixtest.java)
             * [MedianOfRunningArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MedianOfRunningArrayTest.java)
             * [MirrorOfMatrixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/MirrorOfMatrixTest.java)
+            * [PalindromePrimeTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java)
             * [PalindromeSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/PalindromeSinglyLinkedListTest.java)
             * [RangeInSortedArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/RangeInSortedArrayTest.java)
             * [ThreeSumProblemTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/misc/ThreeSumProblemTest.java)
diff --git a/src/main/java/com/thealgorithms/misc/PalindromePrime.java b/src/main/java/com/thealgorithms/misc/PalindromePrime.java
index e1cbf3ff..164e957a 100644
--- a/src/main/java/com/thealgorithms/misc/PalindromePrime.java
+++ b/src/main/java/com/thealgorithms/misc/PalindromePrime.java
@@ -1,51 +1,57 @@
 package com.thealgorithms.misc;
 
-import java.util.Scanner;
+import java.util.ArrayList;
+import java.util.List;
 
 public final class PalindromePrime {
     private PalindromePrime() {
     }
 
-    public static void main(String[] args) { // Main function
-        Scanner in = new Scanner(System.in);
-        System.out.println("Enter the quantity of First Palindromic Primes you want");
-        int n = in.nextInt(); // Input of how many first palindromic prime we want
-        functioning(n); // calling function - functioning
-        in.close();
-    }
+    public static boolean prime(int num) {
+        if (num < 2) {
+            return false; // Handle edge case for numbers < 2
+        }
+        if (num == 2) {
+            return true; // 2 is prime
+        }
+        if (num % 2 == 0) {
+            return false; // Even numbers > 2 are not prime
+        }
 
-    public static boolean prime(int num) { // checking if number is prime or not
         for (int divisor = 3; divisor <= Math.sqrt(num); divisor += 2) {
             if (num % divisor == 0) {
-                return false; //  false if not prime
+                return false;
             }
         }
-        return true; // True if prime
+        return true;
     }
 
-    public static int reverse(int n) { //  Returns  the reverse of the number
+    public static int reverse(int n) {
         int reverse = 0;
         while (n != 0) {
-            reverse *= 10;
-            reverse += n % 10;
+            reverse = reverse * 10 + (n % 10);
             n /= 10;
         }
         return reverse;
     }
 
-    public static void functioning(int y) {
-        if (y == 0) {
-            return;
+    public static List<Integer> generatePalindromePrimes(int n) {
+        List<Integer> palindromicPrimes = new ArrayList<>();
+        if (n <= 0) {
+            return palindromicPrimes; // Handle case for 0 or negative input
         }
-        System.out.print(2 + "\n"); // print the first Palindromic Prime
+
+        palindromicPrimes.add(2); // 2 is the first palindromic prime
         int count = 1;
         int num = 3;
-        while (count < y) {
-            if (num == reverse(num) && prime(num)) { // number is prime and it's reverse is same
-                count++; // counts check when to terminate while loop
-                System.out.print(num + "\n"); // print the Palindromic Prime
+
+        while (count < n) {
+            if (num == reverse(num) && prime(num)) {
+                palindromicPrimes.add(num);
+                count++;
             }
-            num += 2; // inrease iterator value by two
+            num += 2; // Skip even numbers
         }
+        return palindromicPrimes;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java b/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java
new file mode 100644
index 00000000..130cd19b
--- /dev/null
+++ b/src/test/java/com/thealgorithms/misc/PalindromePrimeTest.java
@@ -0,0 +1,53 @@
+package com.thealgorithms.misc;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class PalindromePrimeTest {
+
+    @Test
+    public void testPrimeWithPrimeNumbers() {
+        assertTrue(PalindromePrime.prime(2), "2 should be prime");
+        assertTrue(PalindromePrime.prime(3), "3 should be prime");
+        assertTrue(PalindromePrime.prime(5), "5 should be prime");
+        assertTrue(PalindromePrime.prime(11), "11 should be prime");
+    }
+
+    @Test
+    public void testPrimeWithNonPrimeNumbers() {
+        assertFalse(PalindromePrime.prime(1), "1 is not prime");
+        assertFalse(PalindromePrime.prime(4), "4 is not prime");
+        assertFalse(PalindromePrime.prime(9), "9 is not prime");
+        assertFalse(PalindromePrime.prime(15), "15 is not prime");
+    }
+
+    @Test
+    public void testReverse() {
+        assertEquals(123, PalindromePrime.reverse(321), "Reverse of 321 should be 123");
+        assertEquals(7, PalindromePrime.reverse(7), "Reverse of 7 should be 7");
+        assertEquals(1221, PalindromePrime.reverse(1221), "Reverse of 1221 should be 1221");
+    }
+
+    @Test
+    public void testGeneratePalindromePrimes() {
+        List<Integer> result = PalindromePrime.generatePalindromePrimes(5);
+        List<Integer> expected = List.of(2, 3, 5, 7, 11);
+        assertEquals(expected, result, "The first 5 palindromic primes should be [2, 3, 5, 7, 11]");
+    }
+
+    @Test
+    public void testGeneratePalindromePrimesWithZero() {
+        List<Integer> result = PalindromePrime.generatePalindromePrimes(0);
+        assertTrue(result.isEmpty(), "Generating 0 palindromic primes should return an empty list");
+    }
+
+    @Test
+    public void testGeneratePalindromePrimesWithNegativeInput() {
+        List<Integer> result = PalindromePrime.generatePalindromePrimes(-5);
+        assertTrue(result.isEmpty(), "Generating a negative number of palindromic primes should return an empty list");
+    }
+}