Software Engineering

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. We want to calculate volume of cuboid.

Additionally, the volume of cuboid test case below validates the Java code solution.

The solution in Java code

public class Solution {

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

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;
  public void examples() {
      // assertEquals("expected", "actual");
      assertEquals(4, Solution.getVolumeOfCuboid(1, 2, 2), delta);
      assertEquals(63, Solution.getVolumeOfCuboid(6.3, 2, 5), delta);