
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 Viewwith primary constructor properties: read-onlyidof typeStringand read-writeisVisibleof typeBoolean.
- Implement methods showandhidein theViewclass, that will change theisVisibleproperty totrueandfalserespectively.
- Create two subclasses of View:TextViewandToggle.
- TextViewshould have a constructor with parameter- idand property- text, that will represent the text displayed in the view. It should set superclass- isVisibleproperty to- true.
- Toggleshould have a constructor with parameter- id. It should set superclass- isVisibleproperty to- true.
- Toggleshould have an additional property- isOn, that will represent the state of the toggle. It should be initially set to- false.
- Toggleshould have a method- click, that will change the- isOnproperty to the opposite state.
Example usage:
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.
Playground
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) } }