To Lecture Notes

IT 372 -- May 8, 2024

Review Exercises

  1. Create an Android app that contains an arraylist that holds Dog objects, defined with this XML diagram:
    +----------------------------+
    |            Dog             |
    +----------------------------+
    | + name : String            |
    | + breed : String           |
    | + age : int                |
    +----------------------------+
    | + Dog(name: String, breed: |
    |       String, age: int)    |
    | + toString( ) : String     |
    +----------------------------+
    
    On the layout, include 3 buttons, each of which adds specific Dog data to the arraylist. Also include a button that displays the list of dogs in the arraylist in a textview widget. Save the arraylist in a bundle that can be used to restore the arraylist when the app is destroyed due to being rotated.
    Answer: activity_main.xml  Dog.java  MainActivity.java
  2. How does the Unicode code (called the code point) for a character differ from the UTF-8 encoding? Why can't you just use straight Unicode to encode characters?
    Ans: Here is an example. How do you interpret the hex Unicode code 266F? If you interpret the two bytes separately 26=& (ampersand) and 6F=o (lowercase o). If you interpret the two bytes together you get 266F= (sharp sign from music). To resolve the ambiguity, use UTF-8. Assume that characters are represented by one byte unless they start with 110, which means two bytes, or they start with 1110, which means three bytes. The modified UTF (MUTF) system used by Android apps does not allow four byte representations.
  3. What are the steps to obtaining the Unicode encoding for a character with a two-byte UTF-8 code?
    Ans: (a) Obtain the hex Unicode code from the unicode.org website, (b) translate the hex Unicode code to binary, (c) group the binary bits into 5 and 6 bytes, omitting leading zeros, (d) insert the UTF-bits 110 to the left of the 5 bit group and insert the bits 10 in front of the 6 bit group, (e) regroup these resulting bits into groups of four and translate back into hex.
  4. Obtain the UTF-8 encoding for the Euro character €, which has a three-byte UTF-8 code. Use this table to help you obtain the encoding:
                 Minimum Maximum Byte 1   Byte 2   Byte 3
                 code    code
                 point   point
    One byte:    U+0000  U+007F  0xxxxxxx 
    Two bytes:   U+0080  U+07FF  110xxxxx 10xxxxxx 
    Three bytes: U+0800  U+FFFF  1110xxxx 10xxxxxx 10xxxxxx
    
    Ans: Look up the Unicode code for € on the unicode.org website. The hex code is 20AC. Since 0800 <= 20AC <= FFFF, we use the 3 byte representation:
    (a) Translate to binary:
      2    0    A    C
    0010 0000 1010 1100
    (b) Group bits into groups of 4, 6, and 6 bits:
    0010 0000 1010 1100 --> 0010 000010 101100
    (c) Prepend the UTF-8 bits 1110, 10, and 10.
    0010 000010 101100 --> 11100010 10000010 10101100
    (d) Group into groups of 4 bits and translate
        back to hex:
    11100010 10000010 10101100 --> 
    1110 0010 1000 0010 1010 1100 --> E282AC
    
    Check your answer. Copy and paste the Euro character € into Notepad or another editor that supports Unicode. Then display the hex dump of the file by invoking powershell and then entering the command
    Format-Hex filename.txt
    
    If you are on a Mac, which runs Unix, use the octal dump with the hex flag:
    od -x filename.txt
    
  5. What kind of UTF-8 encoding do Android apps use?
    Ans: They use modified UTF (MUTF-8), which allows one, two, and three byte representations.

Adding Widgets Dynamically

Drawing in a View

Responding to Touch Events

Challenge Problem