To Lecture Notes

IT 372 -- Apr 21, 2025

Review Questions

  1. List three ways of converting an int value into a string.
    Answer: if the int n is defined by
    int n = -376283;
    
    here are three ways of converting n to the string s:
    // Method 1:
    String s = String.valueOf(n);
    // Method 2:
    String s = n + "";
    // Method 3:
    String s = String.format("%d", n);
    
  2. The following code copies the items from an arraylist declared by
    ArrayList<Integer> al = new ArrayList<Integer>( );
    
    into a string:
    String output = "";
    for(Integer n : al) {
        output += n + "\n";
    }
    
    Perform this operation using a StringBuilder collection instead. If there are many items in the arraylist, using a StringBuilder collection is much faster:
    StringBuilder sb = new StringBuilder("");
    String output = "";
    for(Integer n : a1) {
        a1.append(n + "\n");
    }
    
  3. Suppose you have a HashMap collection:
    HashMap<String, Integer> map = 
        new HashMap<String, Integer>( );
    
    How do you add an Integer object to the collection using the key "K123"? How do you retrieve the Integer object? We will use a similar concept when we store information from an activity in a storedInstanceState object. Answer:
    // Put the value 35 into the Hashmap using the key "C123".
    map.put("C123", 35);
    // Retrieve the value stored using the key "C123" from the hashmap.
    int n = map.get("C123");
    
  4. Add these strings to an ArrayList collection:
    c  java  kotlin  python  
    
    Then use the ArrayList method replaceAll that accepts a lambda function to convert each of the strings to uppercase. Answer:
    ArrayList<String> list = new ArrayList<String>( );
    list.add("c");
    list.add("java");
    list.add("kotlin");
    list.add("python");
    list.replaceAll( s -> s.toUpperCase( ));
    String output = "";
    for(String s : list) {
        output += s + \n");
    }
    TextView tv = findViewById(R.id.txt_output);
    tv.setText(output);
    
  5. What is an Android drop-down menu called?
    Answer: it is called a Spinner.
  6. Write an app that shows a toast when a LongTouch event occurs in a linear layout. See The Android Toast section in the April 16 Notes.
  7. Create an app named TestWidgets. Switch the layout to LinearLayout, with vertical orientation. Add a ScrollView that includes the linear layout. Then place each of these widgets in the linear layout:
    Switch  ToggleButton  Button
    
    When the button is clicked, show the states of the switch and the toggle button.
    Answer: The layout and activity source code:
        activity_main.xml   MainActivity.java
  8. How do you display special characters in a widget?
    Answer: You can't use HTML entities, but you can use Unicode characters. See the website unicode.org for code charts. For example, the unicode codes for the greek letters alpha (α), beta (β), gamma (γ) are 0BC1, 0BC2, and 0BC3. Here is an attribute that displays these Greek letters in a TextView widget:
    android:text="Greek letters: \u03B1, \u03B2, \u03B3"

The Activity Lifecycle

Some Examples

Projects 3a and 3b