To Lecture Notes

IT 372 -- May 5, 2025

Review Exercises

  1. Create an Android app that contains an arraylist that holds Dog objects, defined by this UML 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

ASCII Characters, Binary, and Hex

UTF-8 Encoding

More UTF-8 Examples

  1. Find the UTF-8 representations for these two non-Latin characters: δ (Greek letter delta) and Щ (uppercase Cyrillic character shcha). Android uses this modified UTF-8 encoding system (MUTF-8) where up to three bytes may be encoded.
                 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
    In the Byte 1, Byte 2, and Byte 3 columns, x represents the binary bits of the Unicode encoding for the character; 0 and 1 represent the special UTF-8 encoding bits.
    1. Find the UTF-8 representation for the Greek letter δ (lower case delta).
      1. Look up the Unicode code for δ in this document:
        https://www.unicode.org/charts/PDF/U0370.pdf
        The hex Unicode code is 03B4.
      2. Translate the hex code to binary:
          0    3    B    4
        0000 0010 1011 0100   
        
      3. Because 03B4 is between 0080 and 07FF, use the Two Bytes row. Place the binary bits of the unicode code (represented by x in the table) into groups of five and six:
        0000 0010 1011 0100
        01010 110100  
        
      4. Add the UTF-8 encoding bits 110 and 10 from the Two Bytes row:
        01010 110100
        11001010 10110100
        
      5. Group the binary bits into groups of four and translate back to hex:
        11001010 10110100
        1100 1010 1011 0100 
          C    A    B    4
        
      Therefore the UTF-8 encoding for β is CAB4.
    1. Find the UTF-8 representation for the Cyrillic letter Щ (upper case shcha).
      1. Look up the Unicode code for Щ in this document:
        https://www.unicode.org/charts/PDF/U0400.pdf
        Therefore the hex Unicode code is 0429.
      2. Translate the hex code to binary:
          0    4    2    9
        0000 0100 0010 1001   
        
      3. Because 0429 is between 0080 and 07FF, use the Two Bytes row. Place the binary bits of the unicode code (represented by x in the table) into groups of five and six:
        0000 0100 0010 1001   
        10000 101001    
        
      4. Add the UTF-8 encoding bits 110 and 10 from the Two Bytes row:
        10000 101001
        11010000 10101001
        
      5. Group the binary bits into groups of four and translate back to hex:
        11010000 10101001
        1101 0000 1010 1001
          D    0    A    9
        
      Therefore the UTF-8 encoding for Щ is D0A9

Practice Problems

  1. 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.
  2. 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
    

Adding Widgets Dynamically

Drawing in a View

Responding to Touch Events

Challenge Problem