Friday, February 29, 2008

To promote reusability

To reuse our source code or classes is a hard task specially when we don't think about this in the beginning of analysis or design. Even though we have already done our projects or programs that is not well-designed, we can use some refactoring function inside the IDE tool for us to adjust or arrange our source code, for instance, to extract the interface from classes. But, it is still much better if we know how to promote reusability and do it in the analysis/design phase. In the book, Pro Java Programming 2nd Edition, it provides 2 good principles (Loose Coupling and Strong Cohesion) for reference.

1. Loose Coupling
Coupling means that it refers to the degree to which classes depend upon one another, and two classes that are highly dependent upon each other are considered tightly (or highly) coupled. In other words, they cannot be used alone and also not be used for other classes. There are some examples from this book and I draw the class diagrams to explain the idea.

a.) This diagram shows that these two classes have strong coupling. They refer each other and are Bad-designed.

b.) This diagram shows that it uses an interface "FontListener" to reduce the coupling. "FontProperitesPanel" doesn't need to know who implements the interface "FontListener". It just calls the method "fontChanged()" in the interface, and "SampleTextFrame" that implements the interface will response.

c.) This diagram shows we create a "FontPropertiesFrame" class that extends "SampleTextFrame" that can eliminate "SampleTextFrame"’s references to the "FontPropertiesPanel" class and move them into a subclass of SampleTextFrame.
2. Strong Cohesion
If a class is highly cohesive, it means its responsibilities are closely related and that it’s complete. In other words, the class isn’t cohesive if it contains methods that perform unrelated functions or if some set of closely related functions is split across that class and one or more others.
So, I think that these 2 principles are quite useful when we design our classes structure.

Wednesday, February 27, 2008

A glimpse of Flex

This term "Rich Internet Applications (RIAs) is popular in a coupe of years recently. Adobe is one of company that provides a very good software and development kit that is based on Flash to build your Flash-based applications which belongs to RIA. The product, Flex, is the key software to achieve this goal. Currently Adobe is released Flex 3 builder that is a tool using Eclipse IDE and plug-ins to develop Flex web application. Because I download the trail version of Flex 2, the information of display as follows is about Flex 2.
There is a point that I need to clarify. Flash and Flex is not standard Ajax-based. because both of them need to download Flash plug-in and the SWF file is executed on the plug-in player.

(Data Source from Adobe)
A typical Flex application consists of the following elements.

Flex framewor

The Adobe® Flex 2 framework contains all the components you need to build rich Internet applications, which include:

Containers that you use to layout the application

Controls you use to gather data from users and to manage the user interface (Text and Button, for example)

Extensive data binding, formatting, and validation features

An event-driven development model that provides rich user interface transformation features such as effects and transitions.

The Flex framework is contained within a shared component library (SWC) file.

MXML

Every Flex application contains at least one MXML file, known as the main application file. MXML is a markup language, an implementation of XML that was designed specifically for creating Flex applications, and you use it to declaratively define the structure of your application using tags.

ActionScript 3.0

You add dynamic behavior to your applications using ActionScript 3.0, which is an implementation of ECMAScript and is similar to JavaScript. You can add ActionScript to Flex applications directly in the MXML file as script blocks or you can create separate files of ActionScript functions and import them into your MXML files.

CSS

Style attributes in visual components (buttons, list boxes, and so on) are controlled by component properties. For example, a button component has a fontFamily property that you use to set the font. Style properties are controlled by a theme, by styles defined in a CSS file, by styles defined in style blocks in an MXML file, or by setting individual style properties in the component instance itself.

Graphic assets

Like most applications, Flex applications contain a variety of graphic assets such as icons and other images.

Data

Some components are used to display data (a combo box or data grid for example) and you can populate these components with data by using arrays, collection objects, data models, external XML data sources, and so on.

(Data Source from Adobe)
Generating the data:
Since Flex applications do not directly connect to a database, you need to interact with data using some kind of service. In the following examples, the Flex HTTPService component is used to retrieve data from an XML file that is generated by PHP.

The first step in connecting to data is to generate the data that will be used in the Flex application. On the PHP side, you would follow these steps:

a. Create a database (MySQL for example) if one does not already exist.

b. Write a PHP script to both connect to the MySQL database and generate the XML-formatted data.

P.S: These steps also apply to any other tools you might use to generate the data (ColdFusion, ASP.NET, JSP, JavaEE server, and so on).

The screen shot of Flex 2 Builder:






Wednesday, February 20, 2008

A small problem in programming with Java ME

Recently I feel weird by dealing with threads and alerts in programming with Java ME. One of the problems is that my program will occur NullPointException in the console.

java.lang.NullPointerException
at javax.microedition.lcdui.Alert$1.commandAction(Alert.java:663)
at javax.microedition.lcdui.Alert$timeoutTask.run(Alert.java:1110)
at java.util.TimerThread.mainLoop(+237)
at java.util.TimerThread.run(Timer.java:462)


Based on this Exception message, I checked the source code of Java SE 1.6 version ( Thanks for Sun Microsystems to open the Java source ) of Timer and TimerThread because I cannot see the source in Java ME, so I use JavaSE to guess the structure of JavaME. P.S: The core APIs are still different even the package name are the same. And also, I drew a diagram as follows for myself to figure out the whole situation about my threads, alert, and timertask. This problem I guess is in "Alert" class.

Alert class allows to setup the timing for showing on the screen during it appears, and it still has a TimerThread to control seconds. TimerThread only can do one task at the same time. So there is a situation: If I let my "AlertThread" setup the alert before the alert disappear ( done its work) and it happens more than 1 task needed to do. Then it will occur the problem. After I changed the timeout of the Alert, it didn't happen the problem again.


Using the emulator of Wireless Tool kit 2.5.2 to run TimerMIDlet:




Thursday, February 14, 2008

New Android SDK is released!

If you saw the screen of the previous Android SDK before, you will quickly find out that this new Android SDK released is so different from looking and operating now. I learned some information about the previous SDK that had some bugs found by the community. So I think Android this time provides new SDK version for fixing the bugs, update APIs, and also enhances some basic functions, for instance maps application as following pictures:

The new desktop of Android platform

Maps application's new feature (zoom in)


I find my home in Taiwan...

Wednesday, February 13, 2008

A simple Google Android application Part II

In Android framework, Activity class just like MIDlet in Java ME, it provides a frame to display the UI or perform a task. To layout the widgets on Activity, we can use xml file to achieve that. This example has 2 Activity classes, MainActivity.java and Queen.java, each has xml file to define the widgets and layout. In Queen.java, it uses recursive method to solve the N x N Queen problem. Due to the algorithm is not relevant to Android platform, so I emit the most source code with the algorithm and only put the method putQtoArray() to show recursive way.as follows:

main.xml


<Button android:id="@+id/MainButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK"
android:layout_x="10px" android:layout_y="102px">
</Button>


<Button android:id="@+id/MainButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Cancel"
android:layout_x="80px" android:layout_y="102px">
</Button>




queen.xml:




<Button android:id="@+id/QueenButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK"
android:layout_gravity="right">
</Button>


MainActivity.java

package csu.android.applications;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.widget.EditText;

import android.text.method.*;

import android.view.View.OnClickListener;

import android.widget.Button;

public class MainActivity extends Activity {

private EditText myEText;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.main);

myEText = (EditText) findViewById(R.id.MainEditText1);

myEText.setInputMethod(DigitsInputMethod.getInstance());

Button okButton = (Button) findViewById(R.id.MainButton1);

Button cancelButton = (Button) findViewById(R.id.MainButton2);

okButton.setOnClickListener(mOKListener);

cancelButton.setOnClickListener(mCancelListener);

}

private OnClickListener mOKListener = new OnClickListener() {

public void onClick(View v) {

String tmp = myEText.getText().toString();

if (!myEText.getText().toString().equals("")) {

int Qnum = Integer.parseInt(tmp);

if (Qnum <= 9) {

// Here we start up the main entry point

Intent intent = new Intent(MainActivity.this, Queen.class);

intent.putExtra("NumKey", tmp);

startActivity(intent);

finish();

} else {

showAlert("The number is bigger than 9!", 0, "Try again!",

"OK", true);

myEText.setText("");

return;

}

}

}

};

private OnClickListener mCancelListener = new OnClickListener() {

public void onClick(View v) {

myEText.setText("");

}

};

}



Queen.java

package csu.android.applications;

import android.app.Activity;

import android.content.Intent;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import java.util.*;

public class Queen extends Activity {

EditText myEText;

int QueenNum;

int totalcount = 0; // the total number of distinct results.

int uniquecount = 0; // the total number of unique results.

boolean[][] arr;

// Record the scores for mapping

ArrayList al;

// For check the duplicate solutions

int[][] arrRotation0;

int[][] arrRotation90;

int[][] arrRotation180;

int[][] arrRotation270;

int[][] arrMirror;

private StringBuilder sb;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);

setContentView(R.layout.queen);

myEText = (EditText) findViewById(R.id.QueenEditText1);

Button backButton = (Button) findViewById(R.id.QueenButton1);

backButton.setOnClickListener(this.mBackListener);

Bundle extras = getIntent().getExtras();

if (extras != null) {

QueenNum = Integer.parseInt(extras.getString("NumKey"));

startCalculation();

myEText.setText(sb.toString());

} else {

showAlert("There is no input!", 0, "Click me", "OK", true);

Intent intent = new Intent(Queen.this, MainActivity.class);

startActivity(intent);

finish();

}

}

private OnClickListener mBackListener = new OnClickListener() {

public void onClick(View v) {

Intent intent = new Intent(Queen.this, MainActivity.class);

startActivity(intent);

finish();

}

};

/*

* startCalculation This method implements recursion for N*N Queen problem.

*/

public void startCalculation() {

sb = new StringBuilder();

// Array for N*N Queens problem

arr = new boolean[QueenNum][QueenNum];

// Array for calculating the duplicate result.

arrRotation0 = new int[QueenNum][QueenNum];

arrRotation90 = new int[QueenNum][QueenNum];

arrRotation180 = new int[QueenNum][QueenNum];

arrRotation270 = new int[QueenNum][QueenNum];

arrMirror = new int[QueenNum][QueenNum];

al = new ArrayList();

// Prepare random numbers inserted into arrays

// for the calculation of the duplication .

prepareRotationData();

// Do recursions

putQtoArray(QueenNum - 1);

}

/*

* putQtoArray This method implements recursion for N*N Queen problem.

* @param: QueenNum

*/

public void putQtoArray(int myNum) {

if (myNum == -1) {

// Find a distinct result.

totalcount++;

// Display the result.

showMe();

return;

}

for (int i = 0; i <>

if (!checkRule(i, myNum)) {

arr[i][myNum] = true;

// Recursion

putQtoArray(myNum - 1);

arr[i][myNum] = false;

}

}

}

... ... rest of part is omitted ... ...

}

Because Activity "Queen" will be started by Activity "MainActivity" using Intent, we also need to add a line into AndroidManifest.xml as below, then that is all set. <activity android:name="Queen" android:label="@string/app_name">

A simple Google Android application Part I

What is Google Android? I don't need to babble that... because I think that it will be very clear if you see this : http://code.google.com/android/what-is-android.html


Basically, Android includes Linux kernel as operating system, libraries ( C language), Android runtime ( Dalvik virtual machine for Java byte code), and application framework (APIs for Java) . The applications we wanna develop are based on this application framework and Java 1.5 language specification, and will run on Android runtime.

Until now, Google only has prototype cell phone for running Android platform. Due to this reason, all the applications need to execute on the emulator which emulates the same environment just like you really have the hardware. There is a simple example that can give an idea about developing a Android application.

This example will ask you a number for defining N x N Queen problem.
Then it will calculate the possibility of results showing as a simple checker.

P.S: It runs on the Android emulator.

Monday, February 11, 2008

JavaFX Scripting Language

As we know that scripting language for an operating system is called a shell script such as C shell.
In January 2007, IronPython was released. It is a new implementation of the Python programming language running on .NET. This year also in January, Sun was released The JavaFX Script Programming Language.
These two scripting language are amazing for developers to control the applications dynamically during the run-time. For example, you can create a Frame in the command line like : var jframe= new JFrame("This is a Frame"); or use JavaFX code to define it. Then, you can modify the attributes and see the change immediately. I think that it is useful when developers wanna design GUI applications because arranging UI components and layouts are tedious jobs. More details about Java FX Scripting Language as link: https://openjfx.dev.java.net/JavaFX_Programming_Language.html
http://java.sun.com/developer/technicalArticles/scripting/javafx/ria_1/

Thursday, February 7, 2008

[Android] DroidDraw

Dealing with the widgets and layouts in the Android project is a tedious work, especially Eclipse plug-in Android right now doesn't have visual tool for developers to construct the interfaces.
All the things need to do with XML. Fortunately, there is a tool that is very useful to it.
It also provides tutorials that developers can quickly understand how to use it.

http://www.droiddraw.org/

Monday, February 4, 2008

[Flickr] Schmap: San Francisco Photo Short-list

Today I got a email from Flickr about my one photo that has been short-listed for inclusion in the fourth edition of our Schmap San Francisco Guide, to be published mid-February 2008.
Schmap: San Francisco Photo Short-list
http://www.schmap.com/shortlist/p=61565007N00/c=SF20012284

That photo was taken from Hyatt Hotel in San Francisco.
I don't know that my old digital camera (Nikon 995) was still able to take good photos.