Skip to content

Commit e3d9dd7

Browse files
authored
Updated Area.java
1 parent bca8d0e commit e3d9dd7

File tree

1 file changed

+100
-100
lines changed
  • src/main/java/com/thealgorithms/maths

1 file changed

+100
-100
lines changed
Lines changed: 100 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package com.thealgorithms.maths;
22

33
/**
4-
* Find the area of various geometric shapes
4+
* Utility class to find the surface area of various geometric shapes.
55
*/
66
public final class Area {
7+
// Prevent instantiation
78
private Area() {
89
}
910

11+
// Error messages for input validation
12+
1013
/**
1114
* String of IllegalArgumentException for radius
1215
*/
@@ -16,180 +19,177 @@ private Area() {
1619
* String of IllegalArgumentException for height
1720
*/
1821
private static final String POSITIVE_HEIGHT = "Must be a positive height";
19-
22+
2023
/**
2124
* String of IllegalArgumentException for base
2225
*/
2326
private static final String POSITIVE_BASE = "Must be a positive base";
2427

2528
/**
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.
2739
*
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.
3043
*/
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);
3447
}
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");
3559
return 6 * sideLength * sideLength;
3660
}
3761

3862
/**
3963
* Calculate the surface area of a sphere.
4064
*
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.
4368
*/
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);
4871
return 4 * Math.PI * radius * radius;
4972
}
5073

5174
/**
5275
* Calculate the area of a rectangle.
5376
*
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.
5781
*/
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);
6585
return length * width;
6686
}
6787

6888
/**
69-
* Calculate surface area of a cylinder.
89+
* Calculate the surface area of a cylinder.
7090
*
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.
7495
*/
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);
8299
return 2 * (Math.PI * radius * radius + Math.PI * radius * height);
83100
}
84101

85102
/**
86103
* Calculate the area of a square.
87104
*
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.
90108
*/
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");
95111
return sideLength * sideLength;
96112
}
97113

98114
/**
99115
* Calculate the area of a triangle.
100116
*
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.
104121
*/
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;
113126
}
114127

115128
/**
116129
* Calculate the area of a parallelogram.
117130
*
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.
121135
*/
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);
129139
return base * height;
130140
}
131141

132142
/**
133143
* Calculate the area of a trapezium.
134144
*
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);
150155
return (base1 + base2) * height / 2;
151156
}
152157

153158
/**
154159
* Calculate the area of a circle.
155160
*
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.
158164
*/
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);
163167
return Math.PI * radius * radius;
164168
}
165169

166170
/**
167171
* Calculate the surface area of a hemisphere.
168172
*
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.
171176
*/
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);
176179
return 3 * Math.PI * radius * radius;
177180
}
178181

179182
/**
180183
* Calculate the surface area of a cone.
181184
*
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.
185189
*/
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));
194194
}
195195
}

0 commit comments

Comments
 (0)