Multiple inheritance: interface details for a given class

Report a typo

Suppose you're programming a structure which would benefit from generalization. Having an interface which allows you to index your entities allows you to create all sorts of things – lists, queues, and hashes. What if you would like to have a matrix? (It's a mathematical version of a 2-dimensional array, nothing complicated. Or a mathematical representation of a data table.)

Of course, you could index each element with a single number, but it would be more convenient to refer to the elements of a matrix (or array) via rows and columns rather than a single index.

interface Listed {
    val index: String  
}

interface Tabulated : Listed {
    // Your code here
}

data class Element(
    override val column: Int,  
    override val row: Int,
    var value: Int
) : Tabulated

Using what you know about interfaces, make sure that the Element's of the matrix are still indexed, but in an appropriate way.

Your task is to complete the Tabulated interface so that the Element class is implemented correctly and the number property of an Element provides correct information regarding the element's row and column:

// code
val cell1 = Element(1, 2, 10)
val cell2 = Element(2, 4, 15)
val cell3 = Element(4, 5, 92372)

println(cell1.index)
println(cell2.index)
println(cell3.index)
// output of the code above

1, 2
2, 4
4, 5
Write a program in Kotlin
// Do not change the code below.

interface Listed {
val index: String
}

data class Element(
override val column: Int,
override val row: Int,
var value: Int
) : Tabulated

// Do not change the code above.

interface Tabulated : Listed {
// Your Code Here
}
___

Create a free account to access the full topic