Implementing several interfaces in a class

Report a typo

Suppose we're coding software that allows us to manage tables.
Say, we're given:

// Interface which allows us to index
// any class that implements it
interface Listed {
    val index: String
}

// This interface derives from Listed, since
// anything that is tabulated already has
// an index, just a different one --
// row and column instead of a number.
interface Tabulated : Listed {
    val row: Int
    val column: Int
    val dataType: String
    val isEmpty: Boolean

    override val index: String
}

// This interface allows us to store color
// information in anything that implements it
interface Colored {
    val color: Color
}

Your task is to complete the interface TableCell, which should derive from Tabulated and Colored, since we might want any cell in the table to have a color for nice data visualization. Basically, you need to create a property which returns index and color data about a table cell.

Provide the interface with a property info, which should return (or get()) the info about TableCell in the following format:

// Code:
// We need to test our interface, so let's create an implementation:
class Cell(
override val row: Int,   
override val column: Int,   
override val dataType: String,   
override val isEmpty: Boolean,   
override val color: Color) : TableCell  

// Initialize colors:
val red = Color(1)  
val blue = Color(2)  
val green = Color(3)  

// Create colored cells
val redCell = Cell(5, 2, "string", false, red)  
val blueCell = Cell(2, 6, "string", false, blue)  
val greenCell = Cell(1, 4, "string", false, green)  

// Get their info!
println(redCell.info)  
println(blueCell.info)  
println(greenCell.info)
// Output

2, 5; Color: 1
6, 2; Color: 2
4, 1; Color: 3
Write a program in Kotlin
// Do not change the code below!

data class Color(val code: Int) {
override fun toString() = "$code"
}

interface Listed {
val index: String
}

interface Tabulated : Listed {
val row: Int
val column: Int
val dataType: String
val isEmpty: Boolean

override val index: String
}

interface Colored {
val color: Color
}

// Do not change the code above!

// You probably want to inherit from something...

interface TableCell {
// Your code here
}
___

Create a free account to access the full topic