Computer scienceProgramming languagesKotlinObject-oriented programmingObject-oriented programming usage

Sealed class and interface

Shape area

Report a typo

You have a sealed class called Shape. Declare a function called calculateArea(), which takes the sealed class as a parameter and returns the area as a Double.

You can use the following formulas:

Square A=a2A = {a^2} where a is the side.

Rectangle A=wlA = {w l} where w is the width and l is the length.

Circle A=πr2A = {πr^2} where r is the radius.

Triangle A=hbb/2A = {hb_b/2} where b is the base and hbh_b is the height.

Pentagon A=145(5+25)×a2A = {\frac{1}{4}{\sqrt{5(5+2\sqrt5)} }} \times a^2 where a is the side.

Write a program in Kotlin
// Add calculateArea() here

// Don't change this code
sealed class Shape {
data class Rectangle(val width: Double, val height: Double) : Shape()
data class Square(val side: Double) : Shape()
data class Circle(val radius: Double) : Shape()
data class Triangle(val base: Double, val height: Double) : Shape()
data class Pentagon(val side: Double) : Shape()
}
___

Create a free account to access the full topic