Skip to content

Commit 50189d0

Browse files
committed
fix(maths): validate non-positive dimensions and torus radius in Volume
1 parent c1a7f8d commit 50189d0

2 files changed

Lines changed: 134 additions & 11 deletions

File tree

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

Lines changed: 98 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
11
package com.thealgorithms.maths;
22

3-
/* Calculate the volume of various shapes.*/
3+
/**
4+
* Calculate the volume of various shapes.
5+
*/
46
public 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
}

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package com.thealgorithms.maths;
22

3+
import static org.junit.jupiter.api.Assertions.assertAll;
34
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
46

57
import org.junit.jupiter.api.Test;
68

79
public class VolumeTest {
810

911
@Test
1012
public void volume() {
11-
1213
/* test cube */
1314
assertEquals(343.0, Volume.volumeCube(7));
1415

@@ -45,4 +46,38 @@ public void volume() {
4546
/* test ellipsoid */
4647
assertEquals(25.1327412287183459, Volume.volumeEllipsoid(3, 2, 1));
4748
}
49+
50+
@Test
51+
public void testIllegalInputs() {
52+
assertAll(
53+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCube(0)),
54+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCube(-1)),
55+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(0, 1, 1)),
56+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(1, 0, 1)),
57+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCuboid(1, 1, 0)),
58+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeSphere(0)),
59+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeSphere(-5)),
60+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCylinder(0, 5)),
61+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCylinder(5, 0)),
62+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeHemisphere(0)),
63+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCone(0, 5)),
64+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeCone(5, 0)),
65+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePrism(0, 5)),
66+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePrism(5, 0)),
67+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePyramid(0, 5)),
68+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumePyramid(5, 0)),
69+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(0, 5, 5)),
70+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(5, 0, 5)),
71+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfCone(5, 5, 0)),
72+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(0, 5, 5)),
73+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(5, 0, 5)),
74+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeFrustumOfPyramid(5, 5, 0)),
75+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(0, 5)),
76+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(5, 0)),
77+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeTorus(2, 5)),
78+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(0, 1, 2)),
79+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(1, 0, 2)),
80+
() -> assertThrows(IllegalArgumentException.class, () -> Volume.volumeEllipsoid(1, 2, 0))
81+
);
82+
}
4883
}

0 commit comments

Comments
 (0)