Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add math/Volume algorithms and its test #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/main/kotlin/math/Volume.kt
Original file line number Diff line number Diff line change
@@ -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")
}
26 changes: 26 additions & 0 deletions src/test/kotlin/math/VolumeTest.kt
Original file line number Diff line number Diff line change
@@ -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)
}