Skip to content
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
12 changes: 12 additions & 0 deletions src/main/java/com/thealgorithms/maths/Volume.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,16 @@ public static double volumePyramid(double baseArea, double height) {
public static double volumeFrustumOfCone(double r1, double r2, double height) {
return (Math.PI * height / 3) * (r1 * r1 + r2 * r2 + r1 * r2);
}

/**
* Calculate the volume of a frustum of a pyramid.
*
* @param upperBaseArea area of the upper base
* @param lowerBaseArea area of the lower base
* @param height height of the frustum
* @return volume of the frustum
*/
public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) {
return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3;
}
}
3 changes: 3 additions & 0 deletions src/test/java/com/thealgorithms/maths/VolumeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,8 @@ public void volume() {

/* test frustum */
assertEquals(359.188760060433, Volume.volumeFrustumOfCone(3, 5, 7));

/* test pyramid frustum */
assertEquals(140.0, Volume.volumeFrustumOfPyramid(6, 24, 10));
}
}
Loading