Skip to content

Commit d89416d

Browse files
Add Function volumeFrustum To Calculate Frustum Of Cone Then Test It.
1 parent 1e8abf1 commit d89416d

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/main/java/com/thealgorithms/maths/Volume.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,17 @@ public static double volumePrism(double baseArea, double height) {
9090
public static double volumePyramid(double baseArea, double height) {
9191
return (baseArea * height) / 3;
9292
}
93+
94+
/**
95+
* Calculate the volume of a frustum of a cone.
96+
*
97+
* @param r1 radius of the top of the frustum
98+
* @param r2 radius of the bottom of the frustum
99+
* @param height height of the frustum
100+
* @return volume of the frustum
101+
*/
102+
public static double volumeFrustum(double r1, double r2, double height) {
103+
return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2);
104+
}
105+
93106
}

src/test/java/com/thealgorithms/maths/VolumeTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,9 @@ public void volume() {
3232

3333
/* test pyramid */
3434
assertTrue(Volume.volumePyramid(10, 3) == 10.0);
35+
36+
/* test frustum */
37+
assertTrue(Volume.volumeFrustum(3, 5, 7) == 359.188760060433);
38+
3539
}
3640
}

0 commit comments

Comments
 (0)