mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 17:29:31 +08:00
Add Perimeter Calculation Algorithms (#3247)
Co-authored-by: Andrii Siriak <siryaka@gmail.com>
This commit is contained in:
9
pom.xml
9
pom.xml
@ -12,7 +12,7 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<maven.compiler.source>17</maven.compiler.source>
|
<maven.compiler.source>17</maven.compiler.source>
|
||||||
<maven.compiler.target>17</maven.compiler.target>
|
<maven.compiler.target>17</maven.compiler.target>
|
||||||
<assertj.version>3.22.0</assertj.version>
|
<assertj.version>3.23.1</assertj.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -31,6 +31,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.9.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -38,6 +39,12 @@
|
|||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>${assertj.version}</version>
|
<version>${assertj.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.9.0</version>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
38
src/main/java/com/thealgorithms/maths/Perimeter.java
Normal file
38
src/main/java/com/thealgorithms/maths/Perimeter.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.thealgorithms.maths;
|
||||||
|
|
||||||
|
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.
|
||||||
|
* @return Perimeter of given polygon
|
||||||
|
*/
|
||||||
|
public static float perimeter_polygon( int n, float side){
|
||||||
|
float perimeter = n*side;
|
||||||
|
return perimeter;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*Calculate the Perimeter of rectangle.
|
||||||
|
* @parameter length and breadth.
|
||||||
|
* @return Perimeter of given rectangle
|
||||||
|
*/
|
||||||
|
public static float perimeter_rectangle( float length, float breadth){
|
||||||
|
float perimeter = 2*(length + breadth);
|
||||||
|
return perimeter;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
*Calculate the circumference of circle.
|
||||||
|
* @parameter radius of circle.
|
||||||
|
* @return circumference of given circle.
|
||||||
|
*/
|
||||||
|
public static double circumference( float r){
|
||||||
|
double circumference = 2*Math.PI*r;
|
||||||
|
return circumference;
|
||||||
|
}
|
||||||
|
}
|
37
src/test/java/com/thealgorithms/maths/perimeterTest.java
Normal file
37
src/test/java/com/thealgorithms/maths/perimeterTest.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user