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:
parent
957f633c93
commit
8efc71e609
@ -1,42 +1,56 @@
|
|||||||
package com.thealgorithms.maths;
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
// Perimeter of different 2D geometrical shapes
|
||||||
public class Perimeter {
|
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.
|
* Calculate the Perimeter of regular polygon (equals sides)
|
||||||
* @parameter length of side.
|
* Examples of regular polygon are Equilateral Triangle, Square, Regular Pentagon, Regular Hexagon.
|
||||||
* @parameter number of sides.
|
*
|
||||||
|
* @param n for number of sides.
|
||||||
|
* @param side for length of each side.
|
||||||
* @return Perimeter of given polygon
|
* @return Perimeter of given polygon
|
||||||
*/
|
*/
|
||||||
public static float perimeter_polygon(int n, float side) {
|
public static float perimeterRegularPolygon(int n, float side) {
|
||||||
float perimeter = n * 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;
|
return perimeter;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Calculate the Perimeter of rectangle.
|
* Calculate the Perimeter of rectangle
|
||||||
* @parameter length and breadth.
|
*
|
||||||
|
* @param length for length of rectangle
|
||||||
|
* @param breadth for breadth of rectangle
|
||||||
* @return Perimeter of given rectangle
|
* @return Perimeter of given rectangle
|
||||||
*/
|
*/
|
||||||
public static float perimeter_rectangle(float length, float breadth) {
|
public static float perimeterRectangle(float length, float breadth) {
|
||||||
float perimeter = 2 * (length + breadth);
|
return 2 * (length + breadth);
|
||||||
return perimeter;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*Calculate the circumference of circle.
|
* Calculate the Perimeter or Circumference of circle.
|
||||||
* @parameter radius of circle.
|
*
|
||||||
|
* @param r for radius of circle.
|
||||||
* @return circumference of given circle.
|
* @return circumference of given circle.
|
||||||
*/
|
*/
|
||||||
public static double circumference(float r) {
|
public static double perimeterCircle(float r) {
|
||||||
double circumference = 2 * Math.PI * r;
|
return 2 * Math.PI * r;
|
||||||
return circumference;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
51
src/test/java/com/thealgorithms/maths/PerimeterTest.java
Normal file
51
src/test/java/com/thealgorithms/maths/PerimeterTest.java
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user