COMP 1601 2021W Tutorial 6 Solutions 1. Compare the Kotlin and Swift versions of Conversions. How does the Converter class differ? How does the conversions dictionary/map differ? And, how are they the same? What does this tell you about the similarity and differences between the languages? Below is Swift vs Kotlin differences: * struct vs. class declaration * func vs fun name function declarations * removing _ from function argument declarations (all Kotlin function declarations act like they are Swift declarations with an underscore). * struct/class initializer: init vs constructor * String interpolation: \() vs ${} * constant declarations: let vs val * current object/struct references: self vs this * function/closure/lamda return values: - Swift uses -> for named function return values, "in" for anoymous function return values - Kotlin uses : for name function return values, -> for anonymous function return values * anonymous function declarations in Kotlin can just start with "fun (args)", in Swift func always requires a name * Kotlin seems to stricter about function prototypes with optional/nullable types, e.g. InToCM() returns Double in the Swift version while returning Double? in the Kotlin version. Note that Converter in both specifies that convert should return Double?. * dictionaries vs maps: they are accessed with the same syntax, but Swift uses [key: value, key: value] to declare, while Kotlin has mapOf(key to val, key to val) syntax. Note however these differences are mostly syntactical. Assignment, optional/nullable types, conditionals, implicit types, and many others are essentially the same. (There is much more to say on similarities and differences, the point of this question is to show that the languages are remarkably similar, even if there are several minor syntactic differences.) 2. What is the flow of control of MainActivity? How does this flow of control compare to that of ContentView in Converter2 from Tutorial 2? The flow of control of MainActivity is mostly explicit, in that we define everything through methods that are called at well-defined times. onCreate() is called when the activity is created, onMenuItemClick() when a menu item is clicked, and showMenu() is called when the menu button is clicked. The watcher object's methods are called when the status of the EditText widget changes; we register this handler object with addTextChangedListener() when the activity is created (in onCreate()). In contrast, it is not at all clear when code is run in the SwiftUI version. We know that the menu button action closures are run when the buttons are clicked, but we don't really know when (or really, how) the rest of the code runs. SwiftUI says views are updated when they need to be, such as when their state changes (i.e., when a state variable changes). But with SwiftUI we declare relationships between UI elements rather than specifying a flow of control. 3. At the top of MainActivity (lines 15-18) four properties are declared. Are all of these used in a meaningful way? Explain how each is used. result refers to the TextView widget with the result of the conversion. It is used in afterTextChanged() and onMenuItemClick() to change the message in result. menuButton holds a reference to the button that, when clicked, causes a pop-up menu to appear. This property isn't used. (The menu pop-up is triggered by the onClick property specified in activity_main.xml.) fromStringW refers to the EditText widget which receives the user's input for the number to convert. This property is used to make the input widget visible and invisible, and to get the number the user inputted (and initiate the conversion process). conv holds the current Converter object, if any. It is used by afterTextChanged() to perform the actual conversion and is updated when the user clicks on a menu item, in onMenuItemClick(). 4. Add a conversion for miles to kilometers. How difficult was this compared to modifying Converter2? The added code was the same as with the Swift version, except for the minor syntactic differences as discussed above. Here's what you add to the conversions map (copying the entry of km to mi): "mi to km" to Converter("miles", "kilometers", fun (m: Double): Double? { val k = m / 0.6213712 return k }), It was super easy, barely an inconvenience. 5. The setText on line 38 often doesn't produce visible output. What could you do so the message is displayed until something is typed? One way is to check the result string to see if it has the "You picked" message. We can do this by replacing line 67 with the following: val currentMsg = result.getText().toString() if (currentMsg.substring(0,3) != "You") { result.setText("Please enter a number to convert.") } 6. Add a second menu to the bottom of the screen, identical to the one at the top. How hard was this to do? Adding a new button is simple. We just have to add an entry to the end of the layout, changing the id to be something different. The only tricky part is to add it to the bottom of the screen. With the fixed constrained layout, the following works. Note that I had to manually edit the XML, I couldn't figure out how to get the "constraintBottom_toBottomOf" option with the GUI (but I'm sure there's a way to do it).