11package com .thealgorithms .maths ;
22
3- /* Calculate the volume of various shapes.*/
3+ /**
4+ * Calculate the volume of various shapes.
5+ */
46public final class Volume {
57 private Volume () {
68 }
79
10+ private static final String POSITIVE_RADIUS = "Radius must be greater than 0" ;
11+ private static final String POSITIVE_HEIGHT = "Height must be greater than 0" ;
12+ private static final String POSITIVE_SIDE = "Side length must be greater than 0" ;
13+ private static final String POSITIVE_BASE_AREA = "Base area must be greater than 0" ;
14+ private static final String POSITIVE_LENGTH = "Length must be greater than 0" ;
15+ private static final String POSITIVE_WIDTH = "Width must be greater than 0" ;
16+ private static final String POSITIVE_AXIS = "Semi-axis must be greater than 0" ;
17+
818 /**
919 * Calculate the volume of a cube.
1020 *
1121 * @param sideLength length of the given cube's sides
1222 * @return volume of the given cube
23+ * @throws IllegalArgumentException if sideLength is less than or equal to 0
1324 */
1425 public static double volumeCube (double sideLength ) {
26+ if (sideLength <= 0 ) {
27+ throw new IllegalArgumentException (POSITIVE_SIDE );
28+ }
1529 return sideLength * sideLength * sideLength ;
1630 }
1731
@@ -22,8 +36,18 @@ public static double volumeCube(double sideLength) {
2236 * @param height height of given cuboid
2337 * @param length length of given cuboid
2438 * @return volume of given cuboid
39+ * @throws IllegalArgumentException if any dimension is less than or equal to 0
2540 */
2641 public static double volumeCuboid (double width , double height , double length ) {
42+ if (width <= 0 ) {
43+ throw new IllegalArgumentException (POSITIVE_WIDTH );
44+ }
45+ if (height <= 0 ) {
46+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
47+ }
48+ if (length <= 0 ) {
49+ throw new IllegalArgumentException (POSITIVE_LENGTH );
50+ }
2751 return width * height * length ;
2852 }
2953
@@ -32,19 +56,30 @@ public static double volumeCuboid(double width, double height, double length) {
3256 *
3357 * @param radius radius of given sphere
3458 * @return volume of given sphere
59+ * @throws IllegalArgumentException if radius is less than or equal to 0
3560 */
3661 public static double volumeSphere (double radius ) {
62+ if (radius <= 0 ) {
63+ throw new IllegalArgumentException (POSITIVE_RADIUS );
64+ }
3765 return (4 * Math .PI * radius * radius * radius ) / 3 ;
3866 }
3967
4068 /**
41- * Calculate volume of a cylinder
69+ * Calculate volume of a cylinder.
4270 *
4371 * @param radius radius of the given cylinder's floor
4472 * @param height height of the given cylinder
4573 * @return volume of given cylinder
74+ * @throws IllegalArgumentException if radius or height is less than or equal to 0
4675 */
4776 public static double volumeCylinder (double radius , double height ) {
77+ if (radius <= 0 ) {
78+ throw new IllegalArgumentException (POSITIVE_RADIUS );
79+ }
80+ if (height <= 0 ) {
81+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
82+ }
4883 return Math .PI * radius * radius * height ;
4984 }
5085
@@ -53,53 +88,85 @@ public static double volumeCylinder(double radius, double height) {
5388 *
5489 * @param radius radius of given hemisphere
5590 * @return volume of given hemisphere
91+ * @throws IllegalArgumentException if radius is less than or equal to 0
5692 */
5793 public static double volumeHemisphere (double radius ) {
94+ if (radius <= 0 ) {
95+ throw new IllegalArgumentException (POSITIVE_RADIUS );
96+ }
5897 return (2 * Math .PI * radius * radius * radius ) / 3 ;
5998 }
6099
61100 /**
62101 * Calculate the volume of a cone.
63102 *
64103 * @param radius radius of given cone
65- * @param height of given cone
104+ * @param height height of given cone
66105 * @return volume of given cone
106+ * @throws IllegalArgumentException if radius or height is less than or equal to 0
67107 */
68108 public static double volumeCone (double radius , double height ) {
109+ if (radius <= 0 ) {
110+ throw new IllegalArgumentException (POSITIVE_RADIUS );
111+ }
112+ if (height <= 0 ) {
113+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
114+ }
69115 return (Math .PI * radius * radius * height ) / 3 ;
70116 }
71117
72118 /**
73119 * Calculate the volume of a prism.
74120 *
75- * @param baseArea area of the given prism's base
76- * @param height of given prism
121+ * @param baseArea area of the given prism's base
122+ * @param height height of given prism
77123 * @return volume of given prism
124+ * @throws IllegalArgumentException if baseArea or height is less than or equal to 0
78125 */
79126 public static double volumePrism (double baseArea , double height ) {
127+ if (baseArea <= 0 ) {
128+ throw new IllegalArgumentException (POSITIVE_BASE_AREA );
129+ }
130+ if (height <= 0 ) {
131+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
132+ }
80133 return baseArea * height ;
81134 }
82135
83136 /**
84137 * Calculate the volume of a pyramid.
85138 *
86- * @param baseArea of the given pyramid's base
87- * @param height of given pyramid
139+ * @param baseArea area of the given pyramid's base
140+ * @param height height of given pyramid
88141 * @return volume of given pyramid
142+ * @throws IllegalArgumentException if baseArea or height is less than or equal to 0
89143 */
90144 public static double volumePyramid (double baseArea , double height ) {
145+ if (baseArea <= 0 ) {
146+ throw new IllegalArgumentException (POSITIVE_BASE_AREA );
147+ }
148+ if (height <= 0 ) {
149+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
150+ }
91151 return (baseArea * height ) / 3 ;
92152 }
93153
94154 /**
95155 * Calculate the volume of a frustum of a cone.
96156 *
97- * @param r1 radius of the top of the frustum
98- * @param r2 radius of the bottom of the frustum
157+ * @param r1 radius of the top of the frustum
158+ * @param r2 radius of the bottom of the frustum
99159 * @param height height of the frustum
100160 * @return volume of the frustum
161+ * @throws IllegalArgumentException if radii or height are less than or equal to 0
101162 */
102163 public static double volumeFrustumOfCone (double r1 , double r2 , double height ) {
164+ if (r1 <= 0 || r2 <= 0 ) {
165+ throw new IllegalArgumentException (POSITIVE_RADIUS );
166+ }
167+ if (height <= 0 ) {
168+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
169+ }
103170 return (Math .PI * height / 3 ) * (r1 * r1 + r2 * r2 + r1 * r2 );
104171 }
105172
@@ -108,10 +175,17 @@ public static double volumeFrustumOfCone(double r1, double r2, double height) {
108175 *
109176 * @param upperBaseArea area of the upper base
110177 * @param lowerBaseArea area of the lower base
111- * @param height height of the frustum
178+ * @param height height of the frustum
112179 * @return volume of the frustum
180+ * @throws IllegalArgumentException if base areas or height are less than or equal to 0
113181 */
114182 public static double volumeFrustumOfPyramid (double upperBaseArea , double lowerBaseArea , double height ) {
183+ if (upperBaseArea <= 0 || lowerBaseArea <= 0 ) {
184+ throw new IllegalArgumentException (POSITIVE_BASE_AREA );
185+ }
186+ if (height <= 0 ) {
187+ throw new IllegalArgumentException (POSITIVE_HEIGHT );
188+ }
115189 return (upperBaseArea + lowerBaseArea + Math .sqrt (upperBaseArea * lowerBaseArea )) * height / 3 ;
116190 }
117191
@@ -121,8 +195,18 @@ public static double volumeFrustumOfPyramid(double upperBaseArea, double lowerBa
121195 * @param majorRadius major radius of a torus
122196 * @param minorRadius minor radius of a torus
123197 * @return volume of the torus
198+ * @throws IllegalArgumentException if radii are non-positive or minor radius exceeds major radius
124199 */
125200 public static double volumeTorus (double majorRadius , double minorRadius ) {
201+ if (majorRadius <= 0 ) {
202+ throw new IllegalArgumentException ("Major radius must be greater than 0" );
203+ }
204+ if (minorRadius <= 0 ) {
205+ throw new IllegalArgumentException ("Minor radius must be greater than 0" );
206+ }
207+ if (minorRadius > majorRadius ) {
208+ throw new IllegalArgumentException ("Major radius must be greater than or equal to minor radius" );
209+ }
126210 return 2 * Math .PI * Math .PI * majorRadius * minorRadius * minorRadius ;
127211 }
128212
@@ -133,8 +217,12 @@ public static double volumeTorus(double majorRadius, double minorRadius) {
133217 * @param b second semi-axis of an ellipsoid
134218 * @param c third semi-axis of an ellipsoid
135219 * @return volume of the ellipsoid
220+ * @throws IllegalArgumentException if any semi-axis is less than or equal to 0
136221 */
137222 public static double volumeEllipsoid (double a , double b , double c ) {
223+ if (a <= 0 || b <= 0 || c <= 0 ) {
224+ throw new IllegalArgumentException (POSITIVE_AXIS );
225+ }
138226 return (4 * Math .PI * a * b * c ) / 3 ;
139227 }
140228}
0 commit comments