Skip to content

Instantly share code, notes, and snippets.

@pufface
Last active May 4, 2019 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pufface/1f7a1fd56c247686159503eb640b7943 to your computer and use it in GitHub Desktop.
Save pufface/1f7a1fd56c247686159503eb640b7943 to your computer and use it in GitHub Desktop.
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