251 ps1

Computer Science 251
Android App Development

Assignment 1: Threads and Anonymous Inner Classes, and your First Android App

Due 11:59pm Friday 27 April

Objectives

  1. Understand threading (a.k.a. concurrency), an essential part of mobile apps on any operating system.
  2. Be able to create and exploit anonymous inner classes, which are used in building most Android apps.
  3. Be able to add new functionality to an existing app
  4. Know how add listeners (event handlers) to existing widgets
  5. Know how to use images (instead of text) for labeling buttons

Part 1: Threading / concurrency in Python

To get started, download and unzip this script.

Our goal in this part is to understand threading / concurrency in a general way. So we will do this part of the assignment in the language that makes threading (and most other things) explicit and simple, namely, Python. Although the example is rather contrived (combining a Graphical User Interface and a Text User Interface), it illustrates the motivation for threading in a powerful way that will help you in developing and debugging real-world applications.

In the folder you unzipped there is a Python script ball.py Run the this script using Python3. As you can see, this script uses Tkinter to move a little red ball horizontally across the display. Looking at the code, you’ll see a Ball class that has an instance variable supporting the direction of movement, and an instance variable flagging for user-requested quit. To complete this exercise, you will add a threaded function that will loop forever, asking the user whether they want to move the ball left, move it right, or exit the program (use Python’s built-in input function), and setting the appropriate instance variable (the existing code will take care of the rest).

Part 2: Anonymous inner classes in Java

Our textbook assumes that you know about anonymous inner classes in Java. Since I have not seen these used outside of the context of Android development, I wanted us to do a simple coding exercise, like the Python exercise above, allowing us to understand the concept in a general way. In the process, you will get a refresher on crucial Java concept like interfaces and method overriding.

In the folder you unzipped there are also three small Java source files: ClassA.java, ClassB.java, and GreeterInterface.java. To keep things simple, we’ll compile and run this code directly from the command-line in a terminal window (I’m using % to indicate the command-line prompt):

% javac ClassA.java
% java ClassA

Take a few minutes to look over the contents of these three small source files. Then modify ClassA.java (and no other file!) to use an anonymous inner class with @Override. To see how you’ll do this, look at an example in our textbook, or do what I did: just google java anonymous inner class. The examples you will find will likely be a lot more complicated than what you need to do here, which is the whole idea: we are using a bare-bones example to to understand the idiom of anonymous inner classes, apart from their use in Android development.

Part 3: Your First Android App

For the rest of this assignment and future ones, we will work directly from the end-of-chapter challenges in the textbook. Read through the three Challenges at the end of Chapter 2 (Add a Listener to the TextView, Add a Previous Button, and From Button to ImageButton ), and implement them as instructed there. You will need to follow the step-by step directions in Chapter 2 to reach this point. So you should also have a nice icon for your app (see Adding an Icon, five or six pages before the Challenges).

For this and future challenges, you only need to submit a single, final version of the app. For example, the third challenge (convert button to image button) modifies the second challenge (add a Previous button), so I will be looking for the text-view listener and the image-button version from the third challenge, but not the intermediate version from the second challenge.

What to turn in

  • Upload your modified ball.py and ClassA.java to sakai. To test ball.py I will simply open it in IDLE3 and hit F5. To test your ClassA.java I will run the following commands in the terminal window:
    % javac ClassA.java
    % java ClassA
    

    I will also look at your ClassA.java code to make sure that you rewrote it to use an anonymous inner class. Again, do not modify any other Java files, because I will use the original versions.

  • Zip up the root directory of your modified GeoQuiz app and submit the zipfile to sakai.

If you’re smart, you’ll set aside an extra few minutes to download your sakai submissions as test them as above. You will get no partial credit for something that doesn’t compile or doesn’t run.