Skip to content

Commit 0d2a98e

Browse files
authored
Added volume of a torus (#7294)
* Added volume of a torus Added function calculation the volume of a torus according to the formula: V = 2 * pi^2 * R * r^2 where R is the major radius and r is the minor radius of the torus. * Added test for torus volume
1 parent ba286b2 commit 0d2a98e

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,15 @@ public static double volumeFrustumOfCone(double r1, double r2, double height) {
114114
public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBaseArea, double height) {
115115
return (upperBaseArea + lowerBaseArea + Math.sqrt(upperBaseArea * lowerBaseArea)) * height / 3;
116116
}
117+
118+
/**
119+
* Calculate the volume of a torus.
120+
*
121+
* @param majorRadius major radius of a torus
122+
* @param minorRadius minor radius of a torus
123+
* @return volume of the torus
124+
*/
125+
public static double volumeTorus(double majorRadius, double minorRadius) {
126+
return 2 * Math.PI * Math.PI * majorRadius * minorRadius * minorRadius;
127+
}
117128
}

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

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

3939
/* test pyramid frustum */
4040
assertEquals(140.0, Volume.volumeFrustumOfPyramid(6, 24, 10));
41+
42+
/* test torus */
43+
assertEquals(39.47841760435743, Volume.volumeTorus(2, 1));
4144
}
4245
}

0 commit comments

Comments
 (0)