From 84969f7556c49e327718d09df76f2f4e45f34b25 Mon Sep 17 00:00:00 2001 From: ucok Date: Sun, 11 Sep 2022 16:20:31 +0700 Subject: [PATCH 1/2] add volume algorithms --- src/main/kotlin/math/Volume.kt | 91 ++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/main/kotlin/math/Volume.kt diff --git a/src/main/kotlin/math/Volume.kt b/src/main/kotlin/math/Volume.kt new file mode 100644 index 0000000..ed71d00 --- /dev/null +++ b/src/main/kotlin/math/Volume.kt @@ -0,0 +1,91 @@ +package math + +import kotlin.math.pow + +/** + * Calculate the volume of a cube + * + * @param length of a cube + * @return volume of given cube + */ +fun cubeVolume(length: Double) = when { + length > 0 -> length * length * length + else -> throw IllegalArgumentException("Length must be positive") +} + +/** + * Calculate volume of a rectangular prism + * + * @param length of rectangular prism + * @param width of rectangular prism + * @param height of rectangular prism + * @return volume of given rectangular prism + */ +fun rectPrismVolume(length: Double, width: Double, height: Double) = when { + length > 0 && width > 0 && height > 0 -> length * width * height + else -> throw IllegalArgumentException("Length, Width and Height must be positive") +} + +/** + * Calculate volume of a triangle prism + * + * @param base of triangle prism + * @param height of triangle prism + * @param depth of triangle prism + * @return volume of given triangle prism + */ +fun trianglePrismVolume(base: Double, height: Double, depth: Double) = when { + base > 0 && height > 0 && depth > 0 -> base * height * depth / 2 + else -> throw IllegalArgumentException("Base, Height, Depth must be positive") +} + +/** + * Calculate volume of a cone + * + * @param radius of cone + * @param height of cone + * @param PI (optional) default to 3.14 + * @return volume of given cone + */ +fun coneVolume(radius: Double, height: Double, PI: Double = 3.14) = when { + radius > 0 && height > 0 && PI > 0 -> radius.pow(2) * PI * height / 3 + else -> throw IllegalArgumentException("Radius and Height must be positive") +} + +/** + * Calculate volume of a pyramid + * + * @param length of pyramid + * @param width of pyramid + * @param height of pyramid + * @return volume of given pyramid + */ +fun pyramidVolume(length: Double, width: Double, height: Double) = when { + length > 0 && width > 0 && height > 0 -> length * width * height / 3 + else -> throw IllegalArgumentException("Length, Width and Height must be positive") +} + +/** + * Calculate volume of a sphere + * + * @param radius of sphere + * @param PI (optional) default to 3.14 + * @return volume of given sphere + */ +fun sphereVolume(radius: Double, PI: Double = 3.14) = when { + radius > 0 && PI > 0 -> PI * radius.pow(3) * 4 / 3 + else -> throw IllegalArgumentException("Radius must be positive") +} + +/** + * Calculate volume of a cylinder + * + * @param radius of cylinder + * @param height of cylinder + * @param PI (optional) default to 3.14 + * @return volume of given cylinder + */ +fun cylinderVolume(radius: Double, height: Double, PI: Double = 3.14) = when { + radius > 0 && height > 0 && PI > 0 -> PI * radius.pow(2) * height + else -> throw IllegalArgumentException("Radius and Height must be positive") +} \ No newline at end of file From d7960bdb35b8408026c1085742c0c35e2b6fb606 Mon Sep 17 00:00:00 2001 From: ucok Date: Sun, 11 Sep 2022 16:21:47 +0700 Subject: [PATCH 2/2] add test for volume algorithms --- src/test/kotlin/math/VolumeTest.kt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/test/kotlin/math/VolumeTest.kt diff --git a/src/test/kotlin/math/VolumeTest.kt b/src/test/kotlin/math/VolumeTest.kt new file mode 100644 index 0000000..2c728a5 --- /dev/null +++ b/src/test/kotlin/math/VolumeTest.kt @@ -0,0 +1,26 @@ +package math + +import org.junit.Test + +class VolumeTest { + @Test + fun testCubeVolume() = assert(cubeVolume(2.0) == 8.0) + + @Test + fun testRectPrismVolume() = assert(rectPrismVolume(2.0, 2.0, 2.0) == 8.0) + + @Test + fun testTrianglePrismVolume() = assert(trianglePrismVolume(2.0, 2.0, 2.0) == 4.0) + + @Test + fun testConeVolume() = assert(coneVolume(2.0, 3.0) == 12.56) + + @Test + fun testPyramidVolume() = assert(pyramidVolume(2.0, 2.0, 3.0) == 4.0) + + @Test + fun testSphereVolume() = assert(String.format("%.2f", sphereVolume(1.0)).toDouble() == 4.19) + + @Test + fun testCylinderVolume() = assert(String.format("%.2f", cylinderVolume(1.0, 1.0)).toDouble() == 3.14) +} \ No newline at end of file