Complete the code to declare a sealed class named Shape.
sealed class [1]
The sealed class must be named Shape exactly as intended.
Complete the code to define a data class Circle inheriting from Shape with a radius property.
data class Circle(val radius: Double) : [1]()
The class Circle must inherit from the sealed class Shape.
Fix the error in the when expression to make it exhaustive for Shape.
fun area(shape: Shape): Double = when(shape) {
is Circle -> Math.PI * shape.radius * shape.radius
is Rectangle -> shape.width * shape.height
[1]
}Adding else makes the when expression exhaustive if not all subclasses are covered.
Fill both blanks to create a when expression that is exhaustive without else, covering all subclasses.
fun describe(shape: Shape): String = when(shape) {
is Circle -> "Circle with radius ${shape.radius}"
[1] -> "Rectangle with width ${shape.width} and height ${shape.height}"
[2] -> "Square with side ${shape.side}"
}To make the when exhaustive without else, all subclasses must be covered explicitly.
Fill all three blanks to create a sealed class hierarchy and an exhaustive when expression that returns area.
sealed class [1] data class Circle(val radius: Double) : [1]() data class Rectangle(val width: Double, val height: Double) : [1]() fun area(shape: [1]): Double = when(shape) { is Circle -> Math.PI * shape.radius * shape.radius is Rectangle -> shape.width * shape.height }
All classes inherit from the sealed class Shape, and the function uses it as parameter type.