Skip to content

Commit 705eb52

Browse files
authored
Added volume of a pyramid frustum (#7291)
* Added volume of a pyramid frustum Added a function calculating volume of a pyramid frustum, V=(S1+S2+sqrt(S1*S2))*h/3 * compiler error fixed * Added pyramid frustum test case * extra space removed
1 parent 7e5d9d4 commit 705eb52

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,16 @@ public static double volumePyramid(double baseArea, double height) {
102102
public static double volumeFrustumOfCone(double r1, double r2, double height) {
103103
return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2);
104104
}
105+
106+
/**
107+
* Calculate the volume of a frustum of a pyramid.
108+
*
109+
* @param upperBaseArea area of the upper base
110+
* @param lowerBaseArea area of the lower base
111+
* @param height height of the frustum
112+
* @return volume of the frustum
113+
*/
114+
public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) {
115+
return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3;
116+
}
105117
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,8 @@ public void volume() {
3535

3636
/* test frustum */
3737
assertEquals(359.188760060433, Volume.volumeFrustumOfCone(3, 5, 7));
38+
39+
/* test pyramid frustum */
40+
assertEquals(140.0, Volume.volumeFrustumOfPyramid(6, 24, 10));
3841
}
3942
}

0 commit comments

Comments
 (0)