Repository: TheAlgorithms/Java
Pull request #3739 (2022-10-29 19:59:10)

Added functions in Perimeter.java
<!-- For completed items, change [ ] to [x] -->
Added functions in Perimeter.java and corrected functions names in PascalCase and removed main()
Added JUnit Tests for new functions and renamed perimterTest.java to PerimeterTest.java
- [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.



Patch:
diff --git a/src/main/java/com/thealgorithms/maths/Perimeter.java b/src/main/java/com/thealgorithms/maths/Perimeter.java
index 921538b1..a941de19 100644
--- a/src/main/java/com/thealgorithms/maths/Perimeter.java
+++ b/src/main/java/com/thealgorithms/maths/Perimeter.java
@@ -1,42 +1,56 @@
 package com.thealgorithms.maths;
 
+// Perimeter of different 2D geometrical shapes
 public class Perimeter {
 
-    public static void main(String[] args) {
-        System.out.println(perimeter_polygon(5, 4));
-        System.out.println(perimeter_rectangle(3, 4));
-        System.out.printf("%,3f", circumference(5));
+    /**
+     * Calculate the Perimeter of regular polygon (equals sides)
+     * Examples of regular polygon are Equilateral Triangle, Square, Regular Pentagon, Regular Hexagon. 
+     * 
+     * @param n for number of sides.
+     * @param side for length of each side.
+     * @return Perimeter of given polygon
+     */
+    public static float perimeterRegularPolygon(int n, float side) {
+        return n * side;
     }
 
-    // Perimeter of different 2D geometrical shapes
     /**
-     *Calculate the Perimeter of polygon.
-     * @parameter length of side.
-     * @parameter number of sides.
-     * @return Perimeter of given polygon
+     * Calculate the Perimeter of irregular polygon (unequals sides)
+     * Examples of irregular polygon are scalent triangle, irregular quadrilateral, irregular Pentagon, irregular Hexagon. 
+     * 
+     * @param side1 for length of side 1
+     * @param side2 for length of side 2
+     * @param side3 for length of side 3
+     * @param sides for length of remaining sides
+     * @return Perimeter of given trapezoid.
      */
-    public static float perimeter_polygon(int n, float side) {
-        float perimeter = n * side;
+    public static float perimeterIrregularPolygon(float side1, float side2, float side3, float... sides) {
+        float perimeter = side1 + side2 + side3;
+        for (float side : sides) {
+            perimeter += side;
+        }
         return perimeter;
     }
 
     /**
-     *Calculate the Perimeter of rectangle.
-     * @parameter length and breadth.
+     * Calculate the Perimeter of rectangle
+     * 
+     * @param length for length of rectangle
+     * @param breadth for breadth of rectangle
      * @return Perimeter of given rectangle
      */
-    public static float perimeter_rectangle(float length, float breadth) {
-        float perimeter = 2 * (length + breadth);
-        return perimeter;
+    public static float perimeterRectangle(float length, float breadth) {
+        return 2 * (length + breadth);
     }
 
     /**
-     *Calculate the circumference of circle.
-     * @parameter radius of circle.
+     * Calculate the Perimeter or Circumference of circle.
+     * 
+     * @param r for radius of circle.
      * @return circumference of given circle.
      */
-    public static double circumference(float r) {
-        double circumference = 2 * Math.PI * r;
-        return circumference;
+    public static double perimeterCircle(float r) {
+        return 2 * Math.PI * r;
     }
 }


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/PerimeterTest.java b/src/test/java/com/thealgorithms/maths/PerimeterTest.java
new file mode 100644
index 00000000..5af109d5
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/PerimeterTest.java
@@ -0,0 +1,51 @@
+package com.thealgorithms.maths;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class PerimeterTest {
+
+    // Perimeter of Regular polygon
+    @Test
+    void testcase1() {
+        Assertions.assertEquals(20.0, Perimeter.perimeterRegularPolygon(4, 5));
+    }
+
+    @Test
+    void testcase2() {
+        Assertions.assertEquals(30.0, Perimeter.perimeterRegularPolygon(5, 6));
+    }
+
+    // Perimeter of Rectangle
+    @Test
+    void testcase3() {
+        Assertions.assertEquals(18.0, Perimeter.perimeterRectangle(4, 5));
+    }
+
+    @Test
+    void testcase4() {
+        Assertions.assertEquals(14.0, Perimeter.perimeterRectangle(4, 3));
+    }
+
+    // Circumference/Perimeter of a circle
+    @Test
+    void testcase5() {
+        Assertions.assertEquals(31.41592653589793, Perimeter.perimeterCircle(5));
+    }
+
+    @Test
+    void testcase6() {
+        Assertions.assertEquals(43.982297150257104, Perimeter.perimeterCircle(7));
+    }
+  
+    // Perimeter of Irregular polygon
+    @Test
+    void testcase7() {
+        Assertions.assertEquals(12.0, Perimeter.perimeterIrregularPolygon(4, 5, 3));
+    }
+
+    @Test
+    void testcase8() {
+        Assertions.assertEquals(21.0, Perimeter.perimeterIrregularPolygon(3, 4, 5, 3, 6));
+    }
+}
diff --git a/src/test/java/com/thealgorithms/maths/perimeterTest.java b/src/test/java/com/thealgorithms/maths/perimeterTest.java
deleted file mode 100644
index 5ace1726..00000000
--- a/src/test/java/com/thealgorithms/maths/perimeterTest.java
+++ /dev/null
@@ -1,40 +0,0 @@
-package com.thealgorithms.maths;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-public class perimeterTest {
-
-    //Perimeter of polygon
-    @Test
-    void testcase1() {
-        Assertions.assertEquals(20.0, Perimeter.perimeter_polygon(4, 5));
-    }
-
-    @Test
-    void testcase2() {
-        Assertions.assertEquals(30.0, Perimeter.perimeter_polygon(5, 6));
-    }
-
-    //Perimeter of Rectangle
-    @Test
-    void testcase3() {
-        Assertions.assertEquals(18.0, Perimeter.perimeter_rectangle(4, 5));
-    }
-
-    @Test
-    void testcase4() {
-        Assertions.assertEquals(14.0, Perimeter.perimeter_rectangle(4, 3));
-    }
-
-    //Circumference of a circle
-    @Test
-    void testcase5() {
-        Assertions.assertEquals(31.41592653589793, Perimeter.circumference(5));
-    }
-
-    @Test
-    void testcase6() {
-        Assertions.assertEquals(43.982297150257104, Perimeter.circumference(7));
-    }
-}