Java Reference
In-Depth Information
int resultOfMathFPMul = MathFP.mul (MathFP.toFP ("1.231"),
MathFP.toFP ("3.14"));
System.out.println (MathFP.toString (resultOfMathFPMul));
In order to show how the MathFP library can be used in a real-life application, let's implement a small
MIDP calculator. The main functionality of the application is done in the
commandAction()
method, where the arithmetic operations take place. MathFP functions are used after the user activates
the equals (
=
) command, depending on the operator selected for the current calculation.
shows the application in action.
Figure 10.3. The
CalculatorMidp
MIDlet.
Listing 10.8
CalculatorMidp.java
—The
CalculatorMidp
Sample Source Code
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import net.jscience.math.kvm.*;
public class CalculatorMidp extends MIDlet implements CommandListener
{
char operator = '=';
StringItem operand1Item = new StringItem ("", "");
TextField operand2Field = new TextField ("", "", 7,
TextField.NUMERIC);
TextField operand2FField = new TextField (".", "", 5,
TextField.NUMERIC);
Display display;
Form list;
public CalculatorMidp() {
display = Display.getDisplay (this);
list = new Form ("Calculator");
list.append (operand1Item);
list.append (operand2Field);
list.append (operand2FField);
list.addCommand (new Command ("+", Command.SCREEN, 1));
list.addCommand (new Command ("=", Command.SCREEN, 2));
list.addCommand (new Command ("-", Command.SCREEN, 3));
list.addCommand (new Command ("*", Command.SCREEN, 3));
list.addCommand (new Command ("/", Command.SCREEN, 2));
list.addCommand (new Command ("CLR", Command.SCREEN, 4));
list.setCommandListener(this);
}
public void startApp() {
display.setCurrent (list);
}
public void pauseApp() {
}
Search WWH ::
Custom Search