Calculate the Volume of a Cuboid in Java

0 min read 141 words

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

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags