5. Kotlin Fundamentals

Note: we assume here that you have already done either Kotlin From The Command Line or Using Kotlin in IntelliJ IDEA and therefore know how to compile and run Kotlin code.

5.1. Variables & Expressions

Download input.kt into a directory. This contains a helper class to simplify obtaining input from the program user.

In the same directory, create a new file convert.kt. In this file, write a program that

  • Reads a temperature in Celsius from the user

  • Converts that temperature to the Fahrenheit scale

  • Displays both temperatures in pleasing way

Here’s an example of what program input and output should look like when a value of 15.5 is provided for the temperature in Celsius:

Enter a Celsius temperature: 15.5
15.5°C is equivalent to 59.9°F

Note that you can obtain a number from the user with code similar to this:

val input = ConsoleInput()
val x = input.readDouble("Enter a value: ")

Note also that when printing results, you can generate the ‘degree’ symbol using the Unicode escape sequence \u00b0.

If using the command line, you can compile and run your program like this:

kotlinc -include-runtime -d convert.jar convert.kt input.kt
java -jar convert.jar

If you are using IntelliJ, simply put both .kt files in the src folder and the IDE will be able to compile and use them together.

5.2. Loops & Branching

  1. In the same directory (or IntelliJ project) as the previous exercise, create a Kotlinscript file named temptable.kts. In this file, write some Kotlin code that prints out a temperature conversion table from Celsius to Fahrenheit. Your code should use a while loop to generate temperatures between 0.0 and 36.0 degrees Celsius, increasing by 2.0 degrees each time through the loop.

    Remember that you can run this Kotlinscript file on the command line with

    kotlinc -script temptable.kts

    To run it in IntelliJ, choose Run ‣ Run… or press Alt+Shift+F10. IntelliJ will create a new run configuration for the file, which you can subsequently invoke by clicking on the green triangle, pressing Shift+F10, etc.

  2. Write some Kotlin code to demonstrate for loops that

    • Count from 0 up to 5

    • Count from 0 up to, but not including, 5

    • Count down from 10 to 1

    • Count down from 10 to 1 in steps of 3

    Use range expressions to write your loops. Remember that 1..10 represents the range from 1 to 10 inclusive of the end point. You can use until instead of the .. operator if you wish to exclude the end point. You can use downTo to count down instead of up, and you can use step to specify step sizes other than 1.

  3. In a new directory (or a new IntelliJ project), copy the input.kt file used earlier. Then create alongside it a new file named passcheck.kt. In this file, write a Kotlin program that

    • Reads a passphrase from the user

    • Reports that the passphrase is too short if its length is less than 8 characters

    Use the readLine method of a ConsoleInput object to read in the passphrase as a string. Use an if expression to check passphrase length.

    Compile and run in the usual way on IntelliJ, or via the following if working on the command line:

    kotlinc -include-runtime -d passcheck.jar passcheck.kt input.kt
    java -jar passcheck.jar
  4. Extend passcheck.kt so that it separately counts the occurrences of letters, digits and whitespace characters. After doing this, the program should report cases where there are no letters, no digits or no whitespace characters appearing in the passphrase. Here’s an example of how the final program should behave:

    Enter a passphrase: hello
    Too short!
    Must contain at least 1 digit
    Must contain at least 1 whitespace character

    Use a for loop to iterate over the characters of the string. Within the for loop, use a when expression to check the type of each character. You can do the type checking using the methods isLetter, isDigit and isWhitespace.

5.3. String Manipulation

  1. Write some Kotlin code to explore

    • Accessing characters in a string using []

    • Extracting a substring using the slice or substring methods

    • Use of take and takeLast methods to extract the first or last N characters

    Use the documentation for the String class to help you.

  2. Write some Kotlin code that defines a string variable, prints its value and then creates a transformed version of the string. This new version should have

    • Any uppercase letter replaced by its lowercase version

    • Whitespace characters replaced by underscores

    • Occurrences of the letter ‘o’ replaced by the digit ‘0’

    The program should print the transformed string and then print the value of original string variable again, to show that it hasn’t changed.

    Use two different methods of the String class to do this.