This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckBox(){ | |
var isChecked: Boolean = false | |
fun click(){ | |
isChecked = !isChecked | |
} | |
} | |
class TextView(private var text: String){ | |
fun click(){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(){ | |
val mementoManager = LongMementoManager() | |
println("Start") | |
println("Fibonacci at rank 1000 is: ${fibonacci(1000, mementoManager)}") | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun fibonacci(rank: Int, mementoManager: LongMementoManager): Long{ | |
val result = when(rank){ | |
0 -> 0L | |
1 -> 1L | |
else -> { | |
val rankMinus1 = fibonacci(rank-1, mementoManager) | |
val rankMinus2 = mementoManager.getMemento(rank-2)?.restore()?:0L | |
rankMinus1 + rankMinus2 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LongMementoManager{ | |
private val mementos = hashMapOf<Int, LongMemento>() | |
fun saveMemento(index: Int, memento: LongMemento){ | |
mementos[index] = memento | |
} | |
fun getMemento(index: Int): LongMemento?{ | |
return mementos[index] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LongMemento{ | |
private var numberToSave: Long = 0L | |
fun save(number: Long): LongMemento{ | |
numberToSave = number | |
return this | |
} | |
fun restore(): Long = numberToSave | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun fibonacci(rank: Int): Long{ | |
return when(rank){ | |
0 -> 0L | |
1 -> 1L | |
else -> fibonacci(rank-1).toLong() + fibonacci(rank-2).toLong() | |
} | |
} | |
fun main(){ | |
println("Start") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Calculator{ | |
private var result = 0 | |
private var currentOperation = "0" | |
fun addNumber(number: Int){ | |
result += number | |
currentOperation = "$currentOperation + $number" | |
} | |
fun getResult() = result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(){ | |
val thai = RadioButton("Are you thai?") | |
thai.radioButtonMediator = RadioButtonMediator { checked -> | |
if(checked){ | |
println("ผมพูดภาษาไทยได้ครับ") | |
} | |
else{ | |
println("I can speak english.") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun interface RadioButtonMediator{ | |
fun notify(checked: Boolean) | |
} |