Exercise: GUI View Hierarchy Simulation
Implement the following classes and methods, that will represent a view hierarchy in a GUI system:
- Create an open class
View
with primary constructor properties: read-only id
of type String
and read-write isVisible
of type Boolean
. - Implement methods
show
and hide
in the View
class, that will change the isVisible
property to true
and false
respectively. - Create two subclasses of
View
: TextView
and Toggle
. TextView
should have a constructor with parameter id
and property text
, that will represent the text displayed in the view. It should set superclass isVisible
property to true
.Toggle
should have a constructor with parameter id
. It should set superclass isVisible
property to true
.Toggle
should have an additional property isOn
, that will represent the state of the toggle. It should be initially set to false
.Toggle
should have a method click
, that will change the isOn
property to the opposite state.
val view = View(
id = "v1",
isVisible = false,
)
println(view.id) // v1
println(view.isVisible) // false
val textView = TextView(
id = "tv1",
text = "Hello, World!",
)
println(textView.id) // tv1
println(textView.text) // Hello, World!
println(textView.isVisible) // true
textView.text = "Welcome to Kotlin!"
println(textView.text) // Welcome to Kotlin!
textView.hide()
println(textView.isVisible) // false
val toggle = Toggle(
id = "toggle1",
)
println(toggle.id) // toggle1
println(toggle.isOn) // false
toggle.click()
println(toggle.isOn) // true
println(toggle.isVisible) // true
toggle.hide()
println(toggle.isVisible) // false
toggle.show()
println(toggle.isVisible) // true
This problem can either be solved in the below playground or you can clone kotlin-exercises project and solve it locally. In the project, you can find code template for this exercise in essentials/classes/Gui.kt. You can find there example usage and unit tests.
Once you are done with the exercise, you can check your solution here.
import junit.framework.TestCase.assertFalse
import junit.framework.TestCase.assertTrue
import org.junit.Test
import kotlin.test.assertEquals
// TODO
fun main() {
val view = View(
id = "v1",
isVisible = false,
)
println(view.id) // v1
println(view.isVisible) // false
val textView = TextView(
id = "tv1",
text = "Hello, World!",
)
println(textView.id) // tv1
println(textView.text) // Hello, World!
println(textView.isVisible) // true
textView.text = "Welcome to Kotlin!"
println(textView.text) // Welcome to Kotlin!
textView.hide()
println(textView.isVisible) // false
val toggle = Toggle(
id = "toggle1",
)
println(toggle.id) // toggle1
println(toggle.isOn) // false
toggle.click()
println(toggle.isOn) // true
println(toggle.isVisible) // true
toggle.hide()
println(toggle.isVisible) // false
toggle.show()
println(toggle.isVisible) // true
}
class GuiTests {
@Test
fun `view show should set isVisible to true`() {
val view = View("view1", false)
view.show()
assertTrue(view.isVisible)
}
@Test
fun `view hide should set isVisible to false`() {
val view = View("view2", true)
view.hide()
assertFalse(view.isVisible)
}
@Test
fun `toggle click should toggle isOn state`() {
val toggle = Toggle("toggle1")
toggle.click()
assertTrue(toggle.isOn)
toggle.click()
assertFalse(toggle.isOn)
}
@Test
fun `text view setText should update text property`() {
val textView = TextView("textView1", "Hello")
textView.text = "World"
assertEquals("World", textView.text)
}
@Test
fun `toggle should inherit properties from view`() {
val toggle = Toggle("toggle2")
assertTrue(toggle.isVisible)
toggle.hide()
assertFalse(toggle.isVisible)
}
@Test
fun `text view should inherit properties from view`() {
val textView = TextView("textView2", "Hello")
assertTrue(textView.isVisible)
}
@Test
fun `toggle should start with isOn set to false`() {
val toggle = Toggle("toggle3")
assertFalse(toggle.isOn)
}
@Test
fun `toggle should toggle isOn state correctly`() {
val toggle = Toggle("toggle4")
toggle.click()
assertTrue(toggle.isOn)
toggle.click()
assertFalse(toggle.isOn)
toggle.click()
assertTrue(toggle.isOn)
}
@Test
fun `view show and hide interactions`() {
val view = View("view3", false)
view.show()
assertTrue(view.isVisible)
view.hide()
assertFalse(view.isVisible)
}
@Test
fun `text view setText and visibility interactions`() {
val textView = TextView("textView3", "Hello")
textView.text = "World"
assertEquals("World", textView.text)
textView.hide()
assertFalse(textView.isVisible)
}
}
Marcin Moskala is a highly experienced developer and Kotlin instructor as the founder of Kt. Academy, an official JetBrains partner specializing in Kotlin training, Google Developers Expert, known for his significant contributions to the Kotlin community. Moskala is the author of several widely recognized books, including "Effective Kotlin," "Kotlin Coroutines," "Functional Kotlin," "Advanced Kotlin," "Kotlin Essentials," and "Android Development with Kotlin."
Beyond his literary achievements, Moskala is the author of the largest Medium publication dedicated to Kotlin. As a respected speaker, he has been invited to share his insights at numerous programming conferences, including events such as Droidcon and the prestigious Kotlin Conf, the premier conference dedicated to the Kotlin programming language.