1+ package math
2+
3+ import java.lang.IllegalArgumentException
4+ import kotlin.math.pow
5+
6+ /* *
7+ * Calculate the area of a rectangle
8+ *
9+ * @param length length of rectangle
10+ * @param width width of rectangle
11+ * @return area of given rectangle
12+ */
13+ fun areaOfARectangle (length : Double , width : Double ) = when {
14+ length > 0 && width > 0 -> length * width
15+ else -> throw IllegalArgumentException (" Length and Width must be positive" )
16+ }
17+
18+ /* *
19+ * Calculate the area of a square
20+ *
21+ * @param sideLength side length of square
22+ * @return area of given square
23+ */
24+ fun areaOfASquare (sideLength : Double ) =
25+ when {
26+ sideLength > 0 -> sideLength * sideLength
27+ else -> throw IllegalArgumentException (" Side Length must be positive" )
28+ }
29+
30+ /* *
31+ * Calculate the area of a triangle
32+ *
33+ * @param base base of triangle
34+ * @param height height of triangle
35+ * @return area of given triangle
36+ */
37+ fun areaOfATriangle (base : Double , height : Double ) =
38+ when {
39+ base > 0 && height > 0 -> base * height / 2
40+ else -> throw IllegalArgumentException (" Base and Height must be positive" )
41+ }
42+
43+ /* *
44+ * Calculate the area of a circle
45+ *
46+ * @param radius radius of circle
47+ * @return area of given circle
48+ */
49+ fun areaOfACircle (radius : Double ) =
50+ when {
51+ radius > 0 -> Math .PI * radius.pow(2.0 )
52+ else -> throw IllegalArgumentException (" Radius must be positive" )
53+ }
0 commit comments