=================== Kotlin Fundamentals =================== Note: we assume here that you have already done either :doc:`/cmdline/main` or :doc:`/intellij/main` and therefore know how to compile and run Kotlin code. Variables & Expressions ======================= .. highlight:: none Download :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 :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: .. code-block:: text 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: .. code-block:: kotlin 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 :file:`src` folder and the IDE will be able to compile and use them together. Loops & Branching ================= #. In the same directory (or IntelliJ project) as the previous exercise, create a Kotlinscript file named :file:`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 .. code-block:: text kotlinc -script temptable.kts To run it in IntelliJ, choose :menuselection:`Run --> Run...` or press :kbd:`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 :kbd:`Shift+F10`, etc. #. 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. #. In a new directory (or a new IntelliJ project), copy the :download:`input.kt` file used earlier. Then create alongside it a new file named :file:`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 #. Extend :file:`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``. String Manipulation =================== #. 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. #. 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. .. _Converts that temperature: https://en.wikipedia.org/wiki/Conversion_of_units_of_temperature .. _for loops: https://kotlinlang.org/docs/reference/control-flow.html#for-loops .. _range expressions: https://kotlinlang.org/docs/reference/ranges.html .. _if expression: https://kotlinlang.org/docs/reference/control-flow.html#if-expression .. _when expression: https://kotlinlang.org/docs/reference/control-flow.html#when-expression .. _documentation for the String class: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-string/index.html