1
1
package com .thealgorithms .maths ;
2
2
3
3
/**
4
- * Find the area of various geometric shapes
4
+ * Utility class to find the surface area of various geometric shapes.
5
5
*/
6
6
public final class Area {
7
+ // Prevent instantiation
7
8
private Area () {
8
9
}
9
10
11
+ // Error messages for input validation
12
+
10
13
/**
11
14
* String of IllegalArgumentException for radius
12
15
*/
@@ -16,180 +19,177 @@ private Area() {
16
19
* String of IllegalArgumentException for height
17
20
*/
18
21
private static final String POSITIVE_HEIGHT = "Must be a positive height" ;
19
-
22
+
20
23
/**
21
24
* String of IllegalArgumentException for base
22
25
*/
23
26
private static final String POSITIVE_BASE = "Must be a positive base" ;
24
27
25
28
/**
26
- * Calculate the surface area of a cube.
29
+ * String of IllegalArgumentException for length
30
+ */
31
+ private static final String POSITIVE_LENGTH = "Must be a positive length" ;
32
+
33
+ /**
34
+ * String of IllegalArgumentException for width
35
+ */
36
+ private static final String POSITIVE_WIDTH = "Must be a positive width" ;
37
+ /**
38
+ * Validates that a given value is positive.
27
39
*
28
- * @param sideLength side length of cube
29
- * @return surface area of given cube
40
+ * @param value The value to check.
41
+ * @param message The message to display if validation fails.
42
+ * @throws IllegalArgumentException if value is not positive.
30
43
*/
31
- public static double surfaceAreaCube ( final double sideLength ) {
32
- if (sideLength <= 0 ) {
33
- throw new IllegalArgumentException ("Must be a positive sideLength" );
44
+ private static void validatePositive ( double value , String message ) {
45
+ if (value <= 0 ) {
46
+ throw new IllegalArgumentException (message );
34
47
}
48
+ }
49
+
50
+ /**
51
+ * Calculate the surface area of a cube.
52
+ *
53
+ * @param sideLength Length of one side of the cube.
54
+ * @return Surface area of the cube.
55
+ * @throws IllegalArgumentException if sideLength is not positive.
56
+ */
57
+ public static double cubeSurfaceArea (final double sideLength ) {
58
+ validatePositive (sideLength , "Must be a positive side length" );
35
59
return 6 * sideLength * sideLength ;
36
60
}
37
61
38
62
/**
39
63
* Calculate the surface area of a sphere.
40
64
*
41
- * @param radius radius of sphere
42
- * @return surface area of given sphere
65
+ * @param radius Radius of the sphere.
66
+ * @return Surface area of the sphere.
67
+ * @throws IllegalArgumentException if radius is not positive.
43
68
*/
44
- public static double surfaceAreaSphere (final double radius ) {
45
- if (radius <= 0 ) {
46
- throw new IllegalArgumentException (POSITIVE_RADIUS );
47
- }
69
+ public static double sphereSurfaceArea (final double radius ) {
70
+ validatePositive (radius , POSITIVE_RADIUS );
48
71
return 4 * Math .PI * radius * radius ;
49
72
}
50
73
51
74
/**
52
75
* Calculate the area of a rectangle.
53
76
*
54
- * @param length length of a rectangle
55
- * @param width width of a rectangle
56
- * @return area of given rectangle
77
+ * @param length Length of the rectangle.
78
+ * @param width Width of the rectangle.
79
+ * @return Area of the rectangle.
80
+ * @throws IllegalArgumentException if length or width is not positive.
57
81
*/
58
- public static double surfaceAreaRectangle (final double length , final double width ) {
59
- if (length <= 0 ) {
60
- throw new IllegalArgumentException ("Must be a positive length" );
61
- }
62
- if (width <= 0 ) {
63
- throw new IllegalArgumentException ("Must be a positive width" );
64
- }
82
+ public static double rectangleArea (final double length , final double width ) {
83
+ validatePositive (length , POSITIVE_LENGTH );
84
+ validatePositive (width , POSITIVE_WIDTH );
65
85
return length * width ;
66
86
}
67
87
68
88
/**
69
- * Calculate surface area of a cylinder.
89
+ * Calculate the surface area of a cylinder.
70
90
*
71
- * @param radius radius of the floor
72
- * @param height height of the cylinder.
73
- * @return volume of given cylinder
91
+ * @param radius Radius of the base of the cylinder.
92
+ * @param height Height of the cylinder.
93
+ * @return Surface area of the cylinder.
94
+ * @throws IllegalArgumentException if radius or height is not positive.
74
95
*/
75
- public static double surfaceAreaCylinder (final double radius , final double height ) {
76
- if (radius <= 0 ) {
77
- throw new IllegalArgumentException (POSITIVE_RADIUS );
78
- }
79
- if (height <= 0 ) {
80
- throw new IllegalArgumentException (POSITIVE_RADIUS );
81
- }
96
+ public static double cylinderSurfaceArea (final double radius , final double height ) {
97
+ validatePositive (radius , POSITIVE_RADIUS );
98
+ validatePositive (height , POSITIVE_HEIGHT );
82
99
return 2 * (Math .PI * radius * radius + Math .PI * radius * height );
83
100
}
84
101
85
102
/**
86
103
* Calculate the area of a square.
87
104
*
88
- * @param sideLength side length of square
89
- * @return area of given square
105
+ * @param sideLength Length of one side of the square.
106
+ * @return Area of the square.
107
+ * @throws IllegalArgumentException if sideLength is not positive.
90
108
*/
91
- public static double surfaceAreaSquare (final double sideLength ) {
92
- if (sideLength <= 0 ) {
93
- throw new IllegalArgumentException ("Must be a positive sideLength" );
94
- }
109
+ public static double squareArea (final double sideLength ) {
110
+ validatePositive (sideLength , "Must be a positive side length" );
95
111
return sideLength * sideLength ;
96
112
}
97
113
98
114
/**
99
115
* Calculate the area of a triangle.
100
116
*
101
- * @param base base of triangle
102
- * @param height height of triangle
103
- * @return area of given triangle
117
+ * @param base Base of the triangle.
118
+ * @param height Height of the triangle.
119
+ * @return Area of the triangle.
120
+ * @throws IllegalArgumentException if base or height is not positive.
104
121
*/
105
- public static double surfaceAreaTriangle (final double base , final double height ) {
106
- if (base <= 0 ) {
107
- throw new IllegalArgumentException (POSITIVE_BASE );
108
- }
109
- if (height <= 0 ) {
110
- throw new IllegalArgumentException (POSITIVE_HEIGHT );
111
- }
112
- return base * height / 2 ;
122
+ public static double triangleArea (final double base , final double height ) {
123
+ validatePositive (base , POSITIVE_BASE );
124
+ validatePositive (height , POSITIVE_HEIGHT );
125
+ return (base * height ) / 2 ;
113
126
}
114
127
115
128
/**
116
129
* Calculate the area of a parallelogram.
117
130
*
118
- * @param base base of a parallelogram
119
- * @param height height of a parallelogram
120
- * @return area of given parallelogram
131
+ * @param base Base of the parallelogram.
132
+ * @param height Height of the parallelogram.
133
+ * @return Area of the parallelogram.
134
+ * @throws IllegalArgumentException if base or height is not positive.
121
135
*/
122
- public static double surfaceAreaParallelogram (final double base , final double height ) {
123
- if (base <= 0 ) {
124
- throw new IllegalArgumentException (POSITIVE_BASE );
125
- }
126
- if (height <= 0 ) {
127
- throw new IllegalArgumentException (POSITIVE_HEIGHT );
128
- }
136
+ public static double parallelogramArea (final double base , final double height ) {
137
+ validatePositive (base , POSITIVE_BASE );
138
+ validatePositive (height , POSITIVE_HEIGHT );
129
139
return base * height ;
130
140
}
131
141
132
142
/**
133
143
* Calculate the area of a trapezium.
134
144
*
135
- * @param base1 upper base of trapezium
136
- * @param base2 bottom base of trapezium
137
- * @param height height of trapezium
138
- * @return area of given trapezium
139
- */
140
- public static double surfaceAreaTrapezium (final double base1 , final double base2 , final double height ) {
141
- if (base1 <= 0 ) {
142
- throw new IllegalArgumentException (POSITIVE_BASE + 1 );
143
- }
144
- if (base2 <= 0 ) {
145
- throw new IllegalArgumentException (POSITIVE_BASE + 2 );
146
- }
147
- if (height <= 0 ) {
148
- throw new IllegalArgumentException (POSITIVE_HEIGHT );
149
- }
145
+ * @param base1 Length of the upper base of the trapezium.
146
+ * @param base2 Length of the lower base of the trapezium.
147
+ * @param height Height of the trapezium.
148
+ * @return Area of the trapezium.
149
+ * @throws IllegalArgumentException if base1, base2, or height is not positive.
150
+ */
151
+ public static double trapeziumArea (final double base1 , final double base2 , final double height ) {
152
+ validatePositive (base1 , POSITIVE_BASE );
153
+ validatePositive (base2 , POSITIVE_BASE );
154
+ validatePositive (height , POSITIVE_HEIGHT );
150
155
return (base1 + base2 ) * height / 2 ;
151
156
}
152
157
153
158
/**
154
159
* Calculate the area of a circle.
155
160
*
156
- * @param radius radius of circle
157
- * @return area of given circle
161
+ * @param radius Radius of the circle.
162
+ * @return Area of the circle.
163
+ * @throws IllegalArgumentException if radius is not positive.
158
164
*/
159
- public static double surfaceAreaCircle (final double radius ) {
160
- if (radius <= 0 ) {
161
- throw new IllegalArgumentException (POSITIVE_RADIUS );
162
- }
165
+ public static double circleArea (final double radius ) {
166
+ validatePositive (radius , POSITIVE_RADIUS );
163
167
return Math .PI * radius * radius ;
164
168
}
165
169
166
170
/**
167
171
* Calculate the surface area of a hemisphere.
168
172
*
169
- * @param radius radius of hemisphere
170
- * @return surface area of given hemisphere
173
+ * @param radius Radius of the hemisphere.
174
+ * @return Surface area of the hemisphere.
175
+ * @throws IllegalArgumentException if radius is not positive.
171
176
*/
172
- public static double surfaceAreaHemisphere (final double radius ) {
173
- if (radius <= 0 ) {
174
- throw new IllegalArgumentException (POSITIVE_RADIUS );
175
- }
177
+ public static double hemisphereSurfaceArea (final double radius ) {
178
+ validatePositive (radius , POSITIVE_RADIUS );
176
179
return 3 * Math .PI * radius * radius ;
177
180
}
178
181
179
182
/**
180
183
* Calculate the surface area of a cone.
181
184
*
182
- * @param radius radius of cone.
183
- * @param height of cone.
184
- * @return surface area of given cone.
185
+ * @param radius Radius of the base of the cone.
186
+ * @param height Height of the cone.
187
+ * @return Surface area of the cone.
188
+ * @throws IllegalArgumentException if radius or height is not positive.
185
189
*/
186
- public static double surfaceAreaCone (final double radius , final double height ) {
187
- if (radius <= 0 ) {
188
- throw new IllegalArgumentException (POSITIVE_RADIUS );
189
- }
190
- if (height <= 0 ) {
191
- throw new IllegalArgumentException (POSITIVE_HEIGHT );
192
- }
193
- return Math .PI * radius * (radius + Math .pow (height * height + radius * radius , 0.5 ));
190
+ public static double coneSurfaceArea (final double radius , final double height ) {
191
+ validatePositive (radius , POSITIVE_RADIUS );
192
+ validatePositive (height , POSITIVE_HEIGHT );
193
+ return Math .PI * radius * (radius + Math .sqrt (height * height + radius * radius ));
194
194
}
195
195
}
0 commit comments