To Lecture Notes

IT 372 -- May 20, 2026

Review Exercises

  1. Check that the last parameter of the JPC Button element is named content, which accepts a lambda function. Verify that this parameter can be written in trailing lambda notation.
    Answer: Here is a Button element calling its onClick and content parameters:
    Button(onClick = { /*Button action.*/ },
        content = { Text(text = "Button Text") })
    
    Here is the same Button element written using trailing lambda notation:
    Button(onClick = { /*Button action.*/ }) {
        Text(text = "Button Text") 
    }
    
  2. Run the JPCEx ClickCounter2 Example. Click the button several times, then rotate the emulator. Notice that the click count is reset to 0. In a JPC app, how can we save the count in a Bundle and restore it when the app is recreated and restarted?
    Answer: when defining a state variable, replace remember with s like this:
    var newCaption = rememberSaveable { mutableStateOf("Click Me") }
    
  3. For the TheRaven Example, we used the Scanner class, which comes from Java. Test these alternative Kotlin versions for reading from a file in the raw folder:
    // Read the entire file into a string:
    val s = inputStream.bufferedReader().use { 
        it.readText( ) 
    }
    
    // Read the file line by line:
    var s = ""
    inputStream.bufferedReader( ).useLines { 
        lines -> lines.forEach { 
            line -> s += line + "\n"
        } 
    }
    // Or
    var s = ""
    inputStream.bufferedReader( ).useLines { 
        it.forEach { 
            s += it + "\n"
        } 
    }
    
    Look at the WriteToFile JPC Example to see the last construct used.
  4. Create a practice JPC app with a CustomButton composable with two parameters:
    caption : String,
    clickListener : ( ) -> Unit
    
    Use CustomButton to add three buttons to the app. Also add a Text element. The first button should have the caption "Button A" and its click listener should show the message "Button A clicked" in the Text element. Same for buttons B and C.
    Answer: here is the MainActivity.kt file.

JetPack Compose Examples