To Lecture Notes

IT 372 -- April 22, 2026

Review Questions

  1. Which event listeners have we used in our Android Views apps examples?
  2. Set up a LinearLayout form to input grocery item data with these fields: description (String), code (int), and price (double). When the Submit Grocery Item button is clicked, contatenate the information to the String variable groceryItems that is displayed on a new activity. Answer:
         activity_main.xml  MainActivity.java
         activity_display_items.xml  DisplayItemsActivity.java
    When the virtual device is rotated, save the groceryItems variable in a Bundle object so that this variable can be restored when the activity is recreated after the activity is destroyed due to device rotation.
    Ans: Main activity after adding onSaveInstanceState method and statements in onCreate to restore bundle.
         MainActivity.java

Kotlin Review Questions

  1. Translate these Java code fragments into Kotlin. Run them in the Kotlin Playground.
    // a.
    int n = 493;
    System.out.println(n);
    
    // b.
    int n = 493;
    System.out.println(
        String.format("Value of n: %d", n));
    
    // c. 
    // The Kotlin if statement and while loop
    // look the same as they do in Java. However,
    // the Elvis operator is translated differently.
    
    // Java version:
    boolean b = true;
    System.out.println(b ? "true" : "false");
    
    // Kotlin version:
    var b = true
    println(if (b) "true" else "false")
    
    // d.
    for(int n = 1; n <= 10; n++) {
        print(n + " ");
    }
    
    // e.
    for(int n = 1; n <= 10; n += 2) {
        print(n + " ");
    }
    
    // f.
    for(int n = 10; n >= 1; n -= 2) {
        print(n + " ");
    }
    
    // g
    // Translate this function into Kotlin
    // and write a main method to test it:
    public String makeGreeting(String firstWord, String name) {
        return firstWord + ", " + name + ", how are you?";
    }
    
  2. Also test this makeGreeting method from Exercise 1g in an Android app, displaying the output (a) in the Logcat window and (b) in a TextView widget.

Introduction to Kotlin

Look at these sections in the Introduction to Kotlin document:
      Constants   Unit  Named Parameters  Arrays   Lists   Classes