Wednesday, July 3, 2019

Kotlin in Action: Chapter 3 Exercises

I'm currently reading through the book Kotlin in Action, which is a great intro to Kotlin that assumes that you know Java and builds off of your Java knowledge. With that in mind, I thought it would be worthwhile to come up with a couple of test exercises as I go through the book, where you're given some code in Java, and you need to convert it to Kotlin (without using the Java to Kotlin auto converter). So here's a couple small exercises that you should be able to complete once you've gotten through chapter 3 of the book.

Exercise 1: Roman Numerals

This is a simple bit of code that takes the numbers 1 through 100 and converts them to Roman Numerals and print them to the console. It will also compare those Roman Numerals to a test list and print out a "Passed" or "Failed" depending on if the generated Roman Numerals match the list. (Code based loosely off of the stack overflow answer here.)

import java.util.List;
import java.util.TreeMap;

import static java.util.Arrays.asList;

public class JavaExample1 {

    // This can only be used to test your results.
    private static List<String> testList = asList(
        "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X",
        "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX",
        "XX", "XXI", "XXII", "XXIII", "XXIV", "XXV", "XXVI", "XXVII",
        "XXVIII", "XXIX", "XXX", "XXXI", "XXXII", "XXXIII", "XXXIV",
        "XXXV", "XXXVI", "XXXVII", "XXXVIII", "XXXIX", "XL", "XLI",
        "XLII", "XLIII", "XLIV", "XLV", "XLVI", "XLVII", "XLVIII",
        "XLIX", "L", "LI", "LII", "LIII", "LIV", "LV", "LVI", "LVII",
        "LVIII", "LIX", "LX", "LXI", "LXII", "LXIII", "LXIV", "LXV",
        "LXVI", "LXVII", "LXVIII", "LXIX", "LXX", "LXXI", "LXXII",
        "LXXIII", "LXXIV", "LXXV", "LXXVI", "LXXVII", "LXXVIII",
        "LXXIX", "LXXX", "LXXXI", "LXXXII", "LXXXIII", "LXXXIV",
        "LXXXV", "LXXXVI", "LXXXVII", "LXXXVIII", "LXXXIX", "XC", "XCI",
        "XCII", "XCIII", "XCIV", "XCV", "XCVI", "XCVII", "XCVIII",
        "XCIX", "C");

    private static TreeMap<Integer, String> romanNumeralMap =
            new TreeMap<>();
    static {
        romanNumeralMap.put(1, "I");
        romanNumeralMap.put(4, "IV");
        romanNumeralMap.put(5, "V");
        romanNumeralMap.put(9, "IX");
        romanNumeralMap.put(10, "X");
        romanNumeralMap.put(40, "XL");
        romanNumeralMap.put(50, "L");
        romanNumeralMap.put(90, "XC");
        romanNumeralMap.put(100, "C");

    }

    public static void main(String[] args) {
        boolean passed = true;
        for (int i = 1; i <= 100; i++) {
            String romanNumeral = toRomanNumeral(i);
            System.out.println(romanNumeral);
            passed &= romanNumeral.equals(testList.get(i-1));
        }
        System.out.println(passed ? "Passed" : "Failed");
    }

    private static String toRomanNumeral(int num) {
        int floorKey = romanNumeralMap.floorKey(num);
        if (num == floorKey) {
            return romanNumeralMap.get(num);
        }
        return romanNumeralMap.get(floorKey) +
                toRomanNumeral(num - floorKey);
    }
}

Exercise 2: SQL Query String

This exercise simply has a basic pgSQL query in a string that pulls in the table name from a constant. Can you rework it to use a Kotlin multiline String?
public class JavaExample2 {

    private static final String TABLE_NAME = "blog_posts";

    public static void main(String[] args) {
        String query = "SELECT id, data \n" +
                "FROM " + TABLE_NAME + " \n" +
                "WHERE data->>'userId' = ?";
        System.out.println(query);
    }
}
Answers will be put out in a follow up blog post.

And here are the answers: Kotlin in Action: Answers to Chapter 3 Exercises