COMP 1601 2021W Assignment 3 Solutions 1. [2] Add debugging log messages that report on activity lifecycle events. The log key should be "Converter2A". First, import the Log class: import android.util.Log Then, define the application name at the top of the MainActivity class (so we can reuse this code again later more easily): private val appName = "Converter2A" Then add the following code to the end of the MainActivity class (between lines 84 and 85): override fun onDestroy() { super.onDestroy() Log.d(appName, "Destroyed") } override fun onResume() { super.onResume() Log.d(appName, "Resumed") } override fun onPause() { super.onPause() Log.d(appName, "Paused") } override fun onStart() { super.onStart() Log.d(appName, "Started") } override fun onStop() { super.onStop() Log.d(appName, "Stopped") } override fun onRestart() { super.onRestart() Log.d(appName, "Restarted") } 2. [2] Add a Farenheit to Kelvin conversion, making sure that the conversion fails if the converted value would be less than 0 Kelvin (as nothing can be colder than absolute zero). What does the app do when you give it -500F and you ask it to convert to Kelvin (with no other changes)? Why? Add this entry to the conversions map (between lines 40 and 41 of Converter.kt): "F to K" to Converter("Farenheit", "Kelvin", fun (F: Double): Double? { val K = ((F - 32.0)*(5.0/9.0) + 273.15) if (K >= 0.0) { return K } else { return null } }), If you try converting -500F to K, you'll get the message "Converting -500.0 Farenheit to Kelvin failed.". This happens because this code returns null when returning a Kelvin value less than zero, and the return value is tested for this in formatConversion (line 12) and based on this it returns the failed string (line 15). 3. [2] Add an entry to the menu "Cars to Cows" that has no corresponding conversion by modifying the conversions map. Add the following to the conversions map (say between lines 40 and 41 in Converter.kt): "Cars to Cows" to null, That's it! 4. [4] The "Cars to Cows" conversion still allows input to be entered, and that input is ''sometimes'' converted to another value. Why does this happen? How can you change the code so the input field is only active when a valid menu selection is made? If a previous conversion has been selected, conv in MainActivity is then non-null and will be used. However, if "Cars to Cows" is the first one selected, then conv will be null, and no conversion will take place. To make the input field only be visible when a valid entry is selected, replace lines 36-44 with the following: if (c != null) { conv = c result.setText("You picked ${convName}") fromStringW.setText("") fromStringW.setVisibility(View.VISIBLE); } else { result.setText("Invalid Menu Selection") fromStringW.setVisibility(View.INVISIBLE); } Here we set the visibility appropriately based on whether the selected entry is valid or not. 5. [2] Converter2A's layout is messed up when in landscape mode (the device turned horizontally). Why? What is wrong with the current layout? The problem is the fixed width and height of the TextView. If you change the layout width and height to be "wrap_content" (lines 37 and 38) the layout works fine in portrait and landscape modes. 6. [2] Fix Converter2A's layout by converting it to a LinearLayout. Just add a LinearLayout node and make the existing widgets children of it (after removing the absolute size constraints on the TextView). Below is what I got. Note the menu and the entry field are now are on the left of the screen; that is fine. (If you did better, please share!)