Computer Science 318
Mobile Apps and the Internet of Things

Assignment 1: Threads and Anonymous Inner Classes

Due Monday 26 September

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.
Because neither of these objectives requires us to write a complete Android app, we will not use Android Studio for this first assignment.

To get started, download and unzip this script.

Part 1: Threading / concurrency in Python

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.

What to turn in

Upload your modified ball.py and ClassA.java to sakai. I am going to test these programs as follows:
% python3 ball.py
%
% 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.

If you're smart, you'll set aside an extra few minutes to download your sakai submissions as test them as above.