diff --git a/.idea/misc.xml b/.idea/misc.xml index 4b49563..6154377 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - + \ No newline at end of file diff --git a/src/main/kotlin/math/Area.kt b/src/main/kotlin/math/Area.kt index a318708..e02af9a 100644 --- a/src/main/kotlin/math/Area.kt +++ b/src/main/kotlin/math/Area.kt @@ -1,6 +1,6 @@ package math -import java.lang.IllegalArgumentException +import kotlin.IllegalArgumentException import kotlin.math.pow /** @@ -50,4 +50,22 @@ fun areaOfACircle(radius: Double) = when { radius > 0 -> Math.PI * radius.pow(2.0) else -> throw IllegalArgumentException("Radius must be positive") - } \ No newline at end of file + } + + +/** + * 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") + } + + diff --git a/src/test/kotlin/math/AreaTest.kt b/src/test/kotlin/math/AreaTest.kt index 8871c84..32e91c8 100644 --- a/src/test/kotlin/math/AreaTest.kt +++ b/src/test/kotlin/math/AreaTest.kt @@ -1,7 +1,7 @@ package math import org.junit.Test -import java.lang.IllegalArgumentException +import kotlin.IllegalArgumentException class AreaTest { @Test @@ -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) @@ -30,5 +34,8 @@ class AreaTest { areaOfASquare(0.0) areaOfACircle(0.0) areaOfATriangle(0.0, 1.0) + areaOfATrapezium(0.0, 0.0, 0.0) } + + } \ No newline at end of file