To Lecture Notes

IT 372 -- May 19, 2025

Review Questions

  1. What is the difference between the Kotlin val and var keywords?
    Answer: a variable defined with val is a constant, whose value cannot be changed. A variable defined with var can be changed.
  2. Define a Kotlin Animal class specified by this UML diagram:
    +----------------------------------------+
    |               Animal                   |
    +----------------------------------------+
    | - species    : String {get}            |
    | - age        : Int {get, set }         |
    | - vaccinated : Boolean {get, set}      | 
    +----------------------------------------+
    | + haveBirthday( )                      |
    | + vaccinate( )                         |
    | + toString( ) : String                 |
    +----------------------------------------+
    
    Answer:
    // Source code file: Animal.kt
    package it372.ssmith.testkotlin
    // species is defined with val so it only
    //     has an autogenerated getter.
    // age is defined with var, so it has
    //     an autogenerated getter and setter
    class Animal(val species : String, var age : Int) {
        var vaccinated = false;
    
        fun vaccinate( ) {
            vaccinated = true;
        }
    
        fun haveBirthday( ) {
            this.age++;
        }
    
        override fun toString(): String {
            return "$species; $age; $vaccinated"
        }
    }
    ---------------------------------------------
    // Source code file: Test1.kt
    package it372.ssmith.testkotlin
    fun main( ) {
        var animal1 = Animal("dog", 3);
        println(animal1)
        animal1.haveBirthday( );
        println(animal1.age)
        animal1.vaccinate( )
        println(animal1.vaccinated)
    }
    
  3. Implement the Magic8Ball2 Example using Kotlin for the Prediction and MainActivity classes.
    activity_main.xml  Prediction.kt  MainActivity.kt

Introduction to Kotlin

Project 5

Review of SQL

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.