Last active
May 4, 2019 19:00
-
-
Save pufface/1f7a1fd56c247686159503eb640b7943 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Shape { | |
fun area(): Double | |
fun perimeter(): Double | |
} | |
class Square(val side: Double) : Shape { | |
override fun area() = side * side | |
override fun perimeter() = 4 * side | |
} | |
class Circle(val radius: Double) : Shape { | |
override fun area() = radius * radius * Math.PI | |
override fun perimeter() = 2 * Math.PI * radius | |
} | |
class Rectangle(val width: Double, val height: Double) : Shape { | |
override fun area() = height * width | |
override fun perimeter() = 2 * height + 2 * width | |
} | |
fun main() { | |
val shapes = listOf( | |
Square(5.0), | |
Circle(4.0), | |
Rectangle(6.0, 7.0) | |
) | |
val totalArea = shapes.sumByDouble { it.area() } | |
println("Total area: $totalArea") | |
val totalPerimeter = shapes.sumByDouble { it.perimeter() } | |
println("Total perimeter: $totalPerimeter") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment