Calculate the Volume of a Cuboid in Java

A Cuboid is a geometric object that is more or less cubic in shape. It is a solid which has six rectangular faces at right angles to each other.

The formula to calculate a cuboid is pretty simple, just multiple the length by the width by the height to get the total volume.

The solution in Java code

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px,1px,1px,1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0 .75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```public class Solution {

public static double getVolumeOfCuboid(final double length,
final double width,
final double height) {
return length*width*height;
}

}
```Code language: Java (java)```

Test cases to validate our solution

``````import org.junit.Test;
import static org.junit.Assert.*;

public class ExampleTests {

private static final double delta = 0.0001;

@Test
public void examples() {
// assertEquals("expected", "actual");
assertEquals(4, Solution.getVolumeOfCuboid(1, 2, 2), delta);
assertEquals(63, Solution.getVolumeOfCuboid(6.3, 2, 5), delta);
}
}

```Code language: Java (java)```
Tags:
Subscribe
Notify of