Repository: TheAlgorithms/Java
Pull request #5367 (2024-08-22 19:34:33)

refactor: `FloydTriangle`
Improving readability.
Adding tests.

<!--
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/5367?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 45.93%. Comparing base [(`74b05ef`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/74b05ef7c2bf34f2d1acda5f75fdbd8653e728c1?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=TheAlgorithms) to head [(`d7ea443`)](https://app.codecov.io/gh/TheAlgorithms/Java/commit/d7ea4438f9fd44db693bbe3064eeef7a74d2b0de?dropdown=coverage&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    #5367      +/-   ##
============================================
+ Coverage     45.88%   45.93%   +0.05%     
- Complexity     2857     2860       +3     
============================================
  Files           525      525              
  Lines         15418    15416       -2     
  Branches       2917     2918       +1     
============================================
+ Hits           7074     7081       +7     
+ Misses         8048     8038      -10     
- Partials        296      297       +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/TheAlgorithms/Java/pull/5367?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/src/main/java/com/thealgorithms/others/FloydTriangle.java b/src/main/java/com/thealgorithms/others/FloydTriangle.java
index fbeaec33..dff48443 100644
--- a/src/main/java/com/thealgorithms/others/FloydTriangle.java
+++ b/src/main/java/com/thealgorithms/others/FloydTriangle.java
@@ -1,22 +1,30 @@
 package com.thealgorithms.others;
 
-import java.util.Scanner;
+import java.util.ArrayList;
+import java.util.List;
 
 final class FloydTriangle {
     private FloydTriangle() {
     }
 
-    public static void main(String[] args) {
-        Scanner sc = new Scanner(System.in);
-        System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
-        int r = sc.nextInt();
-        int n = 0;
-        sc.close();
-        for (int i = 0; i < r; i++) {
+    /**
+     * Generates a Floyd Triangle with the specified number of rows.
+     *
+     * @param rows The number of rows in the triangle.
+     * @return A List representing the Floyd Triangle.
+     */
+    public static List<List<Integer>> generateFloydTriangle(int rows) {
+        List<List<Integer>> triangle = new ArrayList<>();
+        int number = 1;
+
+        for (int i = 0; i < rows; i++) {
+            List<Integer> row = new ArrayList<>();
             for (int j = 0; j <= i; j++) {
-                System.out.print(++n + " ");
+                row.add(number++);
             }
-            System.out.println();
+            triangle.add(row);
         }
+
+        return triangle;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/others/FloydTriangleTest.java b/src/test/java/com/thealgorithms/others/FloydTriangleTest.java
new file mode 100644
index 00000000..afa280c0
--- /dev/null
+++ b/src/test/java/com/thealgorithms/others/FloydTriangleTest.java
@@ -0,0 +1,46 @@
+package com.thealgorithms.others;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.util.Arrays;
+import java.util.List;
+import org.junit.jupiter.api.Test;
+
+public class FloydTriangleTest {
+
+    @Test
+    public void testGenerateFloydTriangleWithValidInput() {
+        List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6));
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(3));
+    }
+
+    @Test
+    public void testGenerateFloydTriangleWithOneRow() {
+        List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1));
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(1));
+    }
+
+    @Test
+    public void testGenerateFloydTriangleWithZeroRows() {
+        List<List<Integer>> expectedOutput = Arrays.asList();
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(0));
+    }
+
+    @Test
+    public void testGenerateFloydTriangleWithNegativeRows() {
+        List<List<Integer>> expectedOutput = Arrays.asList();
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(-3));
+    }
+
+    @Test
+    public void testGenerateFloydTriangleWithMultipleRows() {
+        List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15));
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(5));
+    }
+
+    @Test
+    public void testGenerateFloydTriangleWithMoreMultipleRows() {
+        List<List<Integer>> expectedOutput = Arrays.asList(Arrays.asList(1), Arrays.asList(2, 3), Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9, 10), Arrays.asList(11, 12, 13, 14, 15), Arrays.asList(16, 17, 18, 19, 20, 21), Arrays.asList(22, 23, 24, 25, 26, 27, 28));
+        assertEquals(expectedOutput, FloydTriangle.generateFloydTriangle(7));
+    }
+}