To Lecture Notes

IT 372 -- May 22, 2024

Review Questions

  1. What is the difference between the Kotlin val and var keywords?
  2. Define a Kotlin Point class specified by this UML diagram:
    +----------------------------------------+
    |                 Point                  |
    +----------------------------------------+
    | - x : Float {get}                      |
    | - y : Float {get}                      |
    +----------------------------------------+
    | + distanceFromOrigin( ) : Float        |
    | + equals(otherPoint : Point) : Boolean |
    | + toString( ) : Boolean                |
    +----------------------------------------+
    
    Answer: See Example 14 in the Introduction to Kotlin document.
  3. Define Kotlin Animal and Pet classes with these instance variables:
    Animal: species  age  vaccinated
    Pet:    name   owner
    
    Pet inherits from Animal. Define toString methods for each class. Test each class.
    Answer: See Example 18 in the Introduction to Kotlin document.
  4. Implement the ClickCounter Example in Kotlin.
    Answer:
  5. Implement the Magic8Ball Example using Kotlin for the Prediction and MainActivity classes.

Introduction to Kotlin

Creating and Using SQL Tables in Android Apps

  1. Create SQLite database.
    Define a class derived from SQLiteOpenHelper:
    public class SDBH extends SQLiteOpenHelper {
        public KidsDbHelper(Context context) {
            super(context, "kids2.db", null, 1);
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            // This method is only called if the database
            // Does not exist.
    
            // Recommendation: leave the body of this
            // method empty.  Put code for creating and
            // populating tables in the activity file.  
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, 
            int i, int i1) {
            // We will not use this onUpgrade method 
            // in IT 372, but it is still required.
        }
    }
    
  2. Create SQLite table in the activity file:
    SQLiteOpenHelper dbh = new KidsDbHelper(this);
    SQLiteDatabase db = dbh.getReadableDatabase( );
    db.execSQL("create table kids(name text, 
        gender text, age integer)");
    
  3. Insert row into table:
    ContentValues studentValues = new ContentValues( );
    studentValues.put("name", name);
    studentValues.put("grade", grade);
    studentValues.put("gender", gender);
    db.insert("students", null, studentValues);
    
  4. Create Cursor to view or use query results:
    Cursor cursor = db.query("students",
        new String[ ] {"name", "grade", "gender"},
        null, null, null, null, null);
    
    The five null arguments correspond to these parameters:
    selection  selectionArgs  groupBy  having  orderBy
    
    We will see how to use these arguments later.
  5. Methods to position cursor within result set:
    cursor.moveToFirst( )
    cursor.moveToNext( )
    cursor.moveToPrevious( )
    cursor.moveToLast( )
    
  6. Methods to obtain field values from current cursor position:
    cursor.getDouble(columnIndex)
    cursor.getInt(columnIndex)
    cursor.getString(columnIndex)
    

Practice Problems

  1. Look at the StudentsDb1 Example.
  2. Look at the StudentsDb2 Example.
  3. KidsDb1 Example. Use this kids.txt file to populate a kids table in a database named kids.db.  Here is the table schema:
    name : text; gender : text; age : integer
    
    After the kids table has been populated, query the table to display all of its data concatenated in a single textview. Answer:
    Here are the files for the app: activity_main.xml   KidsDbHelper.java   MainActivity.java