MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / Area

Class Area

src/main/java/com/thealgorithms/maths/Area.java:6–235  ·  view source on GitHub ↗

Find the area of various geometric shapes

Source from the content-addressed store, hash-verified

4 * Find the area of various geometric shapes
5 */
6public final class Area {
7 private Area() {
8 }
9
10 /**
11 * String of IllegalArgumentException for radius
12 */
13 private static final String POSITIVE_RADIUS = "Radius must be greater than 0";
14
15 /**
16 * String of IllegalArgumentException for height
17 */
18 private static final String POSITIVE_HEIGHT = "Height must be greater than 0";
19
20 /**
21 * String of IllegalArgumentException for base
22 */
23 private static final String POSITIVE_BASE = "Base must be greater than 0";
24
25 /**
26 * Calculate the surface area of a cube.
27 *
28 * @param sideLength side length of cube
29 * @return surface area of given cube
30 */
31 public static double surfaceAreaCube(final double sideLength) {
32 if (sideLength <= 0) {
33 throw new IllegalArgumentException("Side length must be greater than 0");
34 }
35 return 6 * sideLength * sideLength;
36 }
37
38 /**
39 * Calculate the surface area of a cuboid.
40 *
41 * @param length length of the cuboid
42 * @param width width of the cuboid
43 * @param height height of the cuboid
44 * @return surface area of given cuboid
45 */
46 public static double surfaceAreaCuboid(final double length, double width, double height) {
47 if (length <= 0) {
48 throw new IllegalArgumentException("Length must be greater than 0");
49 }
50 if (width <= 0) {
51 throw new IllegalArgumentException("Width must be greater than 0");
52 }
53 if (height <= 0) {
54 throw new IllegalArgumentException("Height must be greater than 0");
55 }
56 return 2 * (length * width + length * height + width * height);
57 }
58
59 /**
60 * Calculate the surface area of a sphere.
61 *
62 * @param radius radius of sphere
63 * @return surface area of given sphere

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected