WML Gadgets, Building a Calculator

Build an application for your mobile device. With this additional article on building WML gadgets, you can expand your knowledge of implementing WAP and WML while building a real application.

This series of articles describes how to provide Web content to mobile devices through WML (Wireless Markup Language). This article covers creating small programs that extend the functionality of mobile devices, building on the previous article and example.

Note: These articles cover WML and WMLScript version 1.1, which are supported by the majority of mobile devices in use today. The articles assume a working knowledge of HTML and general Web technologies, and further assume that you have read the previous article(s) in this series.

Review

As mentioned in Part I, it is possible to add value to a mobile device by creating a small but ultimately useful application. In designing such an application, remember that the user will need to be online to use it, so the application's utility needs to be weighed against the potential cost of use.

Note: Most mobile service plans offer a base amount of online time dedicated to "Web" use. So the user generally isn't paying more for the occasional gadget use.

Simple Calculator

This project will cover how to create a simple calculator, building on some of the concepts covered in the previous article. Our design is straightforward, allowing the user to add, subtract, multiply, or divide two numbers.

Reviewing Data Input

In the last article we determined that it is easier (or at least faster) for the user to enter a floating-point number in two parts—the whole number portion and the decimal portion. The main reason lies in the inability to mix only numbers and symbols in a WML <INPUT> tag's "format" parameter. Without this ability, the user must switch to a symbol set to enter a decimal point, and then back to numbers to enter the decimal part of the entry.

Note: See documentation on the WML <INPUT> tag, or see the last article for clarification of format masks.

We will use the following code to accept input of a floating-point number:

Designing the User Interface

WML supports rudimentary graphics, but displaying a graphical representation of a calculator--like you would find on a PDA--is a bit ambitious. Our design will have to be simple, boiled down to only the necessary components:

The two numbers involved in the computation

The operation to be performed

A few simple controls

Utilizing a <SELECT> statement, we can let the user change the input, change the operator, or perform the calculation. Consider the following rough draft of the interface:

1 <first number>
2 <operator>
3 <second number>
4 Compute

This compact display shows the user everything he/she needs to see and know—the numbers, the operator, and what to press to perform the operation. When the appropriate option is chosen, the code allows the user to change the corresponding data. For example, when the user presses "1" to change the first number, the code displays the <INPUT> statements outlined earlier. After accepting the input, the code returns to the display and displays the new value. The same is done for the second value, except the user presses "3" to enter the second number.

Choosing the operator is even easier—each selection of the option "2" cycles to a new operator: "+," "-," "x," or "/."

Finally, the user presses "4" to perform the operation.

Let's take an example and run through how the user would perform it:

22.5 / 3 (twenty-two point five, divided by three)

The user would do the following steps:

Press "1."

Enter "22".

Enter "5".

Press "2" three times (cycling from "+" to "-" to "x" and finally to "/")

Press "3."

Enter "3".

Enter "". (Nothing for the decimal portion)

Press "4."

Our calculator would then display the result of the calculation: 7.5.

Basic Coding

WML can't perform all the necessary tasks, especially the mathematic computations of the actual calculation. For the intelligence behind the gadget, we will use WMLScript.
We will need three WMLS functions:

A function to initialize the calculator

A function to cycle the operator

A function to perform the calculation

These functions are used in the following cases:

Initialization: When the calculator is first called, or reset

Cycle operator: When the user presses "2" on the main menu

Perform calculation: When the user presses "4" on the main menu

What we won't need is any logic for the other two main menu options—the input of both numbers. For simplicity's sake, let's start by coding the WML cards to display the main menu and accept the numeric input:

Note that for now we are using pseudo-calls in the main menu for the WMLS function calls (options 2 and 4).

The code is straightforward: a separate card is called for each number entry, the card accepts a whole number and decimal, and then the program returns to the display card (which displays the data entered). Each data-entry card offers a "clear" function by pressing the "option" key--pressing that key clears the input in the whole number and decimal inputs via the <REFRESH> mechanism.

Note that we do not combine the numbers, but display them manually on the "display" card (e.g., "$(num1).$(dec1)"). This allows us to maintain the entry of the whole number and decimal as two separate pieces. When the user chooses to modify these values, they are shown as entered, namely, as two separate values.

Note: You might have noticed the one artifact to our display method: the decimal is always displayed, even when there is no entry for the number.

FIGURE 10.02
When the user chooses to edit an entry, it is shown as entered—in two components.

Cycling the Operator

Essentially a handful of IF statements, this code cycles to the next operator every time it is called, rotating back to the first operator when it reaches the end. The "display" card is then called to display the new operator.

Note the use of "x" as the multiplication operator instead of the programming equivalent ("*") for the non-programming user.

Doing the Calculation

This function is a bit more complex because it must combine the whole number and decimal portions of both numbers and operate on the floating-point value of the combination. The routine then calls a "result" card to display the result of the calculation.

As you may recall from Part I, we can take advantage of WMLScript's untyped variables, using string functions to combine the whole number and decimal portions of each number--by concatenating the whole number, a decimal, and the decimal portion. Judicious use of the parseFloat function saves us from embarrassing results like this:

22.5 + 3.5 = 22.53.5 (concatenation instead of addition)

Putting It all Together

Putting all the code together, we arrive at the following completed example:

Room for Improvement

There are several things we could do to make the calculator more useful:

Store the result in the "first number" variable. This would allow the user to enter several calculations all in a row.

Add more functions: Exponents, square roots, etc. (Note that some functions—like square roots—would be performed only on the first number.)

Add a "Clear" function on the main "display" page to clear all data and reset the calculator.

Add a memory feature to store the result in memory for later recall. The ambitious programmer could add a host of memory features such as "Store in Memory," "Add to Memory," "Subtract from Memory," etc.

Fix the "decimal display" artifact.

Note that some of these features would also necessitate using more logic to parse the numbers into and out of the <INPUT> routine. For example, passing a value with a decimal amount to the first number would require breaking the number into two components (whole number and decimal) and storing each in the appropriate variable(s), or completely changing the scheme of how numbers are input.

Next Time

In the next article we will take these concepts further by implementing a calendar gadget with a little CGI integration.

What Do You Want from WML?

I'm interested in hearing what you need/want to do with WML. I'll use some of the more challenging or common ideas in upcoming articles. Send your ideas to the address below.

About the Author

Steve Schafer is the chief operating officer of Progeny Linux Systems, a Linux-based consulting company in Indianapolis, Indiana. He has written several technical books and articles and can be reached at sschafer@synergy-tech.com.