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

Added areaOfATrapezium function to the math folder of algorithms #97

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 20 additions & 2 deletions src/main/kotlin/math/Area.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package math

import java.lang.IllegalArgumentException
import kotlin.IllegalArgumentException
import kotlin.math.pow

/**
Expand Down Expand Up @@ -50,4 +50,22 @@ fun areaOfACircle(radius: Double) =
when {
radius > 0 -> Math.PI * radius.pow(2.0)
else -> throw IllegalArgumentException("Radius must be positive")
}
}


/**
* Calculate the area of a trapezium
*
* @param parallelSide1 one of the parallel side of the trapezium
* @param parallelSide2 second parallel side of the trapezium
* @param height distance between these two parallel sides of trapezium
* @return area of given circle
*/

fun areaOfATrapezium(parallelSide1: Double, parallelSide2: Double, height: Double) =
when {
height > 0 && (parallelSide1 > 0 && parallelSide2 > 0) -> ((parallelSide1 + parallelSide2))/2*height
else -> throw IllegalArgumentException("Length of both the parallel side and height should be positive")
}


9 changes: 8 additions & 1 deletion src/test/kotlin/math/AreaTest.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package math

import org.junit.Test
import java.lang.IllegalArgumentException
import kotlin.IllegalArgumentException

class AreaTest {
@Test
Expand All @@ -16,12 +16,16 @@ class AreaTest {
@Test
fun testAreaOfATriangle() = assert(areaOfATriangle(5.0, 10.0) == 25.0)

@Test
fun testAreaOfATrapezium() = assert(areaOfATrapezium(6.0, 8.0, 4.0) == 28.0)

@Test(expected = IllegalArgumentException::class)
fun testAreaWithNegatives() {
areaOfARectangle(-1.0, 0.0)
areaOfASquare(-1.0)
areaOfACircle(-1.0)
areaOfATriangle(-1.0, 1.0)
areaOfATrapezium(-1.0, -1.0, -1.0)
}

@Test(expected = IllegalArgumentException::class)
Expand All @@ -30,5 +34,8 @@ class AreaTest {
areaOfASquare(0.0)
areaOfACircle(0.0)
areaOfATriangle(0.0, 1.0)
areaOfATrapezium(0.0, 0.0, 0.0)
}


}