To Lecture Notes

IT 372 -- May 28, 2025

Review Questions

  1. Show how to use a View object defined by this MyView class in a layout file:
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Paint;
    import android.graphics.Color;
    import android.util.AttributeSet;
    import android.view.View;
    
    public class MyView extends View {
        
        public MyView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public void onDraw(Canvas c) {
            Paint p = new Paint( );
            p.setTextSize(30);
            p.setColor(Color.BLACK);
            c.drawText("Test MyView", 100, 100, p);
        }
    }
    
    Answer:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 
        xmlns:schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <it372.ssmith.testmyview.MyView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/myview" />
    
    </LinearLayout>
    
  2. Translate this Java statement into Kotlin:
    String s = (n <= 100) ? "Large" : "Small";
    // Answer
    var n = if (n <= 100) "Large" else "Small"
    
  3. What do these keywords and operators mean in Kotlin?
    field  value  Any  Unit  open  override  :  ?  !!
    
    Answers:
    field denotes the backing field of a Kotlin instance variable. Recall that a Kotlin instance variable is, by default, automatically supplied with a getter and optionally a setter. See the Kid class discussed in Exercise 3.

    value denotes the value to which a backing field is changed by a setter, for example:
    k.age = 11  // 11 is the value.
    

    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. The logic for this name for no return value is that unit means one; the return value can only be one possible value, which is the empty set.

    : denotes inherits from:
    class DerivedClass : BaseClass { }
    
    ? 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
    
  4. Rewrite this class to explicitly define the getters and setters:
    class Kid(val name : String, val gender : Char, var age : Int) {
        override toString( ) : String {
            return "$name; $gender; $age:
        }
    }
    // Answer:
    class Kid(n : String, g : Char, a : Int) {
    
        // Customized getters:
        val name : String = n
            get( ) = field
        val gender : Char = g
            get( ) = field
    
        // Customized getter and setter:
        val age : Int = a
            get( ) = field
            set(value) { field = value }
    
        override toString( ) : String {
            return "$name; $gender; $age:
        }
    }
    
  5. In the Introduction to Kotlin document, look at these sections: Functions with Named Parameters and Anonymous and Lambda Functions.
  6. 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.
  7. 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
  8. Write an app that uses the table created in the previous exercise. 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