To Lecture Notes

IT 372 -- Apr 20, 2026

Review Exercises

  1. What are getters and setters in a Java class? How are getters and setters named for the instance variable accountBalance defined like this?
    private double accountBalance;
    
    Are there any exceptions to this naming convention?
    Answer: the getter and setters for the accountBalance instance variable are getAccountBalance and setAccountBalance, respectively. Here are the standard definitions of these methods:
    public double getAccountBalance( ) {
        return accountBalance;
    }
    
    public void setAccountBalance(double balance) {
        accountBalance = balance;
    }
    
    Extra code can be added to the setter to validate the new value. If the setter is omitted, accountBalance is made to be read only. The exception to the naming convention is if the instance variable is boolean, for example,
    private boolean checked;
    
    Then the getter becomes isChecked, not getChecked.
  2. Suppose you have a HashMap collection:
    HashMap<String, Integer> map = new HashMap<String, Integer>( );
    
    How do you add an Integer object to the collection using the key "K123"? How do you retrieve the Integer object? We will use a similar concept when we store information from an activity in a storedInstanceState object. Answer:
    // Put the value 35 into the Hashmap using the key "C123".
    map.put("C123", 35);
    // Retrieve the value stored using the key "C123" from the hashmap.
    int n = map.get("C123");
    
  3. Look again at the Activity Lifecycle Section of the April 15 notes. Create the LifeCycle app that writes these strings to the Logcat Window for each corresponding method that executes:
    In onCreate method.
    In onStart method.
    In onResume method.
    In onPause method.
    In onDestroy method.
    
    Answer: Code to be posted soon.
  4. Look at the ClickCounter Example.
  5. Look at the SendMessage Example. This example shows how to add a second activity to an app.
  6. In the SendMessage Example in the previous problem, how does the app know which is the startup activity, MainActivity.java or SendMessageActivity.java?
    Answer: the startup activity is specified in the AndroidManifest.xml file. Here is the XML code for MainActivity:
    <activity
        android:name=".MainActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    MainActivity is identified as a LAUNCHER activity in the intent-filter so it is the startup activity. Here is the XML code for the SendMessageActivity:
    <activity
        android:name=".DisplayItemsActivity"
        android:exported="false" />
    
    It does not have an intent-filter identifying it as a LAUNCHER activity so it cannot be the startup activity.
  7. Create an app named ShowSuit. Add a radiobutton group with four radio buttons and an ImageView widget to the ConstraintLayout. Set the text for the radio buttons to Club, Diamond, Heart, and Spade. Set up an onCheckedChangeListener event handler that changes the image of the imageview to club.png, diamond.png, heart.png, or spade.png, depending on the radiobutton that is clicked. Here is a zipfile of the suit images.  Use an anonymous inner class to set up the event handler.
    Answer: Copy the suit images files into the res/drawable folder. Here are the source code files:
    activity_main.xml  MainActivity
  8. Write apps that test these widgets: SeekBar and CalendarView. Answer:
    TestSeekBar Files: activity_main.xml   MainActivity.java
    TestCalendar Files: activity_main.xml   MainActivity.java