COMP 1601 2021W Tutorial 5 Solutions 1. Change the format of the output Celsius to have four significant digits rather than a fixed two decimal places. Change line 29 to be result.setText(String.format("%s Farenheit is %.4g Celsius", Fs, C)) Note the change of "%.2f" to "%.4g". 2. Add a line below the Celsius conversion that gives the temperature in Kelvin. First, change activity_main.xml, adding in the following between lines 29 and 30 (at the end of LinearLayout): This creates the area where the Kelvin conversion message will appear. We then need to change MainActivity to update this message when appropriate. First, add a declaration for resultKelvin between lines 12 and 13 (beginning of onCreate): private lateinit var resultKelvin: TextView Next, we set its value, adding a line between 16 and 17 (again, in onCreate): resultKelvin = findViewById(R.id.resultKelvin) And finally, we set its value in the afterTextChanged method of the watcher object. Insert the following between lines 29 and 30: resultKelvin.setText(String.format("%s Farenheit is %.2f Kelvin", Fs, C + 273.15)) Note that this addition required nine lines of changes (5 to create the TextField and 4 to make it work). In Tutorial 1 this change required the addition of two lines. 3. Make the temperatures bold only (the rest of the text should be as it was). Use fromHtml and the HTML bold tag b, e.g., "<b>32</b>". Use 0 for options. Replace line 29 of MainActivity with the following: result.setText(fromHtml(String.format("%s Farenheit is %.4g Celsius", Fs, C), 0)) For this to compile, you need the following imports added to the top of the file: import androidx.core.text.HtmlCompat.fromHtml as fromHtml 4. Add a miles to kilometers converter to the same screen. It should be below the existing Fahrenheit converter. The input for miles and Fahrenheit should be separate. First, let's change the name of the app. Modify strings.xml: Temperature & Distance Converters (This string is used in AndroidManifests.xml for android:label) Then, add an EditText and TextView to activity_main.xml (again, between 29 and 30 or after the Kelvin result). Below we also add some space so it doesn't look so crowded: And then we duplicate the code to make these fields work. We add in declarations for them to the top of the MainActivity class: private lateinit var editDistance: EditText private lateinit var distanceResult: TextView Initialize them in the OnCreate method: distanceResult = findViewById(R.id.distanceResult) editDistance = findViewById(R.id.editDistance) editDistance.addTextChangedListener(distanceWatcher) And define distanceWatcher, a duplicate of watcher with changes to refer to the new fields and do the new conversion: private val distanceWatcher = object : TextWatcher { override fun afterTextChanged(s: Editable) { val ms = editDistance.getText().toString() var miles = ms.toDoubleOrNull() if (miles == null) { distanceResult.setText("Please enter a number to convert.") } else { val km = miles * 1.60934 distanceResult.setText(String.format("%s miles is %.2f kilometers", ms, km)) } } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } } 5. What are the keywords used in the MainActivity class? What are the explicit types? What are the implicit types? There are lots, so don't worry if you can't get them all. However, try to identify as many of them as you can. keywords: private, lateinit, var, val, override, super, fun, object, if, else, null explicit types: AppCompatActivity, EditText, TextView, Bundle?, TextWatcher, Editable, CharSequence?, Int implicit types: String (Fs), Double (F), 6. Out of the ones you identify, which keywords and types seem mysterious? The explicit types should be pretty mysterious (except for Int) as they each refer to something reasonably complex.