Complete the code to declare a sealed class named Shape.
sealed class [1]
The sealed class must be named exactly Shape as per the instruction.
Complete the code to declare a subclass Circle inside the sealed class Shape.
sealed class Shape { data class [1](val radius: Double) : Shape() }
The subclass inside Shape should be named Circle as per the instruction.
Fix the error in the when expression to handle all Shape subclasses without else.
fun area(shape: Shape): Double = when(shape) {
is Shape.Circle -> 3.14 * shape.radius * shape.radius
is Shape.Square -> shape.side * shape.side
[1]
}To avoid using else, all subclasses must be handled explicitly. Adding is Shape.Triangle covers another subclass.
Fill both blanks to declare two subclasses Rectangle and Triangle inside sealed class Shape.
sealed class Shape { data class [1](val length: Double, val width: Double) : Shape() data class [2](val base: Double, val height: Double) : Shape() }
The subclasses are named Rectangle and Triangle as per the instruction.
Fill all three blanks to create a when expression that returns area for each Shape subclass.
fun area(shape: Shape): Double = when(shape) {
is Shape.Circle -> 3.14 * shape.radius * shape.radius
is Shape.Rectangle -> shape.length * shape.width
is Shape.Triangle -> [1] * shape.base * shape.height
}The area of a triangle is 0.5 times base times height.