Skip to content

Commit d867288

Browse files
authored
Added surface area of a cuboid (#7293)
* Added surface area of a cuboid Added surface area of a cuboid according to the formula: S = 2 * (ab + ac + bc) * Removed extra white space * Added test for cuboid surface area * fixed syntax error * Removed extra space * Added tests for cuboid surface area that should fail I have added tests for cuboid surface area where one of the parameters is invalid. These should fail.
1 parent 0d2a98e commit d867288

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/main/java/com/thealgorithms/maths/Area.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,27 @@ public static double surfaceAreaCube(final double sideLength) {
3535
return 6 * sideLength * sideLength;
3636
}
3737

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+
3859
/**
3960
* Calculate the surface area of a sphere.
4061
*

src/test/java/com/thealgorithms/maths/AreaTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ void testSurfaceAreaCube() {
1616
assertEquals(6.0, Area.surfaceAreaCube(1));
1717
}
1818

19+
@Test
20+
void testSurfaceAreaCuboid() {
21+
assertEquals(214.0, Area.surfaceAreaCuboid(5, 6, 7));
22+
}
23+
1924
@Test
2025
void testSurfaceAreaSphere() {
2126
assertEquals(12.566370614359172, Area.surfaceAreaSphere(1));
@@ -70,6 +75,12 @@ void surfaceAreaCone() {
7075
void testAllIllegalInput() {
7176
assertAll(()
7277
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCube(0)),
78+
()
79+
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCuboid(0, 1, 2)),
80+
()
81+
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCuboid(1, 0, 2)),
82+
()
83+
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaCuboid(1, 2, 0)),
7384
()
7485
-> assertThrows(IllegalArgumentException.class, () -> Area.surfaceAreaSphere(0)),
7586
()

0 commit comments

Comments
 (0)