To Lecture Notes

IT 372 -- Jun 3, 2024

Review Questions

  1. What are these items in Kotlin?
    Any  Unit  ?  !!
    
    Answer:
    Any
    is the ultimate base class of all Kotlin classes, like Object is the base class for all Java classes.
    Unit
    is the equivalent of the Java datatype void.  It means no return value for a method.
    ? is a suffix for a datatype, which indicates that a variable with that datatype is nullable, i.e., the variable  can contain a null value, for example:
    var s : String? = null
    
    !! means that the programmer knows that a variable is defined as nullable and that calling a method or property from that variable will cause an exception if its value is set to null. For example:
    var s : String? = "abc"
    println(s!!.uppercase)
    s = null
    println(s!!.uppercase)
    // Output:
    ABC
    
    
  2. Look at Example 15 in the Introduction to Kotlin document. This example is on custom getters and setters
  3. Look at Example 19 in the Introduction to Kotlin document. This example is on anonymous functions and functions in arrow notation.
  4. Explain how to do the following related to using databases. Assume that you are using the KidsDbHelper class with base class SQLiteDbHelper:
    1. Create a new database. Answer:
      A new database is automatically created when the onCreate method of the SQLiteDbHelper executes. You can put additional code in the onCreate method to populate the database or leave this method empty. If you leave the onCreate method empty, the code to create tables in the database and populate them will be done in the MainActivity class.
    2. Create a new table in a database. Answer:
      SQLiteOpenHelper dbh = new KidsDbHelper(this);
      SQLiteDatabase db = dbh.getWritableDatabase( );
      db.execSQL("<<SQL Create statement>>");
      
    3. Add rows to a database table. Answer:
      Use the put method to store values in a ContentValues object named values. Then use this object to insert data into the database table:
      db.insert("<<table_name>>", null, values); 
      
    4. Query a table. Answer:
      Create a Cursor object that contains data from querying the table.
    5. Access data in a Cursor object. Answer:
      Use the Cursor methods moveToFirst, moveToNext, moveToPrevious, moveToLast to navigate between rows. Use the Cursor methods getString, getInt, and getDouble to obtain data values from a row.
  5. 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
  6. Write an app that imports the data from kids.txt. Then display in a textview the results of these queries
    Here are the three source code files for Query a
         activity_main.xml   KidsDbHelper.java   MainActivity.java
    Only the changes shown in the main activity query and output setup are shown for the rest of the queries.
    1. The name, gender, and age of all the kids.
      // Answer for a:
      Cursor cursor = db.query("kids", 
          new String[ ]{"name", "gender", "age"},
          null, null, null, null, null);
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          String gender = cursor.getString(1);
          int age = cursor.getInt(2); 
          display += String.format("%s %s %d\n", name, gender, age); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    2. The name and age of the girls.
      // Answer for b:
      Cursor cursor = db.query("kids", 
          new String[ ]{"name", "age"},
          "gender = 'F'",
          null, null, null, null);
          String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          int age = cursor.getInt(1); 
          display += String.format("%s %d\n", name, age); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    3. the name and age of the boys that are 10 years or older.
      // Answer for c
      Cursor cursor = db.query("kids",
          new String[ ]{"name", "age"},
          "gender = ? and age >= ?",
          new String[ ]{"M", "10"}, null, null, null);
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          int age = cursor.getInt(1); 
          display += String.format("%s %d\n", name, age); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    4. The names for all kids, sorted in ascending order by name.
      // Answer for d:
      Cursor cursor = db.query("kids",
          new String[ ]{"name"},
          null, null, null, null, "name");
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          display += String.format("%s\n", name); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    5. The name and age for all girls, sorted in descending order by age.
      // Answer for e:
      Cursor cursor = db.query("kids",
          new String[ ]{"name", "age"},
          null, null, null, null, "age desc");
          String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          int age = cursor.getInt(1); 
          display += String.format("%s %d\n", name, age); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    6. The name and age for all kids, sorted by gender; within each gender, sort by age.
      // Answer for f:
      Cursor cursor = db.query("kids",
          new String[ ]{"name", "age"},
          null, null, null, null, "gender, age");
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          String name = cursor.getString(0);
          int age = cursor.getInt(1); 
          display += String.format("%s %d\n", name, age); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
    7. The average age for all kids.
      // Answer for g:
      Cursor cursor = db.query("kids",
          new String[ ]{"avg(age)"},
          null, null, null, null, null);
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          double average = cursor.getDouble(0);
          display += String.format("%6.3f\n", average); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      
      
    8. The average age listed by gender separately. For example:
      F 8.653
      M 9.189
      
      // Answer for h:
      Cursor cursor = db.query("kids",
          new String[ ]{"gender", "avg(age)"},
          null, null, "gender", null, null);
      String display = "";
      cursor.moveToFirst( ); 
      do { 
          String gender = cursor.getString(0);
          double average = cursor.getDouble(1); 
          display += String.format("%s %6.3f\n", gender, average); 
      } 
      while(cursor.moveToNext( ));
      txtOutput.setText(display); 
      

Intro to Jetpack Compose