Lesson 12 of 15
Case Classes
Case Classes
Case classes are immutable data containers with built-in equals, hashCode, and toString:
case class Point(x: Int, y: Int)
val p = Point(3, 4)
println(p.x) // 3
println(p.y) // 4
Methods on Case Classes
case class Circle(radius: Double) {
def area: Double = 3.14159 * radius * radius
def circumference: Double = 2 * 3.14159 * radius
}
val c = Circle(5.0)
println(c.area)
Your Task
Define a case class Rectangle(width: Int, height: Int) with two methods:
area: Int— returnswidth * heightperimeter: Int— returns2 * (width + height)
JS Transpiler loading...
Loading...
Click "Run" to execute your code.