Skip to content

Perimeter Calculator #3247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<assertj.version>3.22.0</assertj.version>
<assertj.version>3.23.1</assertj.version>
</properties>

<dependencyManagement>
Expand All @@ -31,13 +31,20 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
<scope>import</scope>
</dependency>
</dependencies>

<build>
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/thealgorithms/maths/Perimeter.java
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions src/test/java/com/thealgorithms/maths/perimeterTest.java
Original file line number Diff line number Diff line change
@@ -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));
}
}