Added functions in Perimeter.java (#3739)

* Added functions and removed main

* deleted perimeterTest.java

deleted perimeterTest.java to create PerimeterTest.java

* Recreated PerimeterTest.java

Renamed perimeterTest.java to PerimeterTest.java and added test cases

* deleted PerimeterTest.java

* Recreated PerimeterTest.java

Recreated PerimeterTest.java from perimeterTest.java
This commit is contained in:
Taranjeet Singh Kalsi 2022-10-30 09:22:25 +05:30 committed by GitHub
parent 957f633c93
commit 8efc71e609
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 87 additions and 62 deletions

View File

@ -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));
}
// Perimeter of different 2D geometrical shapes
/**
*Calculate the Perimeter of polygon.
* @parameter length of side.
* @parameter number of sides.
* 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 perimeter_polygon(int n, float side) {
float perimeter = n * side;
public static float perimeterRegularPolygon(int n, float side) {
return n * side;
}
/**
* 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 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;
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}