To Lecture Notes

IT 372 -- May 29, 2024

Review Questions

  1. 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.
  2. Implement the Magic8Ball Example using Kotlin for the Prediction and MainActivity classes.
    Answer: set the application name to Magic8Ball2.  Here are two possible solutions.
    Solution 1: Use instance variables for the widget objects in the MainActivity class:
          activity_main.xml  Prediction.kt  MainActivity.kt
    Solution 2: Use local variables for widget objects in the onCreate method:
          MainActivity.kt

Introduction to Kotlin

Practice Problems

  1. Write a Java app that creates a database table pets with this data:
    name animal_type age
    Bently Dog 6
    Simba Cat 5
    Cottontail Rabbit 3

    Then display the table data in a TextView widget. Leave the onCreate and onUpdate methods of the SQLiteOpenHelper empty. Create the database and populate it in the MainActivity class.
  2. 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
  3. Implement this KidsDb2 Example. Use these source code files:
         activity_main.xml   KidsDbHelper.java   MainActivity.java
  4. 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);