Home > Software design >  transformation operations
transformation operations

Time:10-17

I need to calculate the perimeters of these rectangles, my teacher said I can do it using list transformation, but I don't understand how i can done it, please help

 data class Rectangle(val height: Int, val width: Int) {
    val perimeter: Int
        get() = height * 2   width * 2
    val area: Int
        get() = height * width
}

val rectangles = listOf(
    Rectangle(11, 5),
    Rectangle(14, 3),
    Rectangle(5, 6),
    Rectangle(12, 21),
    Rectangle(35, 32),
    Rectangle(10, 12),
    Rectangle(4, 9)
)

CodePudding user response:

You already have the operations for the perimeter, only you have to iterate your list something like this:

for(rectangle in rectangles){
        println(rectangle.perimeter)
    } 
  • Related