Answer List

Bob
Bob in Kotlin
object Bob {
    private val answers =
            listOf("Whatever.", "Sure.", "Whoa, chill out!", "Calm down, I know what I'm doing!")

    fun hey(input: String): String {
        val msg = input.trim()
        if (msg.isEmpty()) return "Fine. Be that way!"
        val isQuestion = if (msg.endsWith('?')) 1 else 0
        val isYelling = if (('A'..'Z').any { msg.contains(it) } && msg == msg.uppercase()) 2 else 0
        return answers[isQuestion + isYelling]
    }
}

In this approach you define a List that contains Bob’s answers, and each condition is given a score. The correct answer is selected from the List by using the score as the List index.

An object declaration is used to define Bob as essentially a singleton object instantiation of the class. This is sufficient, since there is no object state that needs to change with each call of the hey method.

The listOf method is used to create a list of Bob's answers.

The String trim() method is applied to the input to eliminate any whitespace at either end of the input. If the string has no characters left, it returns the response for saying nothing.

Caution

Note that a null string would be different from a String of all whitespace. A null String would throw a NullPointerException if trim() were applied to it.

A question is determined by use of the endsWith() method to see if the input ends with a question mark.

A range of A..Z is defined to look for uppercase English alphabetic characters.

The first half of the isYelling implementation

if (('A'..'Z').any { msg.contains(it) }

is constructed from the range and the any method to ensure there is at least one uppercase letter character in the String. The lambda of any uses the it keyword to refer to the single Char parameter for the lambda, and passes it to the contains method to check if the character is in the input String. The check that there is any alphabetic character is needed, because the second half of the condition tests that the uppercased input is the same as the input. If the input were only "123" it would equal itself uppercased, but without letters it would not be a yell.

The conditions of being a question and being a yell are assigned scores through the use of the ternary expression. For example, giving a question a score of 1 would use an index of 1 to get the element from the answers List, which is "Sure.".

isYelling isQuestion Index Answer
false false 0 + 0 = 0 "Whatever."
false true 0 + 1 = 1 "Sure."
true false 2 + 0 = 2 "Whoa, chill out!"
true true 2 + 1 = 3 "Calm down, I know what I'm doing!"
23rd Oct 2024 · Found it useful?