Sunday, 27 January 2013

Stack Overview






The Android operating system is like a cake consisting of various layers. Each layer has
its own characteristics and purpose. The layers are not cleanly separated but often seep
into each other.

When you read through this chapter, keep in mind that I am concerned only with the
big picture of the entire system and will get into the nitty-gritty details later on. Figure 2-1 shows the parts of the Android stack.

Android and Java


In Java, you write your Java source file, compile it into a Java byte code using the Java
compiler, and then run this byte code on the Java VM. In Android, things are different.
You still write the Java source file, and you still compile it to Java byte code using the
same Java compiler. But at that point, you recompile it once again using the Dalvik
compiler to Dalvik byte code. It is this Dalvik byte code that is then executed on the
Dalvik VM. Figure 2-2 illustrates this comparison between standard Java (on the left)
in Android using Dalvik (on the right).


It might sound like you have to do a lot more work with Android when
it comes to Java. However, all these compilation steps are automated
by tools such as Eclipse or Ant, and you never notice the additional
steps.

You may wonder, why not compile straight from Java into the Dalvik byte code? There
are a couple of good reasons for the extra steps. Back in 2005, when work on Dalvik
started, the Java language was going through frequent changes, but the Java byte code
was more or less set in stone. So, the Android team chose to base Dalvik on Java byte
code instead of Java source code.
A side effect of this is that in theory you could write Android applications in any other
language that compiles down to Java byte code. For example, you could use Python or
Ruby. I say “in theory” because in practice the appropriate libraries that are part of the
SDK would need to be available. But it is likely that the open source community will
come up with a solution to that in the future.

Another thing to keep in mind is that Android Java is a nonstandard collection of Java
classes. Java typically ships in:

Java Standard Edition
    Used for development on basic desktop-type applications
Java Enterprise Edition (aka J2EE or JavaEE)
    Used for development of enterprise applications
Java Micro Edition (aka J2ME or JavaME)
    Java for mobile applications

Android’s Java set of libraries is closest to Java Standard Edition. The major difference
is that Java user interface libraries (AWT and Swing) have been taken out and replaced
with Android-specific user interface libraries. Android also adds quite a few new fea-
tures to standard Java while supporting most of Java’s standard features. So, you have
most of your favorite Java libraries at your disposal, plus many new ones.

Creating a Window


GUI components such as windows and labels are, in fact, objects. JFrame is a class and the GUI component window can be created by using a JFrame object. Various attributes are associated with a window. For example:

  • Every window has a title.

  • Every window has width and height.

JFrame


The class JFrame provides various methods to control the attributes of a window. The methods provided by the class JFrame are:

public JFrame()
public JFrame(String s)
public void setSize(int w, int h)
public void setTitle(String s)
public void setVisible(Boolean b)
public void setDefaultCloseOperation(int operation)
public void addWindowListener(WindowEvent e)

Teaching Tip

Descriptions of these methods can be found in Table 6-1 in the text.  A more detailed description of JFrame’s methods can be found at:

One way to create a window you can declare an object of the type JFrame, instantiate the object, and then use the various methods listed above to manipulate the window. In this case, the object created can use the various applicable methods of the class.

Another way is to create the class containing the application program by extending the definition of the class JFrame; to build the class containing the application program “on top of” the class JFrame, utilizes the mechanism ofinheritance. Inheritance means that a new class can be derived from or based on an already existing class. 


When using inheritance, the class containing your application program has more than one method. In addition to the method main, it has at least one other method that will be used to create a window object containing the required GUI components (labels, text fields, etc). This additional method is called aconstructor; it is a method of a class that is automatically executed when an object of the class is created. Typically, it is used to initialize an object and its name is always the same as the name of the class.  Because inheritance is an important concept in programming languages such as Java, we the second way of creating a window is described in detail in this chapter. The definition of the class JFrame is extended by using the modifier and reserved word extends. 

Teaching Tip

Chapter 11 discusses the principles of inheritance in detail. Constructors are covered in detail in Chapter 8.

The class JFrame is contained in the package javax.swing. An important property of inheritance is that the class (subclass) that extends the definition of an existing class—called a superclass—inherits all the properties of the superclass.  The methods setTitle, setSize, setVisible, and setDefaultCloseOperation are methods of the class JFrame, and these methods can be inherited by its subclasses.

A Simplified OOD Methodology


A simplified OOD methodology can be expressed in the following steps:

1.      Write down a detailed description of the problem.

2.      Identify all (relevant) nouns and verbs.

3.      From the list of nouns, select objects. Identify data components of each object.

4.      From the list of verbs, select the operations.

In order to create objects you first need to create classes. To know what type of classes to create, you need to know what an object stores and what operations are needed to manipulate an object’s data.  Because an object consists of data and operations on data in a single unit, in Java we use the mechanism of classes to combine data and its operations in a single unit. In OOD methodology, classes, data members of classes (known as fields), and operations are identified.

Teaching Tip

More information on Object-Oriented Design can be found at:


Implementing Classes and Operations

Once the relevant classes, data members of each class, and relevant operations for each class are identified, the next step is to implement these things in Java. 

In Java, to implement operations we write algorithms. Since there is usually more than one operation on an object, each algorithm is implemented with the help of Java’s methods.  However, Java does not provide all the methods that you will ever need. Therefore, to learn how to design and implement classes, you first must learn how to construct and implement your own methods.

Object-Oriented Design (OOD)


The aim of OOD is to build software from components called classes so that if someone wants to use a class, all they need to know is the various methods provided by that class. 

An object combines data and operations on that data in a single unit, a feature called encapsulation.  In OOD, the object is identified first, then the relevant data is identified, and finally the operations needed to manipulate the object are identified.

The aim of OOD is to build software from components called classes so that if someone wants to use a class, all they need to know is the various methods provided by that class. 

An object combines data and operations on that data in a single unit, a feature called encapsulation.  In OOD, the object is identified first, then the relevant data is identified, and finally the operations needed to manipulate the object are identified.

JButton: Handling an Event


When you click a JButton an event is created, known as an action event, which sends a message to another object, known as an action listener. When the listener receives the message, it performs some action. Sending a message or an event to a listener object simply means that some method in the listener object is invoked with the event as the argument. This invocation happens automatically; you will not see the code corresponding to the method invocation. However, you must specify two things:

  • For each JButton, you must specify the corresponding listener object. In Java, this is known as registering the listener.

  • You must define the methods that will be invoked when the event is sent to the listener. Normally, you will write these methods and you will never write the code for invocation.

Java provides various classes to handle different kinds of events. The action event is handled by the class ActionListener.  The class ActionListener contains the method actionPerformed, which includes the code the system is to execute when an action event is generated.

The class ActionListener that handles the action event is a special type of class called an interface.  An interface contains only the methods headings; each methods heading is terminated with a semicolon.

Because the method actionPerformed does not contain a body, you cannot instantiate an object of the type ActionListener.  One way to register an action listener with an object is to create a class on top of ActionListener so that the required object can be instantiated. The class created must provide the necessary code for the method actionPerformed.  The interface ActionListener is contained in the package java.awt.event.

Teaching Tip

Tutorials regarding how to write various types of listeners, including ActionListeners, can be found at: